You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用iText修改PDF背景色后生成的PDF打开报错求助

解决iText设置PDF背景色后文件损坏的问题

问题描述

我编写了ModifyPDFWithBackgroundColor类,尝试通过Java的iText库为PDF设置#2E506F(对应RGB值46,80,111)的背景色,但生成的PDF文件打开时出现错误。

相关代码

public class ModifyPDFWithBackgroundColor {

    public static void main(String[] args) throws IOException {

        String inputPdfFilePath = "/Users/nunyet/IdeaProjects/mystic-river-api/docs/style-guide/LogoFiles/ForPrint/pdf/color-logo-with-background.pdf";
        String outputPdfFilePath =  "/Users/nunyet/IdeaProjects/mystic-river-api/docs/style-guide/LogoFiles/ForPrint/pdf/color-logo-with-background2.pdf";
        DeviceRgb backgroundColor = new DeviceRgb(46, 80, 111);  // Convert HEX to RGB

        PdfDocument pdfDoc = new PdfDocument(new PdfReader(inputPdfFilePath), new PdfWriter(outputPdfFilePath));

        // Access the first page of the PDF document
        PdfPage page = pdfDoc.getFirstPage();

        // Create a PdfCanvas for the page
        PdfCanvas canvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDoc);

        // Set the background color
        canvas.saveState();
        canvas.setFillColor(backgroundColor);
        canvas.rectangle(0, 0, page.getPageSize().getWidth(), page.getPageSize().getHeight());
        canvas.fill();
        canvas.restoreState();

        System.out.println("PDF modified successfully!");
    }
}

错误截图

错误截图

解决方案

问题根源

代码未关闭PdfDocument对象,导致iText无法完成PDF文件的最终写入操作,文件结构不完整,从而触发打开错误。

修改后的代码

public class ModifyPDFWithBackgroundColor {

    public static void main(String[] args) throws IOException {

        String inputPdfFilePath = "/Users/nunyet/IdeaProjects/mystic-river-api/docs/style-guide/LogoFiles/ForPrint/pdf/color-logo-with-background.pdf";
        String outputPdfFilePath =  "/Users/nunyet/IdeaProjects/mystic-river-api/docs/style-guide/LogoFiles/ForPrint/pdf/color-logo-with-background2.pdf";
        DeviceRgb backgroundColor = new DeviceRgb(46, 80, 111);  // Convert HEX to RGB

        // 用try-with-resources自动管理文档资源,确保执行完毕后关闭文档
        try (PdfDocument pdfDoc = new PdfDocument(new PdfReader(inputPdfFilePath), new PdfWriter(outputPdfFilePath))) {
            PdfPage page = pdfDoc.getFirstPage();
            PdfCanvas canvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDoc);

            canvas.saveState();
            canvas.setFillColor(backgroundColor);
            canvas.rectangle(0, 0, page.getPageSize().getWidth(), page.getPageSize().getHeight());
            canvas.fill();
            canvas.restoreState();

            System.out.println("PDF modified successfully!");
        }
    }
}

关键修改说明

  • 使用try-with-resources语法:这是Java 7+的特性,能自动实现资源的关闭,避免因代码异常或遗漏手动关闭导致的文件损坏。
  • 若不使用try-with-resources,也可在代码最后手动调用pdfDoc.close(),但前者更安全可靠。

内容的提问来源于stack exchange,提问作者Nunyet Calçada

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 21:45:15