打印系列 —— Java iText 动态 生成 PDF 文档 表格 中文问题

 

Java iText 动态 生成 PDF 文档表格中文问题

iText是一个开发源代码的项目,你可以使用iText方便的实现PDF的输出。
 
 
一、iText的下载:

    你可以在 http://www.lowagie.com/iText/ 查看关于iText的相关信息,包括源代码,文档..

    1. itext-src-1.4.zip(源代码)
        
http://jaist.dl.sourceforge.net/sourceforge/itext/itext-src-1.4.zip

    2. itext-1.4.jar(可以直接导入的jar文件)

        http://jaist.dl.sourceforge.net/sourceforge/itext/itext-1.4.jar

    3. 亚洲语言包

        http://itextdocs.lowagie.com/downloads/iTextAsian.jar          (或者)

        http://itextdocs.lowagie.com/downloads/iTextAsianCmaps.jar

二、示例程序:

        首先把上面的这几个jar包的完整路径添加到环境变量 classpath 中,然后再下面的程序
        中导入相应的包

  1. /**
  2.   * HelloWorld.java
  3.  */
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import com.lowagie.text.*;
  7. import com.lowagie.text.pdf.PdfWriter;
  8. public class HelloWorld {
  9.   public static void main(String[] args) {
  10.     System.out.println("Hello World");
  11.     // 创建一个Document对象
  12.     Document document = new Document();
  13.     try
  14.     {
  15.       // 生成名为 HelloWorld.pdf 的文档
  16.       PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));
  17.       // 添加PDF文档的一些信息
  18.       document.addTitle("Hello World example");
  19.       document.addAuthor("Bruno Lowagie");
  20.       document.addSubject("This example explains how to add metadata.");
  21.       document.addKeywords("iText, Hello World, step 3, metadata");
  22.       document.addCreator("My program using iText");
  23.       // 打开文档,将要写入内容
  24.       document.open();
  25.       // 插入一个段落
  26.       document.add(new Paragraph("Hello World!"));
  27.     } 
  28.     catch (DocumentException de)
  29.     {
  30.       System.err.println(de.getMessage());
  31.     }
  32.     catch (IOException ioe)
  33.     {
  34.       System.err.println(ioe.getMessage());
  35.     }
  36.     // 关闭打开的文档
  37.     document.close();
  38.   }

编译运行以后,我们可以在运行的目录发现生成的HelloWorld.pdf,打开可以看到我们写的文字:

Hello World



三、中文问题:

        由于iText不支持东亚语言,我们下载了iTextAsian.jar 以后,就可以在PDF里面写中文:

  1. /**
  2.   * AsianTest.java
  3.  */
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import com.lowagie.text.*;
  7. import com.lowagie.text.pdf.PdfWriter;
  8. import com.lowagie.text.pdf.BaseFont;
  9. import com.lowagie.text.Font;
  10. import java.awt.Color;
  11. public class AsianTest{
  12.   public static void main(String[] args) {
  13.     // 创建一个Document对象
  14.     Document document = new Document();
  15.     try
  16.     {
  17.       // 生成名为 AsianTest.pdf 的文档
  18.       PdfWriter.getInstance(document, new FileOutputStream("AsianTest.pdf"));
  19.      /**  新建一个字体,iText的方法
  20.        *  STSongStd-Light 是字体,在iTextAsian.jar 中以property为后缀
  21.        *  UniGB-UCS2-H   是编码,在iTextAsian.jar 中以cmap为后缀
  22.        *  H 代表文字版式是 横版, 相应的 V 代表竖版
  23.       */
  24.       BaseFont bfChinese = BaseFont.createFont("STSongStd-Light""UniGB-UCS2-H"false);
  25.         Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.GREEN);
  26.       // 打开文档,将要写入内容
  27.       document.open();
  28.       // 插入一个段落
  29.       Paragraph par = new Paragraph("我们",fontChinese);
  30.       document.add(par);
  31.     } 
  32.     catch (DocumentException de)
  33.     {
  34.       System.err.println(de.getMessage());
  35.     }
  36.     catch (IOException ioe)
  37.     {
  38.       System.err.println(ioe.getMessage());
  39.     }
  40.     // 关闭打开的文档
  41.     document.close();
  42.   }
  43. }

就可以显示中文了。

四、其他问题:(应导入相应的包)

       1. 换页:

  1. document.newPage();

       2. 表格:

  1.     // 设置 Table
  2.     Table aTable = new Table(3);
  3.     int width[] = {25,25,50};
  4.     aTable.setWidths(width);
  5.     aTable.setWidth(80); // 占页面宽度 80%
  6.     aTable.setDefaultHorizontalAlignment(Element.ALIGN_LEFT);
  7.     aTable.setDefaultVerticalAlignment(Element.ALIGN_MIDDLE);
  8.     aTable.setAutoFillEmptyCells(true); //自动填满
  9.     aTable.setPadding(1);
  10.     aTable.setSpacing(1);
  11.     aTable.setDefaultCellBorder(0);
  12.     aTable.setBorder(0);
  13.     Cell cell = new Cell(new Phrase("这是一个测试的 3*3 Table 数据", fontChinese ));
  14.     cell.setVerticalAlignment(Element.ALIGN_TOP);
  15.     cell.setRowspan(3);
  16.     aTable.addCell(cell);
  17.     aTable.addCell(new Cell("#1"));
  18.     aTable.addCell(new Cell("#2"));
  19.     aTable.addCell(new Cell("#3"));
  20.     aTable.addCell(new Cell("#4"));
  21.     aTable.addCell(new Cell("#5"));
  22.     aTable.addCell(new Cell("#6"));
  23.     document.add(aTable);

       3. 图片:

  1.     // 可以是绝对路径,也可以是URL
  2.     Image img = Image.getInstance("logo.gif");
  3.     // Image image = Image.getInstance(new URL(http://xxx.com/logo.jpg));
  4.     img.setAbsolutePosition(00);
  5.     document.add(img);

五、参考文档:

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java iText是一个用于生成PDF文件的开源库。它提供了丰富的API,可以通过编程方式创建、编辑和操作PDF文档使用iText生成PDF文件的基本步骤如下: 1. 导入iText库:首先需要在Java项目导入iText库的相关jar文件。 2. 创建文档对象:使用`Document`类创建一个PDF文档对象。 3. 创建写入器对象:使用`PdfWriter`类创建一个写入器对象,将文档对象与输出流关联起来。 4. 打开文档使用`document.open()`方法打开文档。 5. 添加内容:通过调用文档对象的方法,如`document.add()`或`document.addParagraph()`,向文档添加内容,如文本、表格、图片等。 6. 关闭文档使用`document.close()`方法关闭文档,确保所有的内容都已经写入到PDF文件。 7. 保存PDF文件:将文档对象写入到输出流,保存为PDF文件。 以下是一个简单的示例代码,演示了如何使用iText动态生成PDF文件: ```java import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileOutputStream; import java.io.FileNotFoundException; public class PdfGenerator { public static void main(String[] args) { // 创建文档对象 Document document = new Document(); try { // 创建写入器对象,并将文档对象与输出流关联起来 PdfWriter.getInstance(document, new FileOutputStream("output.pdf")); // 打开文档 document.open(); // 添加内容 document.add(new Paragraph("Hello, World!")); // 关闭文档 document.close(); System.out.println("PDF文件生成成功!"); } catch (DocumentException | FileNotFoundException e) { e.printStackTrace(); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值