The removing page has already been flushed - java

I created a PDF file with the data from an Excel file. I am not sure what was happened, but when I tried to remove one PdfPage and insert it somewhere else, it showed the warning message "The removing page has already been flushed".
The used code is quite simple:
PdfDocument pdf = ...;
....
PdfPage page = pdf.removePage(10);
pdf.addPage(1, page);
But I got warning and errors:
[main] WARN com.itextpdf.kernel.pdf.PdfPage - The removing page has
already been flushed. Exception in thread "main"
com.itextpdf.kernel.PdfException:flushed.page.cannot.be.added.or.inserted
at
com.itextpdf.kernel.pdf.PdfDocument.checkAndAddPage(PdfDocument.java:1473)
at com.itextpdf.kernel.pdf.PdfDocument.addPage(PdfDocument.java:437)
To be honest, I did have tried the above code with some other PDF files, it works to remove and to insert pages. What could be the possible reasons with my PDF file?
Complete code used in my application:
PdfWriter writer;
PdfDocument pdfDocument;
Document document;
try {
writer = new PdfWriter(FileConfigurator.getAbsoluteResultFilePath(),
new WriterProperties().addXmpMetadata().setPdfVersion(PdfVersion.PDF_1_7));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//Initialize PDF document
pdfDocument = new PdfDocument(writer);
// Initialize document
document = new Document(pdfDocument);

Related

PDFBox Open PDF file into new browser tab

I am using the pdfbox library 2.0 version. I need to open PDF in new browser tab i.e. Print View.
As if we are migrating from iText to PDFBox below is the existing code with iText.
With below code, there is PDFAction class to achieve same. It is,
PdfAction action = new PdfAction(PdfAction.PRINTDIALOG);
and to apply print Javascript on doc,
copy.addJavaScript(action);
I need equivalent solution with PDFBox.
Document document = new Document();
try{
outputStream=response.getOutputStream();
// step 2
PdfCopy copy = new PdfCopy(document, outputStream);
// step 3
document.open();
// step 4
PdfReader reader;
int n;
//add print dialog in Pdf Action to open file for preview.
PdfAction action = new PdfAction(PdfAction.PRINTDIALOG);
// loop over the documents you want to concatenate
Iterator i=mergepdfFileList.iterator();
while(i.hasNext()){
File f =new File((String)i.next());
is=new FileInputStream(f);
reader=new PdfReader(is);
n = reader.getNumberOfPages();
for (int page = 0; page < n; ) {
copy.addPage(copy.getImportedPage(reader, ++page));
}
copy.freeReader(reader);
reader.close();
is.close();
}
copy.addJavaScript(action);
// step 5
document.close();
}catch(IOException io){
throw io;
}catch(DocumentException e){
throw e;
}catch(Exception e){
throw e;
}finally{
outputStream.close();
}
I also tried with below reference but could not find print() method of PDDocument type.
Reference Link
Please guide me with this.
This is how file looks when display in browser tab:
This code reproduces what your file has, a JavaScript action in the name tree in the JavaScript entry in the name dictionary in the document catalog. ("When the document is opened, all of the actions in this name tree shall be executed, defining JavaScript functions for use by other scripts in the document" - PDF specification) There's probably an easier way to do this, e.g. with an OpenAction.
PDActionJavaScript javascript = new PDActionJavaScript("this.print(true);\n");
PDDocumentCatalog documentCatalog = document.getDocumentCatalog();
PDDocumentNameDictionary names = new PDDocumentNameDictionary(documentCatalog, new COSDictionary());
PDJavascriptNameTreeNode javascriptNameTreeNode = new PDJavascriptNameTreeNode();
Map<String, PDActionJavaScript> map = new HashMap<>();
map.put("0000000000000000", javascript);
javascriptNameTreeNode.setNames(map);
names.setJavascript(javascriptNameTreeNode);
document.getDocumentCatalog().setNames(names);

How to use same form template on every page in IText 7

I have a pdf template which contains images and form fields.
I read this template and fill form fields per page and write to a temp pdf file. Then I read this file and copy to a master document to have multiple pages using the same template. Roughly as below:
Document masterDoc = ...
-- loop per page --
PdfWriter pfdWriter = new PdfWriter(tmpFileName);
PdfDocument pdf = new PdfDocument(new PdfReader(templateFile), pfdWriter);
Document doc = new Document(pdf);
// Set form fields
PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
form.setDefaultJustification(0);
Map<String, PdfFormField> formFields = form.getFormFields();
formFields.get("key").setValue("value");
form.flattenFields();
doc.close();
try (PdfDocument resource = new PdfDocument(new PdfReader("pathToTmpFile"))) {
resource.copyPagesTo(1, 1, masterDoc.getPdfDocument());
}
-- end of loop
This approach is slow (depends on the template file size, but takes seconds not milliseconds).
Would it be possible to use same template file per every page without writing and reading from/to temp files?
I read the documentation and guess it might be possible with new page event handler but couldn't figure it out.

PDFBox 2.0.3: PDDocument Scratch File Already Closed

I'm generating a PDDocument in Java with code like this...
HashMap<Integer, PDPageContentStream> mPageContentStreamMap = new HashMap<>();
PDDocument doc = new PDDocument();
for (int i = 1; i <= mNumPages; i++) {
PDPage page = new PDPage(PDRectangle.A4);
page.setRotation(90);
PDPageContentStream pageContentStream = new PDPageContentStream(doc, page);
contentStreamMap.put(i, pageContentStream);
doc.addPage(page);
}
}
Then later save and close the document like this...
for (int i : mPageContentStreamMap.keySet()) {
mPageContentStreamMap.get(i).close();
}
doc.save("test-filename");
doc.close();
This works fine on the first run; however when I run my program multiple times I get the following error
java.io.IOException: Scratch file already closed
at org.apache.pdfbox.io.ScratchFile.checkClosed(ScratchFile.java:390)
at org.apache.pdfbox.io.ScratchFileBuffer.<init>(ScratchFileBuffer.java:78)
at org.apache.pdfbox.io.ScratchFile.createBuffer(ScratchFile.java:403)
at org.apache.pdfbox.cos.COSStream.createOutputStream(COSStream.java:208)
at org.apache.pdfbox.pdmodel.common.PDStream.createOutputStream(PDStream.java:224)
at org.apache.pdfbox.pdmodel.PDPageContentStream.<init>(PDPageContentStream.java:259)
at org.apache.pdfbox.pdmodel.PDPageContentStream.<init>(PDPageContentStream.java:121)
If I re-run my program without the "doc.close();" line, this error goes away, but the output of the PDF is duplicated (i.e. a new PDF is generated, but with the content from the last PDF and the content from the current PDF).
Is there a way to close the stream and create multiple PDFs without running into the scratch file error?
I had created a singleton object for my drawing logic meaning after the first run, the same objects were reused when they shouldn't've been, because the input (what was being drawn) had changed.

