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?
That exception indicates that you initialize your
PdfDocumentwith only aPdfReader, noPdfWriter. You don't show yourPdfDocumentinstantiation code but I assume you do something like this: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
PdfDocumentwith both aPdfReaderand aPdfWriter, e.g.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
SOURCEin thePdfReaderand asDESTINATIONin thePdfWriter.Either first write to a temporary file, close all participating objects, and then replace the original file with the temporary file:
Or read the original file into a
byte[]and initialize thePdfReaderfrom that array: