当我尝试连续打印一些文档时,出现标题中描述的异常。
第一个被打印,但是第二个过程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
谢谢。
问题已解决。对于每个文档,必须生成单独的组iText对象(单元格,段落,表...)。