iText 5 通过创建 Document 对象,并使用 PdfWriter 将内容写入 PDF 文件

在 iText 5 中,你可以通过创建 Document 对象,并使用 PdfWriter 将内容写入 PDF 文件。以下是一个简单的例子,展示了如何根据样式填充数据生成 PDF 文件:

步骤 1: 添加 iText 5 依赖

首先,确保你的 Maven pom.xml 文件中包含了 iText 5 的依赖。

XML 复制代码
<!-- Maven pom.xml -->
<dependencies>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.5.13.1</version>
    </dependency>
</dependencies>

步骤 2: 创建 PDF 文件并添加样式

创建一个 Java 类,使用 iText 5 的 API 来生成 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.FileNotFoundException;
import java.io.FileOutputStream;

public class CreatePdfWithStyle {
    public static void main(String[] args) {
        // 定义输出 PDF 文件的路径
        String dest = "output.pdf";

        // 创建 Document 对象
        Document document = new Document();

        try {
            // 创建 PdfWriter 实例
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
            // 打开文档
            document.open();

            // 添加标题
            Paragraph title = new Paragraph("Hello, iText 5!");
            title.setAlignment(Paragraph.ALIGN_CENTER);
            title.setFont(FontFactory.getFont(FontFactory.HELVETICA, 24, Font.BOLD, BaseColor.BLUE));
            document.add(title);

            // 添加子标题
            Paragraph subtitle = new Paragraph("This is a subtitle.");
            subtitle.setAlignment(Paragraph.ALIGN_CENTER);
            subtitle.setFont(FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLD, BaseColor.RED));
            document.add(subtitle);

            // 添加普通文本
            Paragraph text = new Paragraph("This is some normal text.");
            text.setAlignment(Paragraph.ALIGN_JUSTIFIED);
            text.setFont(FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.BLACK));
            document.add(text);

            // 添加换行
            document.add(Chunk.NEWLINE);

            // 添加列表
            List list = new List();
            list.add("Item 1");
            list.add("Item 2");
            list.add("Item 3");
            document.add(list);

            // 关闭文档
            document.close();
            System.out.println("PDF created successfully.");
        } catch (DocumentException | FileNotFoundException e) {
            e.printStackTrace();
            System.err.println("Error while creating the PDF document.");
        }
    }
}

步骤 3: 运行程序

运行上面的程序,它将在指定的路径创建一个名为 output.pdf 的文件,其中包含具有不同样式的标题、子标题、文本和列表。

注意事项

  1. 字体加载 :iText 5 默认使用基础字体,如果需要使用其他字体,可能需要加载字体文件。例如,使用 FontFactory.getFont() 方法加载字体。

  2. 异常处理 :在实际应用中,应适当处理异常,例如文档异常(DocumentException)和文件未找到异常(FileNotFoundException)。

  3. 样式设置 :可以通过 ParagraphFontBaseColor 类来设置文本的样式,包括字体、大小、颜色和对齐方式。

  4. 元素添加:除了文本段落和列表,iText 5 还支持添加图像、表格和其他元素。

  5. PDF 阅读器:生成的 PDF 文件可以用任何 PDF 阅读器查看,如 Adobe Acrobat Reader。

通过上述步骤,你可以使用 iText 5 在 Java 应用程序中根据样式填充数据生成 PDF 文件。根据需要,你可以添加更复杂的内容和样式。

相关推荐
SimonKing4 小时前
OpenCode AI辅助编程,不一样的编程思路,不写一行代码
java·后端·程序员
FastBean4 小时前
Jackson View Extension Spring Boot Starter
java·后端
Seven975 小时前
剑指offer-79、最⻓不含重复字符的⼦字符串
java
皮皮林55114 小时前
Java性能调优黑科技!1行代码实现毫秒级耗时追踪,效率飙升300%!
java
冰_河15 小时前
QPS从300到3100:我靠一行代码让接口性能暴涨10倍,系统性能原地起飞!!
java·后端·性能优化
桦说编程17 小时前
从 ForkJoinPool 的 Compensate 看并发框架的线程补偿思想
java·后端·源码阅读
躺平大鹅19 小时前
Java面向对象入门(类与对象,新手秒懂)
java
初次攀爬者20 小时前
RocketMQ在Spring Boot上的基础使用
java·spring boot·rocketmq
花花无缺20 小时前
搞懂@Autowired 与@Resuorce
java·spring boot·后端