[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
文件已生成

在这里插入图片描述
效果图
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值