iText生成PDF文件

iText介紹

  iText是著名的開放源碼的站點sourceforge一個項目,是用於生成PDF文檔的一個java類庫。通過iText不僅可以生成PDF或rtf的文檔,而且可以將XML、Html文件轉化爲PDF文件。

  項目要使用iText,必須引入jar包。才能使用,maven依賴如下:

轉載:http://www.cnblogs.com/h--d/p/6150320.html
1 <dependency>
2     <groupId>com.itextpdf</groupId>
3     <artifactId>itextpdf</artifactId>
4     <version>5.5.10</version>
5 </dependency>

  輸出中文,還要引入下面itext-asian.jar包:

1 <dependency>
2     <groupId>com.itextpdf</groupId>
3     <artifactId>itext-asian</artifactId>
4     <version>5.2.0</version>
5 </dependency>

      設置pdf文件密碼,還要引入下面bcprov-jdk15on.jar包:

1 <dependency>
2     <groupId>org.bouncycastle</groupId>
3     <artifactId>bcprov-jdk15on</artifactId>
4     <version>1.54</version>
5 </dependency>

 

iText常用類

  • com.itextpdf.text.Document:這是iText庫中最常用的類,它代表了一個pdf實例。如果你需要從零開始生成一個PDF文件,你需要使用這個Document類。首先創建(new)該實例,然後打開(open)它,並添加(add)內容,最後關閉(close)該實例,即可生成一個pdf文件。
  • com.itextpdf.text.Paragraph:表示一個縮進的文本段落,在段落中,你可以設置對齊方式,縮進,段落前後間隔等。
  • com.itextpdf.text.Chapter:表示PDF的一個章節,他通過一個Paragraph類型的標題和整形章數創建。
  • com.itextpdf.text.Font:這個類包含了所有規範好的字體,包括family of font,大小,樣式和顏色,所有這些字體都被聲明爲靜態常量。
  • com.itextpdf.text.List:表示一個列表;
  • cocom.itextpdf.text.List:表示一個列表;
  • com.itextpdf.text.Anchor:表示一個錨,類似於HTML頁面的鏈接。
  • com.itextpdf.text.pdf.PdfWriter:當這個PdfWriter被添加到PdfDocument後,所有添加到Document的內容將會寫入到與文件或網絡關聯的輸出流中。
  • com.itextpdf.text.pdf.PdfReader:用於讀取PDF文件;