iText mergeFields in PdfCopy creates invalid pdf

I am working on the task of merging some input PDF documents using iText 5.4.5. The input documents may or may not contain AcroForms and I want to merge the forms as well.
I am using the example pdf files found here and this is the code example:
public class TestForms {
#Test
public void testNoForms() throws DocumentException, IOException {
test("pdf/hello.pdf", "pdf/hello_memory.pdf");
}
#Test
public void testForms() throws DocumentException, IOException {
test("pdf/subscribe.pdf", "pdf/filled_form_1.pdf");
}
private void test(String first, String second) throws DocumentException, IOException {
OutputStream out = new FileOutputStream("/tmp/out.pdf");
InputStream stream = getClass().getClassLoader().getResourceAsStream(first);
PdfReader reader = new PdfReader(new RandomAccessFileOrArray(
new RandomAccessSourceFactory().createSource(stream)), null);
InputStream stream2 = getClass().getClassLoader().getResourceAsStream(second);
PdfReader reader2 = new PdfReader(new RandomAccessFileOrArray(
new RandomAccessSourceFactory().createSource(stream2)), null);
Document pdfDocument = new Document(reader.getPageSizeWithRotation(1));
PdfCopy pdfCopy = new PdfCopy(pdfDocument, out);
pdfCopy.setFullCompression();
pdfCopy.setCompressionLevel(PdfStream.BEST_COMPRESSION);
pdfCopy.setMergeFields();
pdfDocument.open();
pdfCopy.addDocument(reader);
pdfCopy.addDocument(reader2);
pdfCopy.close();
reader.close();
reader2.close();
}
}
With input files containing forms I get a NullPointerException with or without compression enabled.
With standard input docs, the output file is created but when I open it with Acrobat it says there was a problem (14) and no content is displayed.
With standard input docs AND compression disabled the output is created and Acrobat displays it.
Questions
I previously did this using PdfCopyFields but it's now deprecated in favor of the boolean flag mergeFields in the PdfCopy, is this correct? There's no javadoc on that flag and I couldn't find documentation about it.
Assuming the answer to the previous question is Yes, is there anything wrong with my code?
Thanks
We are using PdfCopy to merge differents files, some of files may have fields. We use the version 5.5.3.0. The code is simple and it seems to work fine, BUT sometimes the result file is impossible to print!
Our code :
Public Shared Function MergeFiles(ByVal sourceFiles As List(Of Byte())) As Byte()
Dim document As New Document()
Dim output As New MemoryStream()
Dim copy As iTextSharp.text.pdf.PdfCopy = Nothing
Dim readers As New List(Of iTextSharp.text.pdf.PdfReader)
Try
copy = New iTextSharp.text.pdf.PdfCopy(document, output)
copy.SetMergeFields()
document.Open()
For fileCounter As Integer = 0 To sourceFiles.Count - 1
Dim reader As New PdfReader(sourceFiles(fileCounter))
reader.MakeRemoteNamedDestinationsLocal()
readers.Add(reader)
copy.AddDocument(reader)
Next
Catch exception As Exception
Throw exception
Finally
If copy IsNot Nothing Then copy.Close()
document.Close()
For Each reader As PdfReader In readers
reader.Close()
Next
End Try
Return output.GetBuffer()
End Function
Your usage of PdfCopy.setMergeFields() is correct and your merging code is fine.
The issues you described are because of bugs that have crept into 5.4.5. They should be fixed in rev. 6152 and the fixes will be included in the next release.
Thanks for bringing this to our attention.
Its just to say that we have the same probleme : iText mergeFields in PdfCopy creates invalid pdf. So it is still not fixed in the version 5.5.3.0

