Hello,

I've modified the CopyPdf example in an attempt to copy the content of one
PDF file, and the AcroForm from another, to create a third PDF.
When it attempts to copy the AcroForm, it throws an exception:

java.lang.NullPointerException
        at com.lowagie.text.pdf.PdfWriter.getReaderFile(PdfWriter.java:1951)
        at com.lowagie.text.pdf.PRStream.toPdf(PRStream.java:194)
        at
com.lowagie.text.pdf.PdfIndirectObject.writeTo(PdfIndirectObject.java:167)
        at com.lowagie.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:369)
        at com.lowagie.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:349)
        at com.lowagie.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:341)
        at com.lowagie.text.pdf.PdfWriter.addToBody(PdfWriter.java:1860)
        at com.lowagie.text.pdf.PdfCopy.copyIndirect(PdfCopy.java:194)
        at com.lowagie.text.pdf.PdfCopy.copyObject(PdfCopy.java:260)
        at com.lowagie.text.pdf.PdfCopy.copyArray(PdfCopy.java:246)
        at com.lowagie.text.pdf.PdfCopy.copyObject(PdfCopy.java:262)
        at com.lowagie.text.pdf.PdfCopy.copyDictionary(PdfCopy.java:216)
        at com.lowagie.text.pdf.PdfCopy.copyObject(PdfCopy.java:258)
        at com.lowagie.text.pdf.PdfCopy.copyIndirect(PdfCopy.java:193)
        at com.lowagie.text.pdf.PdfCopy.copyObject(PdfCopy.java:260)
        at com.lowagie.text.pdf.PdfCopy.copyDictionary(PdfCopy.java:216)
        at com.lowagie.text.pdf.PdfCopy.copyObject(PdfCopy.java:258)
        at com.lowagie.text.pdf.PdfCopy.copyIndirect(PdfCopy.java:193)
        at com.lowagie.text.pdf.PdfCopy.copyObject(PdfCopy.java:260)
        at com.lowagie.text.pdf.PdfCopy.copyArray(PdfCopy.java:246)
        at com.lowagie.text.pdf.PdfCopy.copyObject(PdfCopy.java:262)
        at com.lowagie.text.pdf.PdfCopy.copyDictionary(PdfCopy.java:216)
        at com.lowagie.text.pdf.PdfCopy.copyAcroForm(PdfCopy.java:384)
        at AcroFormCopy.main(AcroFormCopy.java:71)


The CopyPdf example works fine on the same input file.


I'm assuming that I'm doing something wrong - does anyone have any insight
into this, or a better way to do it?


Here's the source code:

----------------------------------------------------------------------------
-----------------------------------

import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

/**
 * Utility to copy PDF Form Fields from one PDF file to another.
 * Useful when updating a PDF file by regenerating it from MS Word or
another source that does not preserve the fields.
 * Use this utility to copy the fields from the previous version to the new
version.
 */
public class AcroFormCopy
{    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) 
        {
                if (args.length != 3) 
                {
                        System.err.println("usage: java AcroFormCopy
fieldspdf nofieldspdf destpdf");
                }
                else 
                {
                        try 
                        {
                                String formFile = args[0];
                                String imageFile = args[1];
                                String outFile = args[2];
                                Document document = null;
                                PdfCopy  writer = null;
                                
                                // we create a reader for the "nofields
file"
                                PdfReader reader = new PdfReader(imageFile);
                                
                                // we retrieve the total number of pages
                                int n = reader.getNumberOfPages();
                                System.out.println("Copying " + n + "
page(s) of content from " + imageFile + " to " + outFile);
                                
                                // step 1: creation of a document-object
                                document = new
Document(reader.getPageSizeWithRotation(1));
                                
                                // step 2: we create a writer that listens
to the document
                                writer = new PdfCopy(document, new
FileOutputStream(outFile));
                                
                                // step 3: we open the document
                                document.open();
                                
                                
                                // step 4: we add content
                                PdfImportedPage page;
                                for (int i = 0; i < n; ) 
                                {
                                        ++i;
                                        page =
writer.getImportedPage(reader, i);
                                        writer.addPage(page);
                                        System.out.println("Processed page "
+ i);
                                }
                                
                                
                                // create a reader for the "form file"
                                PdfReader formReader = new
PdfReader(formFile);
                                
                                // copy the form
                                PRAcroForm form = formReader.getAcroForm();
                                
                                if(form == null)
                                {
                                        System.out.println("No fields found
in " + formFile);
                                }
                                else
                                {
                                        System.out.println("Copying fields
from " + formFile + " to " + outFile);
                                        writer.copyAcroForm(formReader);
                                }
                                
                                // step 5: we close the document
                                document.close();
                                
                                System.out.println("done!");
                        }
                        catch(Exception e) 
                        {
                                e.printStackTrace();
                        }
                }
        }
} 

----------------------------------------------------------------------------
-----------------------------------
Thanks,

-Rich

--------------------------------------------------
Rich Freedman
Senior Software Engineer
Cross Current Corporation
New Hope, PA, USA
http://www.crosscurrent.com
--------------------------------------------------



-------------------------------------------------------
This SF.Net email is sponsored by: IntelliVIEW -- Interactive Reporting
Tool for open source databases. Create drag-&-drop reports. Save time
by over 75%! Publish reports on the web. Export to DOC, XLS, RTF, etc.
Download a FREE copy at http://www.intelliview.com/go/osdn_nl
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to