zoukankan      html  css  js  c++  java
  • itextpd f生成 pdf 文件

    一、简介
      itextpdf 是一个开源的允许你去创建和操作PDF文档的库。它使的开发者可以提高web和其他应用来动态地生成或操作PDF文档。通过iText 中的Document和PdfWriter类,我们可以以数据库、xml文件、或者其他数据源为源头创建PDF文档。有如下三种方式:

    • 使用高级别的对象如Chunk,Phrase,Paragraph,List等待。这些对象通常被称为iText的基本构建块。
    • 使用低级别的功能。使用pdfContentByte,由一系列的类映射到每一个操作符的方法,操作数可以在Adobe的成像模型。这个类也有很多便利的方法来画弧线,圆形、矩形和文本在绝对位置。
    • 使用Java版本的iText的抽象类Graphics2D的实现类PdfGraphics2D(在iTextSharp中不可用)。

    iText附带大量的类,支持不同的图像类型,颜色空间、字体、便携式集合。有功能的提高可访问性的PDF文件,以确保PDF是可用的、等等。itextpdf的官网如下:

    http://www.itextpdf.com/

    二、示例
      1、相关依赖如下:

    <!-- pdf -->
    <dependency>
         <groupId>com.itextpdf</groupId>
         <artifactId>itextpdf</artifactId>
         <version>5.5.6</version>
         <type>jar</type>
       </dependency>
    
       <dependency>
    <groupId>com.itextpdf.tool</groupId>
        <artifactId>xmlworker</artifactId>
        <version>5.5.6</version>
    </dependency>
    <!-- 中文支持 -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext-asian</artifactId>
        <version>5.2.0</version>
    </dependency>

      2、示例

      1 package com.test.excel;
      2 
      3 import java.io.FileNotFoundException;
      4 import java.io.FileOutputStream;
      5 import java.io.IOException;
      6 
      7 import org.junit.Test;
      8 
      9 import com.itextpdf.text.BaseColor;
     10 import com.itextpdf.text.Chunk;
     11 import com.itextpdf.text.Document;
     12 import com.itextpdf.text.DocumentException;
     13 import com.itextpdf.text.Element;
     14 import com.itextpdf.text.Font;
     15 import com.itextpdf.text.PageSize;
     16 import com.itextpdf.text.Paragraph;
     17 import com.itextpdf.text.Phrase;
     18 import com.itextpdf.text.Rectangle;
     19 import com.itextpdf.text.pdf.BaseFont;
     20 import com.itextpdf.text.pdf.PdfPTable;
     21 import com.itextpdf.text.pdf.PdfWriter;
     22 
     23 /**
     24  * 创建PDF
     25  */
     26 public class PDFCreate {
     27 
     28     /**
     29      * 创建PDF
     30      */
     31     @Test
     32     public void createPDF() {
     33         // 页面大小
     34         Rectangle rect = new Rectangle(PageSize.B5.rotate());
     35         // 页面背景色
     36         rect.setBackgroundColor(BaseColor.WHITE);
     37         // 创建一个文档
     38         Document document = new Document(rect);
     39         try {
     40             // 获取PdfWriter实例
     41             PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("c:/test/testPdF.pdf"));
     42 
     43             // PDF版本(默认1.4)
     44             writer.setPdfVersion(PdfWriter.PDF_VERSION_1_7);
     45 
     46             // 文档属性
     47             document.addTitle("测试文档标题");
     48             document.addAuthor("张三");
     49             document.addSubject("PDF测试");
     50             document.addKeywords("pdf");
     51             document.addCreator("张三");
     52 
     53             // 页边空白
     54             document.setMargins(10, 20, 30, 40);
     55 
     56             // 解决中文支持问题
     57             BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
     58             com.itextpdf.text.Font fontChinese = new com.itextpdf.text.Font(bfChinese, 12, com.itextpdf.text.Font.NORMAL);
     59 
     60             // 标题
     61             Paragraph toptile = new Paragraph("标题", new Font(bfChinese, 14, Font.NORMAL));
     62             toptile.setAlignment(Paragraph.ALIGN_CENTER);
     63             toptile.add(new Paragraph("   "));
     64 
     65             
     66             Paragraph pragraph = new Paragraph("这是一个段落,这是一个段落,这是一个段落,这是一个段落,这是一个段落,这是一个段落,这是一个段落,这是一个段落,这是一个段落,这是一个段落,这是一个段落", fontChinese);
     67             pragraph.setAlignment(Element.ALIGN_LEFT);
     68             pragraph.add(new Paragraph("   "));
     69 
     70         
     71 
     72             // 添加表格
     73             PdfPTable table = new PdfPTable(4);
     74             String content = "你好 : ";
     75             for (int i = 0; i < 200; i++)
     76                 table.addCell(new Phrase(new Chunk(content + i, fontChinese)));
     77 
     78             // 使用 HttpServletResponse 进行下载
     79 //            response.setContentType("application/pdf; charset=UTF-8");
     80 //            response.setHeader("Content-Disposition", "inline; filename=" + new String("test.pdf".getBytes(), "UTF-8"));
     81 //            PdfWriter.getInstance(document, response.getOutputStream());
     82             
     83             // 打开文档
     84             document.open();
     85             
     86             // 添加内容
     87             document.add(toptile);
     88             document.add(pragraph);
     89             document.add(table);
     90             // 关闭文档
     91             document.close();
     92         } catch (FileNotFoundException e) {
     93             e.printStackTrace();
     94         } catch (DocumentException e) {
     95             e.printStackTrace();
     96         } catch (IOException e) {
     97             e.printStackTrace();
     98         }
     99     }
    100 }
  • 相关阅读:
    [报错]编译报错:clang: error: linker command failed with exit code 1及duplicate symbol xxxx in错误解决方法之一
    修改UISearBar的文字颜色,placehoder颜色及输入框颜色
    designated initializer和secondary initializer是什么?
    设置UIButton的文字居右显示 去掉点击默认置灰效果
    设置UITextField的placeholder的颜色
    Xcode8和iOS10的适配问题
    [转载]做一个 App 前需要考虑的几件事
    git 常用命令行整理
    Xcode中使用GitHub详解
    截取字符串
  • 原文地址:https://www.cnblogs.com/always-online/p/4800875.html
Copyright © 2011-2022 走看看