用itext生成带表格的PDF,并显示中文问题

本文介绍如何利用iText库生成包含表格的PDF文件,并解决在PDF中显示中文的问题。首先需要引入iText及其中文支持包,然后创建Document对象、设置页面大小,通过PdfWriter实例化文件。接着,定义字体、设置表格列宽,创建PdfPTable对象,添加单元格内容,并确保中文能正常显示。最后,将表格添加到文档并关闭Document。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        使用itext可以很容易实现PDF的生成,其中itext包中提供了很多的API,可以操作pdf,在这里我先用一个简单的例子介绍一下用itext生成PDF的方法,下面的代码是我抽空随便写了一个:

程序调试前,必须先引用两个包,一个是itext的jar包:itext-2.0.4.jar,另一个是中文支持包:iTextAsian.jar

import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
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 CreatPDF {
 
 private String mPdfPath;
 
 private CreatPDF(){
  
 }
 public void setPdfPath(String pdfPath)
 {
  mPdfPath=pdfPath;
 }
 /**
  * 生成PDF的方法
  * @return boolean
  */
 public boolean createPDF(){
    Document document = new Document();//建立一个Document对象
    document.setPageSize(PageSize.A4);//设置页面大小
    try{
     PdfWriter.getInstance(document,new FileOutputStream(mPdfPath+"taony125-test.pdf"));//建立一个PdfWriter对象
     document.open(); 
     BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//设置中文字体
      Font headFont = new Font(bfChinese, 10, Font.BOLD);//设置字体大小
      Font headFont1 = new Font(bfChinese, 8, Font.BOLD);//设置字体大小
      Font headFont2 = new Font(bfChinese, 10, Font.NORMAL);//设置字体大小
      float[] widths = {140f, 60f, 320f, 120f, 110f, 110f, 190f};//设置表格的列宽
      PdfPTable table = new PdfPTable(widths);//建立一个pdf表格
     table.setSpacingBefore(130f);//设置表格上面空白宽度
   table.setTotalWidth(535);//设置表格的宽度            
   table.setLockedWidth(true);//设置表格的宽度固定
   //table.getDefaultCell().setBorder(0);//设置表格默认为无边框
   PdfPCell cell = new PdfPCell(new Paragraph("Taony125 testPdf 中文字体",headFont));//建立一个单元格
   //cell.setBorder(0);//设置单元格无边框
   cell.setColspan(7);//设置合并单元格的列数
   table.addCell(cell);//增加单元格
   cell = new PdfPCell(new Paragraph("Taony125 testPdf 中文字体",headFont));
      //cell.setBorder(0);
   cell.setFixedHeight(20);
   cell.setColspan(7);//设置合并单元格的列数
   cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置内容水平居中显示
   cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
   table.addCell(cell);
   cell = new PdfPCell(new Paragraph("Taony125 testPdf 中文字体",headFont1));
      //cell.setBorder(0);
   cell.setFixedHeight(20);
   cell.setColspan(7);//设置合并单元格的列数
   cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置内容水平居中显示
   cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
   table.addCell(cell);
   table.addCell(new Paragraph("Taony125 testPdf 中文字体",headFont2));
   document.add(table);

     } catch (DocumentException de) {
     System.err.println(de.getMessage());
            return false;
     }
     catch (IOException ioe) {
     System.err.println(ioe.getMessage());
            return false;
      }
     document.close();
  return true;
 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO 自动生成方法存根
  CreatPDF mCreatPDF=new CreatPDF();
  mCreatPDF.setPdfPath("D:/");
  mCreatPDF.createPDF();

 }

}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值