2

It is there any way to add a Blank Page to an existing PdfDocument ? I've created a method like this:

  public void addEmptyPage(PdfDocument pdfDocument){
    pdfDocument.addNewPage();
    pdfDocument.close();
}

However , when I use it with a PdfDocument , it throws :

com.itextpdf.kernel.PdfException: There is no associate PdfWriter for making indirects.
at com.itextpdf.kernel.pdf.PdfObject.makeIndirect(PdfObject.java:228) ~[kernel-7.1.1.jar:?]
at com.itextpdf.kernel.pdf.PdfObject.makeIndirect(PdfObject.java:248) ~[kernel-7.1.1.jar:?]
at com.itextpdf.kernel.pdf.PdfPage.<init>(PdfPage.java:104) ~[kernel-7.1.1.jar:?]
at com.itextpdf.kernel.pdf.PdfDocument.addNewPage(PdfDocument.java:416) ~[kernel-7.1.1.jar:?]

Which is the correct way to insert a Blank page into a pdf document?

Nexussim Lements
  • 535
  • 1
  • 15
  • 47
  • Same question as https://stackoverflow.com/questions/58392082/add-blank-page-to-pdfdocument but why did you delete instead of edit? – Amedee Van Gasse Oct 15 '19 at 14:20

1 Answers1

2

com.itextpdf.kernel.PdfException: There is no associate PdfWriter for making indirects.

That exception indicates that you initialize your PdfDocument with only a PdfReader, no PdfWriter. You don't show your PdfDocument instantiation code but I assume you do something like this:

PdfReader reader = new PdfReader(SOURCE);
PdfDocument document = new PdfDocument(reader);

Such documents are for reading only. (Actually you can do some minor manipulations but nothing as big as adding pages.)

If you want to edit a PDF, initialize your PdfDocument with both a PdfReader and a PdfWriter, e.g.

PdfReader reader = new PdfReader(SOURCE);
PdfWriter writer = new PdfWriter(DESTINATION);
PdfDocument document = new PdfDocument(reader, writer);

If you want to store the edited file at the same location as the original file, you must not use the same file name as SOURCE in the PdfReader and as DESTINATION in the PdfWriter.

Either first write to a temporary file, close all participating objects, and then replace the original file with the temporary file:

PdfReader reader = new PdfReader("document.pdf");
PdfWriter writer = new PdfWriter("document-temp.pdf");
PdfDocument document = new PdfDocument(reader, writer);
...
document.close();
Path filePath = Path.of("document.pdf");
Path tempPath = Path.of("document-temp.pdf");
Files.move(tempPath, filePath, StandardCopyOption.REPLACE_EXISTING);

Or read the original file into a byte[] and initialize the PdfReader from that array:

PdfReader reader = new PdfReader(new ByteArrayInputStream(Files.readAllBytes(Path.of("document.pdf"))));
PdfWriter writer = new PdfWriter("document.pdf");
PdfDocument document = new PdfDocument(reader, writer);
...
document.close();
mkl
  • 90,588
  • 15
  • 125
  • 265
  • Could you give me an example of your explanation please ? I'm not familiar with Pdfs in Java – Nexussim Lements Oct 16 '19 at 07:33
  • I've added some code. I originally did not add it because it's mostly not specific to PDFs but basic Java stuff. – mkl Oct 16 '19 at 08:44
  • Can you check my new question about PDF ? https://stackoverflow.com/questions/58415525/cannot-copy-indirect-object-from-the-document-that-is-being-written-java – Nexussim Lements Oct 17 '19 at 11:03