Dear iText community,

I'm trying to copy a pdf containing a form (copy.mergeFields()), while limiting 
the memory usage(copy.freeReader(reader)) with code similar to this main class:

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfReader;

public class MergeFormFreeReader {

       public static void main(String[] args) throws DocumentException, 
IOException {
             Document document = new Document();
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
             PdfCopy copy = new PdfCopy(document, baos);
             copy.setMergeFields();
             document.open();

             PdfReader reader = new PdfReader("PDF_FORM.pdf");
             copy.addDocument(reader);
             copy.freeReader(reader);

             document.close();
       }
}

The stacktrace:

Exception in thread "main" java.lang.NullPointerException
       at com.itextpdf.text.pdf.PdfCopy.updateReferences(PdfCopy.java:1359)
       at com.itextpdf.text.pdf.PdfCopy.updateReferences(PdfCopy.java:1364)
       at com.itextpdf.text.pdf.PdfCopy.updateReferences(PdfCopy.java:1364)
       at com.itextpdf.text.pdf.PdfCopy.addToBody(PdfCopy.java:736)
       at com.itextpdf.text.pdf.PdfCopy.branchForm(PdfCopy.java:1464)
       at com.itextpdf.text.pdf.PdfCopy.createAcroForms(PdfCopy.java:1337)
       at com.itextpdf.text.pdf.PdfCopy.flushAcroFields(PdfCopy.java:792)
       at com.itextpdf.text.pdf.PdfDocument.close(PdfDocument.java:862)
       at com.itextpdf.text.Document.close(Document.java:416)
       at MergeFormFreeReader.main(MergeFormFreeReader.java:27)

Immediate cause: In PdfCopy.updateReferences it is assumed that the readers are 
stored (in indirectMap), while in fact the reader was 'freed'.

If I remove copy.freeReader(reader), MergeFormFreeReader produces a pdf, but I 
really want to use freeReader because I'm merging potentially a lot of pdfs.
If I remove copy.setMergeFields(), MergeFormFreeReader also produces a pdf, but 
now I'm not sure what will happen if a merge forms containing same field names, 
etc..

While writing this mail, I think I'm going to verify if it's possible to 
flatten all forms before merging, but in any case:

*         Is this a bug, or is it simply not possible to merge forms, while 
freeing readers?

*         Any suggestions for a work-around? Rename all fields myself, etc

Emiel Ackermann

________________________________

Deze e-mail en alle gekoppelde bestanden zijn officiele documenten van het 
Gemeentelijk Havenbedrijf Antwerpen en kunnen vertrouwelijke of persoonlijke 
informatie bevatten. Gelieve de afzender onmiddellijk via e-mail of telefonisch 
te verwittigen als u deze e-mail per vergissing heeft ontvangen en verwijder 
vervolgens de e-mail zonder deze te lezen, te reproduceren, te verspreiden of 
te ontsluiten naar derden. Het Gemeentelijk Havenbedrijf Antwerpen is op geen 
enkele manier verantwoordelijk voor fouten of onnauwkeurigheden in de inhoud 
van deze e-mail. Het Gemeentelijk Havenbedrijf Antwerpen kan niet aansprakelijk 
gesteld worden voor directe of indirecte schade, verlies of ongemak veroorzaakt 
als gevolg van een onnauwkeurigheid of fout in deze e-mail.

English Translation: This e-mail and all attached files are official documents 
of Antwerp Port Authority and may contain confidential or personal information. 
If you have received this e-mail in error, you are asked to inform the sender 
by e-mail or telephone immediately, and to remove it from your system without 
reading or reproducing it or passing it on to other parties. Antwerp Port 
Authority is in no way responsible for any errors or inaccuracies in the 
contents of this e-mail, nor can it be held liable for any direct or indirect 
loss, damage or inconvenience arising from any such errors or inaccuracies.
------------------------------------------------------------------------------
Want fast and easy access to all the code in your enterprise? Index and
search up to 200,000 lines of code with a free copy of Black Duck®
Code Sight™ - the same software that powers the world's largest code
search on Ohloh, the Black Duck Open Hub! Try it now.
http://p.sf.net/sfu/bds
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to