在生成PDF然后使用Itext 7合并它时,如何摆脱PDF startxref错误和AbstractRenderer警告消息?

时间:2019-07-18 11:35:35

标签: itext itext7

我正在使用IText版本7.1.6生成PDF,最后我尝试将其合并。

下面是用于与注释合并的代码。

List<byte[]> pdfDocumentList= new ArrayList<byte[]>();
//pdfDocumentList has list of byte arrays generated from other ways
ByteArrayOutputStream mergeOutputStream = new ByteArrayOutputStream();
PdfDocument pdfMerged = new PdfDocument(new PdfWriter(mergeOutputStream));
PdfMerger merger = new PdfMerger(pdfMerged);
ByteArrayOutputStream finalOutputStream = new ByteArrayOutputStream();
PdfWriter writer = new PdfWriter(finalOutputStream);
PdfDocument pdf = new PdfDocument(writer);
//sb is containing the concatenated HTML sources
HtmlConverter.convertToPdf(sb.toString(), pdf, properties);
pdf.close();
pdfDocumentList.add(finalOutputStream.toByteArray());
if(!pdfDocumentList.isEmpty()){
    for(byte[] bytes : pdfDocumentList){
        PdfDocument externalPdf = new PdfDocument(new PdfReader(new ByteArrayInputStream(bytes)));
        merger.merge(externalPdf, 1, externalPdf.getNumberOfPages());
    }
}
pdfMerged.close();
return mergeOutputStream.toByteArray();

当我合并PDF列表时,出现以下错误和警告。此外,警告还会多次打印。谁能让我知道如何解决这个问题?

警告

WARNING: The background rectangle has negative or zero sizes. It will not be displayed.
Jul 18, 2019 2:24:24 PM com.itextpdf.layout.renderer.AbstractRenderer drawBackground

错误

<Jul 18, 2019, 2:27:19,964 PM AST> <Error> <com.itextpdf.kernel.pdf.PdfReader> <BEA-000000> <Error occurred while reading cross reference table. Cross reference table will be rebuilt.
com.itextpdf.io.IOException: PDF startxref not found.
    at com.itextpdf.io.source.PdfTokenizer.getStartxref(PdfTokenizer.java:262)
    at com.itextpdf.kernel.pdf.PdfReader.readXref(PdfReader.java:753)
    at com.itextpdf.kernel.pdf.PdfReader.readPdf(PdfReader.java:538)
    at com.itextpdf.kernel.pdf.PdfDocument.open(PdfDocument.java:1818)
    at com.itextpdf.kernel.pdf.PdfDocument.<init>(PdfDocument.java:238)
    Truncated. see log file for complete stacktrace
> 
2019-07-18 14:27:19 ERROR user: KALASINX ip: 127.0.0.1 (ServiceInterceptor.java:59) ~ ServiceInterceptor Error: 
com.itextpdf.kernel.PdfException: Trailer not found.
    at com.itextpdf.kernel.pdf.PdfReader.rebuildXref(PdfReader.java:1064) ~[kernel-7.1.6.jar:?]

1 个答案:

答案 0 :(得分:0)

分析HTML代码并进行反复测试之后,我摆脱了警告消息。我必须删除与table,tr和td标签相关的HTML CSS中的 background-color 样式。