Itext 7 PdfException-“ com.itextpdf.kernel.PdfException:Pdf间接对象属于其他PDF文档。将对象复制到当前的pdf文档中。”

问题描述 投票:0回答:1

当我尝试连续打印一些文档时,出现标题中描述的异常。

第一个被打印,但是第二个过程doc.close()抛出异常。

1。打印方法。

private void print1(Cell header, Table table, Cell footer, int size) throws Exception {
    byte[] bytes = somePrintService.getByteArray(header, table, footer, size);
    somePrintService.printbytes(bytes);
}

private void print2(Cell header, Table table, Cell footer, int size) throws Exception {
    byte[] bytes = somePrintService.getByteArray(header, table, footer, size);
    somePrintService.printbytes(bytes);
}

somePrintService中的2.getByteArray方法

public byte[] getByteArray(Cell header, Table table, Cell footer, int height) {


    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    PdfWriter writer = new PdfWriter(outputStream);

    PdfDocument pdf = new PdfDocument(writer);
    Rectangle rectangle = new Rectangle(135f, height);
    PageSize pageSize = new PageSize(rectangle);
    pdf.setDefaultPageSize(pageSize);

    Document doc = new Document(pdf);
    doc.setMargins(0, 0, 0, 0);


    doc.add(header);
    doc.add(table);
    doc.add(footer);


    doc.close(); ------- Exception thrown here!!!

    return outputStream.toByteArray();
}

3.Itext引发异常的内核代码

private void write(PdfIndirectReference indirectReference) {
    if (document != null && !indirectReference.getDocument().equals(document)) {
        throw new PdfException(PdfException.PdfIndirectObjectBelongsToOtherPdfDocument);
    }
    ...
}

PS1。我正在使用Spring服务来创建表,等字体。

PS2。 itext版本-7.1.10

谢谢。

java spring pdf itext itext7
1个回答
0
投票

问题已解决。对于每个文档,必须生成单独的组iText对象(单元格,段落,表...)。

© www.soinside.com 2019 - 2024. All rights reserved.