iText使用

  1. 創建一個簡單的pdf文件,如下:
    複製代碼
     1 package com.hd.pdf;
     2 
     3 import java.io.FileNotFoundException;
     4 import java.io.FileOutputStream;
     5 
     6 import com.itextpdf.text.Document;
     7 import com.itextpdf.text.DocumentException;
     8 import com.itextpdf.text.Paragraph;
     9 import com.itextpdf.text.pdf.PdfWriter;
    10 
    11 public class TestPDFDemo1 {
    12 
    13     public static void main(String[] args) throws FileNotFoundException, DocumentException {
    14 
    15         // 1.新建document對象
    16         Document document = new Document();
    17 
    18         // 2.建立一個書寫器(Writer)與document對象關聯,通過書寫器(Writer)可以將文檔寫入到磁盤中。
    19         // 創建 PdfWriter 對象 第一個參數是對文檔對象的引用,第二個參數是文件的實際名稱,在該名稱中還會給出其輸出路徑。
    20         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/H__D/Desktop/test.pdf"));
    21 
    22         // 3.打開文檔
    23         document.open();
    24         
    25         // 4.添加一個內容段落
    26         document.add(new Paragraph("Hello World!"));
    27 
    28         // 5.關閉文檔
    29         document.close();
    30 
    31     }
    32 
    33 }
    複製代碼

    打開文件

  • 給PDF文件設置文件屬性,例如:

    複製代碼
     1 public static void main(String[] args) throws FileNotFoundException, DocumentException {
     2         
     3         //創建文件
     4         Document document = new Document();
     5         //建立一個書寫器
     6         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/H__D/Desktop/test2.pdf"));
     7         //打開文件
     8         document.open();
     9         //添加內容
    10         document.add(new Paragraph("Some content here"));
    11      
    12         //設置屬性
    13         //標題
    14         document.addTitle("this is a title");
    15         //作者
    16         document.addAuthor("H__D");
    17         //主題
    18         document.addSubject("this is subject");
    19         //關鍵字
    20         document.addKeywords("Keywords");
    21         //創建時間
    22         document.addCreationDate();
    23         //應用程序
    24         document.addCreator("hd.com");
    25         
    26         //關閉文檔
    27         document.close();
    28         //關閉書寫器
    29         writer.close();
    30     }
    複製代碼

    打開文件

  • PDF中添加圖片

    複製代碼
     1 public static void main(String[] args) throws DocumentException, IOException {
     2         
     3         //創建文件
     4         Document document = new Document();
     5         //建立一個書寫器
     6         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/H__D/Desktop/test3.pdf"));
     7         //打開文件
     8         document.open();
     9         //添加內容
    10         document.add(new Paragraph("HD content here"));
    11      
    12         //圖片1
    13         Image image1 = Image.getInstance("C:/Users/H__D/Desktop/IMG_0109.JPG");
    14         //設置圖片位置的x軸和y周
    15         image1.setAbsolutePosition(100f, 550f);
    16         //設置圖片的寬度和高度
    17         image1.scaleAbsolute(200, 200);
    18         //將圖片1添加到pdf文件中
    19         document.add(image1);
    20      
    21         //圖片2
    22         Image image2 = Image.getInstance(new URL("http://static.cnblogs.com/images/adminlogo.gif"));
    23         //將圖片2添加到pdf文件中
    24         document.add(image2);
    25         
    26         //關閉文檔
    27         document.close();
    28         //關閉書寫器
    29         writer.close();
    30     }
    複製代碼

    打開文件
     

  • PDF中創建表格
     

    複製代碼
     1 public static void main(String[] args) throws DocumentException, FileNotFoundException {
     2         //創建文件
     3         Document document = new Document();
     4         //建立一個書寫器
     5         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/H__D/Desktop/test4.pdf"));
     6         //打開文件
     7         document.open();
     8         //添加內容
     9         document.add(new Paragraph("HD content here"));
    10      
    11         // 3列的表.
    12         PdfPTable table = new PdfPTable(3); 
    13         table.setWidthPercentage(100); // 寬度100%填充
    14         table.setSpacingBefore(10f); // 前間距
    15         table.setSpacingAfter(10f); // 後間距
    16 
    17         List<PdfPRow> listRow = table.getRows();
    18         //設置列寬
    19         float[] columnWidths = { 1f, 2f, 3f };
    20         table.setWidths(columnWidths);
    21         
    22         //行1
    23         PdfPCell cells1[]= new PdfPCell[3];
    24         PdfPRow row1 = new PdfPRow(cells1);
    25        
    26         //單元格
    27         cells1[0] = new PdfPCell(new Paragraph("111"));//單元格內容
    28         cells1[0].setBorderColor(BaseColor.BLUE);//邊框驗證
    29         cells1[0].setPaddingLeft(20);//左填充20
    30         cells1[0].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
    31         cells1[0].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
    32 
    33         cells1[1] = new PdfPCell(new Paragraph("222"));
    34         cells1[2] = new PdfPCell(new Paragraph("333"));
    35         
    36         //行2
    37         PdfPCell cells2[]= new PdfPCell[3];
    38         PdfPRow row2 = new PdfPRow(cells2);
    39         cells2[0] = new PdfPCell(new Paragraph("444"));
    40 
    41         //把第一行添加到集合
    42         listRow.add(row1);
    43         listRow.add(row2);
    44         //把表格添加到文件中
    45         document.add(table);
    46         
    47         //關閉文檔
    48         document.close();
    49         //關閉書寫器
    50         writer.close();
    51     }
    複製代碼

    打開文件

     

  •  PDF中創建列表

    複製代碼
     1 public static void main(String[] args) throws DocumentException, FileNotFoundException {
     2         //創建文件
     3         Document document = new Document();
     4         //建立一個書寫器
     5         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/H__D/Desktop/test5.pdf"));
     6         //打開文件
     7         document.open();
     8         //添加內容
     9         document.add(new Paragraph("HD content here"));
    10      
    11         //添加有序列表
    12         List orderedList = new List(List.ORDERED);
    13         orderedList.add(new ListItem("Item one"));
    14         orderedList.add(new ListItem("Item two"));
    15         orderedList.add(new ListItem("Item three"));
    16         document.add(orderedList);
    17 
    18         //關閉文檔
    19         document.close();
    20         //關閉書寫器
    21         writer.close();
    22     }
    複製代碼

    打開文件

     

  •  

    PDF中設置樣式/格式化輸出,輸出中文內容,必須引入itext-asian.jar

    複製代碼
     1 public static void main(String[] args) throws DocumentException, IOException {
     2         //創建文件
     3         Document document = new Document();
     4         //建立一個書寫器
     5         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/H__D/Desktop/test6.pdf"));
     6         //打開文件
     7         document.open();
     8         
     9         //中文字體,解決中文不能顯示問題
    10         BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
    11         
    12         //藍色字體
    13         Font blueFont = new Font(bfChinese);
    14         blueFont.setColor(BaseColor.BLUE);
    15         //段落文本
    16         Paragraph paragraphBlue = new Paragraph("paragraphOne blue front", blueFont);
    17         document.add(paragraphBlue);
    18      
    19         //綠色字體
    20         Font greenFont = new Font(bfChinese);
    21         greenFont.setColor(BaseColor.GREEN);
    22         //創建章節
    23         Paragraph chapterTitle = new Paragraph("段落標題xxxx", greenFont);
    24         Chapter chapter1 = new Chapter(chapterTitle, 1);
    25         chapter1.setNumberDepth(0);
    26        
    27         Paragraph sectionTitle = new Paragraph("部分標題", greenFont);
    28         Section section1 = chapter1.addSection(sectionTitle);
    29      
    30         Paragraph sectionContent = new Paragraph("部分內容", blueFont);
    31         section1.add(sectionContent);
    32         
    33         //將章節添加到文章中
    34         document.add(chapter1);
    35         
    36         //關閉文檔
    37         document.close();
    38         //關閉書寫器
    39         writer.close();
    40     }
    複製代碼

    打開文件

     

  •  給PDF文件設置密碼,需要引入bcprov-jdk15on.jar包:

    複製代碼
     1 public static void main(String[] args) throws DocumentException, IOException {
     2         // 創建文件
     3         Document document = new Document();
     4         // 建立一個書寫器
     5         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/H__D/Desktop/test8.pdf"));
     6 
     7         //用戶密碼
     8         String userPassword = "123456";
     9         //擁有者密碼
    10         String ownerPassword = "hd";
    11         writer.setEncryption(userPassword.getBytes(), ownerPassword.getBytes(), PdfWriter.ALLOW_PRINTING,
    12                 PdfWriter.ENCRYPTION_AES_128);
    13 
    14         // 打開文件
    15         document.open();
    16         
    17         //添加內容
    18         document.add(new Paragraph("password !!!!"));
    19 
    20         // 關閉文檔
    21         document.close();
    22         // 關閉書寫器
    23         writer.close();
    24     }
    複製代碼

    打開文件

  • 給PDF文件設置權限

    複製代碼
     1 public static void main(String[] args) throws DocumentException, IOException {
     2         // 創建文件
     3         Document document = new Document();
     4         // 建立一個書寫器
     5         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/H__D/Desktop/test9.pdf"));
     6 
     7         // 只讀權限
     8         writer.setEncryption("".getBytes(), "".getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
     9 
    10         // 打開文件
    11         document.open();
    12 
    13         // 添加內容
    14         document.add(new Paragraph("password !!!!"));
    15 
    16         // 關閉文檔
    17         document.close();
    18         // 關閉書寫器
    19         writer.close();
    20     }
    複製代碼

     

  • 讀取/修改已有的PDF文件

    複製代碼
     1 public static void main(String[] args) throws DocumentException, IOException {
     2         
     3         //讀取pdf文件
     4         PdfReader pdfReader = new PdfReader("C:/Users/H__D/Desktop/test1.pdf");
     5      
     6         //修改器
     7         PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream("C:/Users/H__D/Desktop/test10.pdf"));
     8      
     9         Image image = Image.getInstance("C:/Users/H__D/Desktop/IMG_0109.JPG");
    10         image.scaleAbsolute(50, 50);
    11         image.setAbsolutePosition(0, 700);
    12      
    13         for(int i=1; i<= pdfReader.getNumberOfPages(); i++)
    14         {
    15             PdfContentByte content = pdfStamper.getUnderContent(i);
    16             content.addImage(image);
    17         }
    18      
    19         pdfStamper.close();
    20     }
    複製代碼

    打開文件

     




     

     


     


  •   

     

    發表評論
    所有評論
    還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
    相關文章