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

Itext PdfPCell上的字体颜色增加了PDF文件的大小,itext,pdf-generation,openpdf,Itext,Pdf Generation,Openpdf,我用这种方法创建了一种字体 Font tableHeaderFont = new Font(Font.HELVETICA, 8, Font.NORMAL, Color.WHITE); 然后在PdfPCell中使用短语 PdfPCell hcell = new PdfPCell(new Phrase("Column A", tableHeaderFont)); 我在每页上显示表格标题 现在,如果我删除字体颜色,它将生成大小为10 MB的PDF文件,但当提供颜色时,它将生成大

我用这种方法创建了一种字体

Font tableHeaderFont = new Font(Font.HELVETICA, 8, Font.NORMAL, Color.WHITE);
然后在PdfPCell中使用短语

PdfPCell hcell = new PdfPCell(new Phrase("Column A", tableHeaderFont));
我在每页上显示表格标题

现在,如果我删除字体颜色,它将生成大小为10 MB的
PDF文件
,但当提供颜色时,它将生成大小为24 MB的PDF文件。PDF文档大约有1400页

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

除此之外,当我尝试使用
PdfSmartCopy
合并这些pdf文档时,大约需要4 GB的内存使用

我尝试了
iText
OpenPDF

更新:

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13</version>
</dependency>
iText 5.5:

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

<dependency>
    <groupId>com.github.librepdf</groupId>
    <artifactId>openpdf</artifactId>
    <version>1.3.20</version>
</dependency>
OpenPDFiText4中,我们是否有BaseColor类的替代方案

更新2:复制问题的示例用例。

OpenPDF Impl:PDF文件大小约为15 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, 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, Font.NORMAL, 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();
    }
}
import com.itextpdf.text.*;
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;

public class TestIText {

    public static void main(String[] args) throws FileNotFoundException {
        Document document = new Document(PageSize.A4.rotate(), 10, 10, 10, 10);
        Document.compress = false;
        try {
            PdfWriter.getInstance(document,
                    new FileOutputStream("AddBigTable_iText.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 = FontFactory.getFont(FontFactory.HELVETICA, 20, Font.NORMAL, BaseColor.WHITE);
            PdfPCell header = new PdfPCell(new Phrase("Clock #", tableHeaderFont));
            header.setBackgroundColor(BaseColor.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();
    }
}
import com.lowagie.text.*;
导入com.lowagie.text.Font;
导入com.lowagie.text.pdf.PdfPCell;
导入com.lowagie.text.pdf.PdfPTable;
导入com.lowagie.text.pdf.PdfWriter;
导入java.awt.*;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
公共类TestOpenPDF{
公共静态void main(字符串[]args)引发FileNotFoundException{
文档=新文档(PageSize.A4.rotate(),10,10,10,10);
Document.compress=false;
试一试{
PdfWriter.getInstance(文档,
新的FileOutputStream(“AddBigTable_OpenPDF.pdf”);
document.open();
字符串[]bogudata={“M0065920”};
int NumColumns=1;
PdfPTable datatable=新的PdfPTable(NumColumns);
datatable.getDefaultCell().setPadding(3);
datatable.getDefaultCell().setBorderWidth(2);
datatable.getDefaultCell().setHorizontalAlignment(
元素。对齐(U中心);
Font tableHeaderFont=新字体(Font.HELVETICA,20,Font.NORMAL,Color.WHITE);
PdfPCell header=新的PdfPCell(新短语(“Clock#”,tableHeaderFont));
标题.背景颜色(颜色.灰色);
标题。设置填充(3);
页眉宽度(2);
标题.设置水平对齐(元素.对齐\中心);
datatable.addCell(表头);
datatable.setHeaderRows(1);//这是表头的结尾
datatable.getDefaultCell().setBorderWidth(1);
对于(int i=1;i<75000;i++){
如果(i%2==1){
datatable.getDefaultCell().setGrayFill(0.9f);
}
对于(int x=0;x
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, 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, Font.NORMAL, 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();
    }
}
import com.itextpdf.text.*;
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;

public class TestIText {

    public static void main(String[] args) throws FileNotFoundException {
        Document document = new Document(PageSize.A4.rotate(), 10, 10, 10, 10);
        Document.compress = false;
        try {
            PdfWriter.getInstance(document,
                    new FileOutputStream("AddBigTable_iText.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 = FontFactory.getFont(FontFactory.HELVETICA, 20, Font.NORMAL, BaseColor.WHITE);
            PdfPCell header = new PdfPCell(new Phrase("Clock #", tableHeaderFont));
            header.setBackgroundColor(BaseColor.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();
    }
}
import com.itextpdf.text.*;
导入com.itextpdf.text.pdf.PdfPCell;
导入com.itextpdf.text.pdf.PdfPTable;
导入com.itextpdf.text.pdf.PdfWriter;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
公共类文本{
公共静态void main(字符串[]args)引发FileNotFoundException{
文档=新文档(PageSize.A4.rotate(),10,10,10,10);
Document.compress=false;
试一试{
PdfWriter.getInstance(文档,
新文件输出流(“AddBigTable_iText.pdf”);
document.open();
字符串[]bogudata={“M0065920”};
int NumColumns=1;
PdfPTable datatable=新的PdfPTable(NumColumns);
datatable.getDefaultCell().setPadding(3);
datatable.getDefaultCell().setBorderWidth(2);
datatable.getDefaultCell().setHorizontalAlignment(
元素。对齐(U中心);
Font tableHeaderFont=FontFactory.getFont(FontFactory.HELVETICA,20,Font.NORMAL,BaseColor.WHITE);
PdfPCell header=新的PdfPCell(新短语(“Clock#”,tableHeaderFont));
标题.背景色(底色.灰色);
标题。设置填充(3);
页眉宽度(2);
标题.设置水平对齐(元素.对齐\中心);
datatable.addCell(表头);
datatable.setHeaderRows(1);//这是表头的结尾
datatable.getDefaultCell().setBorderWidth(1);
对于(int i=1;i<75000;i++){
如果(i%2==1){
datatable.getDefaultCell().setGrayFill(0.9f);
}
对于(int x=0;x
从共享的代码片段中,我可以了解到使用了
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();
PdfDocument pdfDoc=新的PdfDocument(新的PdfWriter(outFileName));
文档文档=新文档(pdfDoc