PdfPCell 上的字体颜色增加了 PDF 文件的大小

如何解决PdfPCell 上的字体颜色增加了 PDF 文件的大小

我以这种方式创建了一个字体。

C

然后在 PdfPCell 中使用 Phrase。

Font tableHeaderFont = new Font(Font.HELVETICA,8,Font.NORMAL,Color.WHITE);

我在每一页都显示表格标题。

现在,如果我删除字体颜色,它会生成 PdfPCell hcell = new PdfPCell(new Phrase("Column A",tableHeaderFont)); ,但是当提供颜色时,它会生成 PDF file of size 10 MB 的 PDF 文件。 PDF文档大约有1400页。

有没有更好的方法在 PdfPCell 级别指定字体颜色?

除此之外,当我尝试使用 size 24 MB 合并这些 pdf 文档时,大约需要 4 GB 内存使用量。

PdfSmartCopyiText 我都试过了。

更新:

iText 5.5:

OpenPDF

当我使用 iText 5+ 的 BaseColor 类时,无论有没有字体颜色,我都会得到相同大小的 pdf 文件。

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13</version>
</dependency>

OpenPDF 1.3.20

Font tableHeaderFont = new Font(Font.HELVETICA,BaseColor.WHITE);

但是 OpenPDF 没有 BaseColor 类,所以我必须在这里使用 Color.WHITE

<dependency>
    <groupId>com.github.librepdf</groupId>
    <artifactId>openpdf</artifactId>
    <version>1.3.20</version>
</dependency>

我们在 OpenPDFiText4 中是否有任何 BaseColor 类的替代品?

更新 2:重现问题的示例用例。

OpenPDF Impl:PDF 文件大小约为 15 MB

Font tableHeaderFont = new Font(Font.HELVETICA,Color.WHITE);

iText Impl:PDF 文件大小约为 8.5 MB

import com.lowagie.text.*;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

import java.awt.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class TestOpenPDF {

    public static void main(String[] args) throws FileNotFoundException {
        Document document = new Document(PageSize.A4.rotate(),10,10);
        Document.compress = false;
        try {
            PdfWriter.getInstance(document,new FileOutputStream("AddBigTable_OpenPDF.pdf"));
            document.open();
            String[] bogusData = {"M0065920"};
            int NumColumns = 1;

            PdfPTable datatable = new PdfPTable(NumColumns);
            datatable.getDefaultCell().setPadding(3);
            datatable.getDefaultCell().setBorderWidth(2);
            datatable.getDefaultCell().setHorizontalAlignment(
                    Element.ALIGN_CENTER);
            Font tableHeaderFont = new Font(Font.HELVETICA,20,Color.WHITE);
            PdfPCell header = new PdfPCell(new Phrase("Clock #",tableHeaderFont));
            header.setBackgroundColor(Color.GRAY);
            header.setPadding(3);
            header.setBorderWidth(2);
            header.setHorizontalAlignment(Element.ALIGN_CENTER);
            datatable.addCell(header);

            datatable.setHeaderRows(1); // this is the end of the table header

            datatable.getDefaultCell().setBorderWidth(1);
            for (int i = 1; i < 75000; i++) {
                if (i % 2 == 1) {
                    datatable.getDefaultCell().setGrayFill(0.9f);
                }
                for (int x = 0; x < NumColumns; x++) {
                    datatable.addCell(bogusData[x]);
                }
                if (i % 2 == 1) {
                    datatable.getDefaultCell().setGrayFill(1);
                }
            }
            document.add(datatable);
        } catch (Exception de) {
            de.printStackTrace();
        }
        document.close();
    }
}

解决方法

从分享的片段中,我可以了解到使用了 iText 2。如果可能,最好重新考虑这样的选择,因为 iText 2 已有 10 多年的历史,可能存在很多功能和安全问题,不再维护。

我建议更新到 iText 7。虽然我没有您的代码,因此无法证明在您的情况下设置颜色不会使 PDF 的大小变大,但我创建了以下示例来证明应该没有问题:

    PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
    Document doc = new Document(pdfDoc);

    for (int i = 0; i < 10000; i++) {
        doc.add(new Paragraph("Hello World")/*.setFontColor(ColorConstants.RED)*/);
    }

    doc.close();

启用字体颜色后生成的 PDF 大小为 161Kb;生成的 PDF(其中禁用字体颜色)的大小为 160Kb(这是完全预期的,因为启用颜色意味着将相应的指令写入 PDF:那里有 10k 条指令,因此相差 1Kb)。

,

最新版本的 OpenPDF 1.3.25 已修复该问题。我用的是旧版本。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res