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 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);




             PdfReader reader = new PdfReader("PDF_FORM.pdf");








The stacktrace:


Exception in thread "main" java.lang.NullPointerException

       at com.itextpdf.text.pdf.PdfCopy.updateReferences(

       at com.itextpdf.text.pdf.PdfCopy.updateReferences(

       at com.itextpdf.text.pdf.PdfCopy.updateReferences(

       at com.itextpdf.text.pdf.PdfCopy.addToBody(

       at com.itextpdf.text.pdf.PdfCopy.branchForm(

       at com.itextpdf.text.pdf.PdfCopy.createAcroForms(

       at com.itextpdf.text.pdf.PdfCopy.flushAcroFields(

       at com.itextpdf.text.pdf.PdfDocument.close(

       at com.itextpdf.text.Document.close(

       at MergeFormFreeReader.main(


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.