0

lowagie PdfWriter is not appending data but overwriting it whenever writeData is called. How can we append data?

void writeData(List<org.bson.Document> item){
boolean isExistingFile = Files.exists(Paths.get(filePath));
File file = new File(filePath);
PdfPTable table = null;
if (!isExistingFile) {
    writer = PdfWriter.getInstance(doc, new FileOutputStream(file, true));
    writer.setPageEvent(reportPdfWriter);
    table = new PdfPTable(headerLength);
    // table cell added for each header
        {   table.addCell(header); }
}
for (org.bson.Document d : item) {
    Iterator<Object> itr = d.values().iterator();
    while (itr.hasNext()) {
        Object obj = itr.next();
        table.addCell(String.valueOf(obj));
    }
}
doc.open();
doc.add(table);
}
surm
  • 167
  • 2
  • 11
  • Do you need to be using lowagie PdfWriter? See [here](https://stackoverflow.com/a/13515403/12567365) which discusses how that library is an older (_very_ much older) incarnation of what is now upgraded/rewritten as [iText](https://mvnrepository.com/artifact/com.itextpdf/itextpdf). So, first of all, I'd recommend "upgrading". If you can. – andrewJames Apr 19 '23 at 20:16
  • I can try that. New page is not getting created / it is not going into append. @andrewJames – surm Apr 20 '23 at 07:04
  • Now that you are using iText, have you researched existing iText answers, such as [Editing PDF text using Java](https://stackoverflow.com/q/4131031/12567365), [How to add Content to a PDF using iText PdfStamper](https://stackoverflow.com/q/8176780/12567365), or [iText - add content to existing PDF file](https://stackoverflow.com/q/3335126/12567365) and various other answers? Maybe you can try using those approaches and ask a new, specific question with a [mre] if you get stuck. – andrewJames Apr 20 '23 at 13:29

0 Answers0