我以这种方式创建了一个字体。
Font tableHeaderFont = new Font(Font.HELVETICA, 8, Font.NORMAL, Color.WHITE);
然后在PdfPCell中使用Phrase。
PdfPCell hcell = new PdfPCell(new Phrase("Column A", tableHeaderFont));
我在每一页上显示表头。
现在,如果我删除字体颜色,它会生成大小为10MB的PDF文件,但是当提供颜色时,它会生成大小为24MB的PDF文件。PDF文档中大约有1400页。
有没有更好的方法在PdfPCell级别上指定字体颜色?
除此之外,当我尝试使用PdfSmartCopy
合并这些pdf文档时,它需要大约4 GB的内存使用。
我已经尝试了iText
更新:
iText 5.5:
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
当我使用iText 5的BaseColor类时,我会得到相同大小的带有或不带有字体颜色的pdf文件。
Font tableHeaderFont = new Font(Font.HELVETICA, 8, Font.NORMAL, BaseColor.WHITE);
OpenPDF 1.3.20
<dependency>
<groupId>com.github.librepdf</groupId>
<artifactId>openpdf</artifactId>
<version>1.3.20</version>
</dependency>
但是OpenPDF没有带BaseColor类,所以我必须在这里使用Color. WHITE。
Font tableHeaderFont = new Font(Font.HELVETICA, 8, Font.NORMAL, Color.WHITE);
我们在OpenPDF或iText4中有BaseColor类的替代方案吗?
更新2:重现问题的示例用例。
OpenPDF Impl:PDF文件大小约为15MB
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();
}
}
iText Impl:PDF文件大小约为8.5MB
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();
}
}
从分享的片段中,我可以了解到使用了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中得到修复。我使用的是旧版本。