Java生成pdf中的表格

来源:互联网 发布:自治区有什么好处知乎 编辑:程序博客网 时间:2024/05/16 01:00

package cn.gei.test;

import java.awt.Color;
import java.io.FileOutputStream;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class TestPDFForTable {

 public static void main(String[] args) {
  try {
   //以A4的格式打印
   Document document = new Document(PageSize.A4, 71.4f, 71.4f, 71.4f,
     71.4f);
   //设置输出的位置及文件名
   PdfWriter writer = PdfWriter.getInstance(document,
     new FileOutputStream("c://TestPDFForTable.pdf"));
   document.open();
            //设置字体
   BaseFont bfChinese = BaseFont.createFont("STSong-Light",
     "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
   // 普通文字字体
   Font titleFont = new Font(bfChinese, 12, Font.BOLD);
   Font contentFont = new Font(bfChinese, 12);

   PdfPCell cell = null;
   PdfPTable table = new PdfPTable(new float[] { 20.9f, 79.1f });// 建立一个pdf表格
    table.setTotalWidth(380);// 设置表格的宽度
   table.setLockedWidth(true);// 设置表格的宽度固定

   cell = new PdfPCell(new Paragraph("标题一", titleFont));
   cell.setHorizontalAlignment(Element.ALIGN_CENTER);
   cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
   cell.setRowspan(2);
   table.addCell(cell);

   //一下连个单元格在页面显示是一个单元格,其实使用了一个小技巧,将表格的border的宽度设为0。
   Chunk chunk1 = new Chunk("内容长度可变", contentFont);
   Phrase phrase1 = new Phrase(chunk1);
   cell = new PdfPCell(new Paragraph(phrase1));

   cell.setFixedHeight(155.0f);
   cell.setBorderColorBottom(Color.red);
   cell.setBorderWidthBottom(0f);
   table.addCell(cell);

   cell = new PdfPCell(
     new Paragraph(
       "固定的内容,名字,时间等等",
       contentFont));
   cell.setBorderWidthTop(0.01f);
   cell.setBorderColorTop(Color.white);
   cell.setBorderWidthBottom(0);
   table.addCell(cell);
 
   cell = new PdfPCell(new Paragraph("标题二", titleFont));
   cell.setHorizontalAlignment(Element.ALIGN_CENTER);
   cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
   //合并行
   cell.setRowspan(3);
   table.addCell(cell);

   cell = new PdfPCell(new Paragraph("内容二 ", contentFont));
   cell.setFixedHeight(115.0f);
   cell.setColspan(2);
   table.addCell(cell);
   
   cell = new PdfPCell(
     new Paragraph(
       "^^^^^^^^^^^^^^^",
       titleFont));
   table.addCell(cell);
  
   cell = new PdfPCell(
     new Paragraph(
       ">>>>>>>>>>>>>>",
       titleFont));
   table.addCell(cell);
   
   cell = new PdfPCell(new Paragraph("标题三", titleFont));
   cell.setHorizontalAlignment(Element.ALIGN_CENTER);
   cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
   table.addCell(cell);

   cell = new PdfPCell(new Paragraph("内容四", contentFont));
   cell.setFixedHeight(115.0f);
   table.addCell(cell);
   
   document.add(table);
   document.close();
  } catch (Exception e2) {
   System.out.println(e2.getMessage());
  }
 }
}

原创粉丝点击