`
ssydxa219
  • 浏览: 609622 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

itext cell row span

 
阅读更多

public static void main(String[] args) {

Document docu = new Document(PageSize.A4, 5, 5, 10, 5);

try {

   FileOutputStream fos = new FileOutputStream("f://a.pdf");

   PdfPTable table = new PdfPTable(8);

   PdfWriter.getInstance(docu, fos);

   docu.open();

   // 内容字体

   BaseFont bfComic = BaseFont.createFont("STSong-Light",

   "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);//支持中文

   Font font = new Font(bfComic, 12, Font.NORMAL);//大标题

   Font font2 = new Font(bfComic, 10, Font.NORMAL);//小标题

 

   // C(内部的table)

   PdfPTable begTable = new PdfPTable(2);

   PdfPCell begCell = new PdfPCell(new Paragraph("期初余额", font));

   begCell.setHorizontalAlignment(Element.ALIGN_CENTER);

   begCell.setColspan(2);

   begTable.addCell(begCell);

   // D(内部的table)

   PdfPTable yTable = new PdfPTable(2);

   PdfPCell yCell = new PdfPCell(new Paragraph("累计发生额", font));

   yCell.setHorizontalAlignment(Element.ALIGN_CENTER);

   yCell.setColspan(2);

   yTable.addCell(yCell);

   // E(内部的table)

   PdfPTable endTable = new PdfPTable(2);

   PdfPCell endCell = new PdfPCell(new Paragraph("期末余额", font));

   endCell.setHorizontalAlignment(Element.ALIGN_CENTER);

   endCell.setColspan(2);

   endTable.addCell(endCell);

   // 第一行抬头

   PdfPCell acc = new PdfPCell(new Paragraph("科目代码", font));

   acc.setRowspan(2);//合并行

   //水平垂直都居中

   acc.setHorizontalAlignment(Element.ALIGN_CENTER);

   acc.setVerticalAlignment(Element.ALIGN_MIDDLE);

   

   PdfPCell name = new PdfPCell(new Paragraph("科目名称", font));

   name.setRowspan(2);

   //水平垂直都居中

   name.setHorizontalAlignment(Element.ALIGN_CENTER);

   name.setVerticalAlignment(Element.ALIGN_MIDDLE);

   //C列

   PdfPCell cell2 = new PdfPCell(begTable);

   cell2.setHorizontalAlignment(Element.ALIGN_CENTER);

   cell2.setColspan(2);

   //D列

   PdfPCell cell3 = new PdfPCell(yTable);

   cell3.setHorizontalAlignment(Element.ALIGN_CENTER);

   cell3.setColspan(2);

   //E列

   PdfPCell cell4 = new PdfPCell(endTable);

   cell4.setHorizontalAlignment(Element.ALIGN_CENTER);

   cell4.setColspan(2);

   

   

   table.addCell(acc);//A

   table.addCell(name);//B

   table.addCell(cell2);

   table.addCell(cell3);

   table.addCell(cell4);

   //第二行标题

   table.addCell(new Paragraph("C1", font2));

   table.addCell(new Paragraph("C2", font2));

   table.addCell(new Paragraph("D1", font2));

   table.addCell(new Paragraph("D2", font2));

   table.addCell(new Paragraph("E1", font2));

   table.addCell(new Paragraph("E2", font2));

 

   docu.add(table);

   docu.close();

} catch (Exception e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

}

    }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics