Я пытаюсь добавить динамически созданную таблицу в динамически создаваемый существующий PDF. Я хочу «добавить» таблицу в определенную позицию на последней странице существующего PDF.
Мой текущий код основан на ответе Владимира Осипчука на этой ветке, он выглядит так:
public byte[] completePDF(byte[] originalPDF){
ByteArrayInputStream is = new ByteArrayInputStream(originalPDF);
ByteArrayOutputStream os = new ByteArrayOutputStream();
PdfDocument pdfDoc;
try {
pdfDoc = new PdfDocument(new PdfReader(is), new PdfWriter(os));
final Document doc = new Document(pdfDoc);
//FAKE TABLE TO TEST
Table table = new Table(UnitValue.createPercentArray(new float[] {10f,90f}));
for (int i = 0; i<150; i++){
table.addCell(new String(new Integer(i).toString()));
table.addCell("FAKE DATA");
}
doc.setRenderer(new DocumentRenderer(doc) {
@Override
protected LayoutArea updateCurrentArea(LayoutResult overflowResult) {
int lastPage = doc.getPdfDocument().getNumberOfPages();
LayoutArea area = super.updateCurrentArea(overflowResult);
if (area.getPageNumber() == lastPage) {
PdfDocumentContentParser parser = new PdfDocumentContentParser(doc.getPdfDocument());
TextMarginFinder finder = parser.processContent(lastPage, new TextMarginFinder());
area.getBBox().decreaseHeight(finder.getTextRectangle().getHeight() + 30);
}
return area;
}
});
doc.add(table);
doc.close();
} catch (Exception e) {
return null;
}
return os.toByteArray();
}
Этот код выполняет почти то, что я хочу (новые страницы добавляются в зависимости от размера таблицы, а позиция таблицы на последней странице начинается после текста этой страницы), но я не знаю, для чего вызывать таблицу чтобы начать рисовать на последней странице.
Возможно ли?
Любая помощь будет принята с благодарностью.
Спасибо!