Can display Chinese pdftable

package com.wondersgroup.bc.pm.service;
import java.io.FileOutputStream;

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.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class zwppdf {
public static void main(String[] args) {

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

try {

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

   PdfPTable table = new PdfPTable(8);

   PdfWriter.getInstance(docu, fos);

   docu.open();

   // Content font

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

   "UniGB-UCS2-H", BaseFont .NOT_EMBEDDED);//Support Chinese

   Font font = new Font(bfComic, 12, Font.NORMAL);//Main title

   Font font2 = new Font(bfComic, 10, Font.NORMAL);//Subtitle



   // C( internal table)

   PdfPTable begTable = new PdfPTable(2);

   PdfPCell begCell = new PdfPCell(new Paragraph("Opening Balance", font));

   begCell.setHorizontalAlignment(Element.ALIGN_CENTER);

   begCell.setColspan(2);

   begTable.addCell (begCell);

   // D(internal table)

   PdfPTable yTable = new PdfPTable(2);

   PdfPCell yCell = new PdfPCell(new Paragraph("cumulative balance", font));

   yCell.setHorizontalAlignment(Element.ALIGN_CENTER);

   yCell.setColspan(2);

   yTable.addCell(yCell);

   // E(internal table)

   PdfPTable endTable = new PdfPTable(2);

   PdfPCell endCell = new PdfPCell(new Paragraph("Ending Balance", font));

   endCell.setHorizontalAlignment(Element.ALIGN_CENTER);

   endCell.setColspan(2);

   endTable.addCell(endCell);

   // The header of the first line

   PdfPCell acc = new PdfPCell(new Paragraph("Account Code", font));

   acc.setRowspan(2);//Merge lines

   //Center both horizontally and vertically

   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();

}

    }


}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326944357&siteId=291194637
Recommended