自学内容网 自学内容网

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

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

步骤 1: 添加 iText 5 依赖

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

<!-- 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 文件,并添加样式。

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 文件。根据需要,你可以添加更复杂的内容和样式。


原文地址:https://blog.csdn.net/weixin_73060959/article/details/142456491

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!