Itext 7 PdfException - "com.itextpdf.kernel.PdfException: косвенный объект PDF принадлежит другому документу PDF. Скопируйте объект в текущий документ PDF." - PullRequest
0 голосов
/ 17 апреля 2020

Я получаю исключение, описанное в заголовке, когда пытаюсь печатать несколько документов непрерывно.

Первый напечатан, но для второго процесса создайте c .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);
}

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.Текст кода ядра, выдавший исключение

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

PS1. I Я использую сервисы Spring для создания таблиц, шрифтов и т. д. c.

PS2. Версия Itext - 7.1.10

Спасибо.

1 Ответ

2 голосов
/ 18 апреля 2020

Проблема решена. Для каждого документа должно быть сгенерировано отдельных наборов объектов iText (Cell, Paragraph, Table ...).

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...