Itext 7 PdfException - "com.itextpdf.kernel.PdfException: Pdf indirect object belongs to other PDF document. Copy object to current pdf document."
Itext 7 PdfException - "com.itextpdf.kernel.PdfException: Pdf indirect object belongs to other PDF document. Copy object to current pdf document."
当我试图连续打印一些文档时,我遇到了标题中描述的异常。
第一个已打印,但第二个过程 doc.close() 抛出异常。
1.print 方法。
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);
}
2.getByteArray somePrintService
中的方法
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 创建表格、字体的服务 e.t.c.
PS2. itext 版本 - 7.1.10
谢谢。
问题已解决。
对于每个文档 必须生成单独的 组iText 对象(单元格、段落、Table ...)。
当我试图连续打印一些文档时,我遇到了标题中描述的异常。
第一个已打印,但第二个过程 doc.close() 抛出异常。
1.print 方法。
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);
}
2.getByteArray somePrintService
中的方法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 创建表格、字体的服务 e.t.c.
PS2. itext 版本 - 7.1.10
谢谢。
问题已解决。 对于每个文档 必须生成单独的 组iText 对象(单元格、段落、Table ...)。