Java: combine 2000-5000 PDFs into 1 using iText yield OutOfMemorryError

I have eyeballing this code for a long time, trying to reducing the amount of memory the code use and still it generated java.lang.OutOfMemoryError: Java heap space. As my last resort, I want to ask the community on how can I improve this code to avoid OutOfMemoryError
I have a driver/manifest file (.txt file) that contain information about the PDFs. I have about 2000-5000 pdf inside a zip file that I need to combine together. Before the combining, for each pdf, I need to add 2-3 more pdf pages to it. Manifest object holds information about a pdf.
try{
blankPdf = new PdfReader(new FileInputStream(config.getBlankPdf()));
mdxBacker = new PdfReader(new FileInputStream(config.getMdxBacker()));
theaBacker = new PdfReader(new FileInputStream(config.getTheaBacker()));
mdxAffidavit = new PdfReader(new FileInputStream(config.getMdxAffidavit()));
theaAffidavit = new PdfReader(new FileInputStream(config.getTheaAffidavit()));
ImmutableList<Manifest> manifestList = //Read manifest file and obtain List<Manifest>
File zipFile = new File(config.getInputDir() + File.separator + zipName);
//Extracting PDF into `process` folder
ZipUtil.extractAll(config.getExtractPdfDir(), zipFile);
outputPdfName = zipName.replace(".zip", ".pdf");
outputZipStream = new FileOutputStream(config.getOutputDir() +
File.separator + outputPdfName);
document = new Document(PageSize.LETTER, 0, 0, 0, 0);
writer = new PdfCopy(document , outputZipStream);
document.open(); //Open the document
//Start combining PDF files together
for(Manifest m : manifestList){
//Obtain full path to the current pdf
String pdfFilePath = config.getExtractPdfDir() + File.separator + m.getPdfName();
//Before combining PDF, add backer and affidavit to individual PDF
PdfReader pdfReader = PdfUtil.addBackerAndAffidavit(config, pdfType, m,
pdfFilePath, blankPdf, mdxBacker, theaBacker, mdxAffidavit,
theaAffidavit);
for(int pageNumber=1; pageNumber<=pdfReader.getNumberOfPages(); pageNumber++){
document.newPage();
PdfImportedPage page = writer.getImportedPage(pdfReader, pageNumber);
writer.addPage(page);
}
}
} catch (DocumentException e) {
} catch (IOException e) {
} finally{
if(document != null) document.close();
try{
if(outputZipStream != null) outputZipStream.close();
if(writer != null) writer.close();
}catch(IOException e){
}
}
Please, rest assure that I have look at this code for a long time, and try rewrite it many times to reduce the amount of memory it using. After the OutOfMemoryError, there are still lots of pdf files that have not been added 2-3 extra pages, so I think it is inside addBackerAndAffidavit, however, I try to close every resources I opened, but it still exception out. Please help.
You need to invoke PdfWriter#freeReader() by end of every loop to free the involved PdfReader. The PdfCopy#freeReader() has this method inherited from PdfWriter and does the same. See also the javadoc:
freeReader
public void freeReader(PdfReader reader)
throws IOException
Description copied from class: PdfWriter
Use this method to writes the reader to the document and free the memory used by it. The main use is when concatenating multiple documents to keep the memory usage restricted to the current appending document.
Overrides:
freeReader in class PdfWriter
Parameters:
reader - the PdfReader to free
Throws:
IOException - on error

Categories