1. 程式人生 > 其它 >[itext] java生成pdf

[itext] java生成pdf

iText簡介
iText是著名的開放原始碼的站點sourceforge一個專案,是用於生成PDF文件的一個java類庫。通過iText不僅可以生成PDF或rtf的文件,而且可以將XML、Html檔案轉化為PDF檔案。
iText的安裝非常方便,可以在maven中央倉庫找到所需要的版本,只需要pom.xml檔案新增依賴即可。

使用itext生成pdf,所需jar包

  <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.5.10</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext-asian</artifactId>
        <version>5.2.0</version>
    </dependency>

1
2
3
4
5
6
7
8
9
10
附一個完整的將資料寫到pdf原始碼

package com.ruoyi.test;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**

  • TODO

  • @author linfeng

  • @date 2021/1/6 14:29
    */
    public class PDFTest {

    private static Font headFont;// 設定字型大小
    private static Font textFont;// 設定字型大小
    private static Font paperNameFont;//試卷名稱
    static {
    BaseFont bfChinese;
    try {
    bfChinese = BaseFont.createFont(“STSong-Light”, “UniGB-UCS2-H”, BaseFont.NOT_EMBEDDED);

    headFont = new Font(bfChinese, 10, Font.BOLD);// 設定字型大小
    textFont = new Font(bfChinese, 8, Font.BOLD);// 設定字型大小
    paperNameFont = new Font(bfChinese, 18, Font.BOLD);// 設定字型大小
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    public static void main(String[] args) throws FileNotFoundException, DocumentException {
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    //pdf檔案儲存路徑
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(“F:/test.pdf”));
    document.open();
    Paragraph paragraph = new Paragraph(“成都職業技術學院2019 2020 學年度下期期末考試”,headFont);
    // 設定文字居中 0靠左 1,居中 2,靠右
    paragraph.setAlignment(1);
    document.add(paragraph);
    //新增換行符
    document.add(new Paragraph("\n"));
    Paragraph paperName = new Paragraph(“2021英語期末考試”,paperNameFont);
    paperName.setAlignment(1);
    document.add(paperName);
    document.add(new Paragraph("\n"));
    Paragraph propositionPeople = new Paragraph(“命題人:林鋒 稽核人:陳從亮”,headFont);
    propositionPeople.setAlignment(1);
    document.add(propositionPeople);
    document.add(new Paragraph("\n"));
    PdfPTable table = createTable(8);
    table.addCell(createCell(“題號”,headFont,Element.AALIGN_CENTER));
    table.addCell(createCell(“一”,headFont,Element.ALIGN_CENTER));
    table.addCell(createCell(“二”,headFont,Element.ALIGN_CENTER));
    table.addCell(createCell(“三”,headFont,Element.ALIGN_CENTER));
    table.addCell(createCell(“四”,headFont,Element.ALIGN_CENTER));
    table.addCell(createCell(“五”,headFont,Element.ALIGN_CENTER));
    table.addCell(createCell(“六”,headFont,Element.ALIGN_CENTER));
    table.addCell(createCell(“總分”,headFont,Element.ALIGN_CENTER));
    document.add(table);
    PdfPTable table2 = createTable(8);
    table2.addCell(createCell(“得分”,headFont,Element.ALIGN_CENTER));
    table2.addCell(createCell("",headFont,Element.ALIGN_CENTER));
    table2.addCell(createCell("",headFont,Element.ALIGN_CENTER));
    table2.addCell(createCell("",headFont,Element.ALIGN_CENTER));
    table2.addCell(createCell("",headFont,Element.ALIGN_CENTER));
    table2.addCell(createCell("",headFont,Element.ALIGN_CENTER));
    table2.addCell(createCell("",headFont,Element.ALIGN_CENTER));
    table2.addCell(createCell("",headFont,Element.ALIGN_CENTER));
    document.add(table2);
    document.add(new Paragraph("\n"));
    document.add(new Paragraph("\n"));
    document.add(new Paragraph("\n"));
    document.add(new Paragraph(“一、單選題”,headFont));
    document.add(new Paragraph("\n"));
    //題的所有資訊
    document.add(new Paragraph(“1.【網際網路環境下的典型營銷組合】杜爾於2011年提出移動互聯營銷SoLoMo模式,這一營銷組合重點突出哪些方面?”,textFont));
    document.add(new Paragraph(“A:社交”,textFont));
    document.add(new Paragraph(“B:本地化或精準化”,textFont));
    document.add(new Paragraph(“C:移動”,textFont));
    document.add(new Paragraph(“D:渠道”,textFont));
    document.close();
    writer.close();
    }

    public static PdfPTable createTable(int colNumber){
     PdfPTable table = new PdfPTable(colNumber);
     try{
         table.setTotalWidth(520);
         table.setLockedWidth(true);
         table.setHorizontalAlignment(Element.ALIGN_CENTER);
         table.getDefaultCell().setBorder(1);
     }catch(Exception e){
         e.printStackTrace();
     }
     return table;
    

    }

    public static PdfPCell createCell(String value, Font font, int align){
    PdfPCell cell = new PdfPCell();
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setHorizontalAlignment(align);
    cell.setPhrase(new Phrase(value,font));
    return cell;
    }
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
檔案已生成

在這裡插入圖片描述
效果圖
在這裡插入圖片描述