Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » iText »

[iText-questions] PdfCopy.copyAcroForm wrong method getReaderFile

Andreas Wittek

2005-09-26


Hello,

I am trying to use one PDF-File containing a form as as template (regarding the form) for other PDFs which have the same structure as the form document but do not contain a form.

Like Richard Freedman's Question on 2005-02-02 I try something like


  public static void copyFormToPDF(InputStream formpdf, InputStream tmplpdf, OutputStream outpdf)
      throws IOException {
    // Create document descriptors
   try {
     PdfReader formreader = new PdfReader(formpdf);
     PdfReader tmplreader = new PdfReader(tmplpdf);

     Document document = new Document(tmplreader.getPageSizeWithRotation(1));
     PdfCopy copy = new PdfCopy(document,outpdf);
     document.open();
     int n = tmplreader.getNumberOfPages();
     for (int i = 1; i <= n; i++) {
       copy.addPage(copy.getImportedPage(tmplreader,i));
     }
     copy.copyAcroForm(formreader);
     document.close();
   }
   catch (Exception ex) {
     ex.printStackTrace();
   }
  }

and I get

java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at java.io.BufferedOutputStream.write(Unknown Source)
at com.lowagie.text.pdf.OutputStreamCounter.write (OutputStreamCounter.java:160)
at com.lowagie.text.pdf.PRStream.toPdf (PRStream.java:218)
at com.lowagie.text.pdf.PdfIndirectObject.writeTo (PdfIndirectObject.java:167)
at com.lowagie.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:370)
at com.lowagie.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:350)
at com.lowagie.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:342)
at com.lowagie.text.pdf.PdfWriter.addToBody (PdfWriter.java:1889)
at com.lowagie.text.pdf.PdfCopy.copyIndirect (PdfCopy.java:195)
at com.lowagie.text.pdf.PdfCopy.copyObject (PdfCopy.java:263)
at com.lowagie.text.pdf.PdfCopy.copyArray (PdfCopy.java:249)
at com.lowagie.text.pdf.PdfCopy.copyObject (PdfCopy.java:265)
at com.lowagie.text.pdf.PdfCopy.copyDictionary (PdfCopy.java:216)
at com.lowagie.text.pdf.PdfCopy.copyObject (PdfCopy.java:261)
at com.lowagie.text.pdf.PdfCopy.copyIndirect (PdfCopy.java:194)
at com.lowagie.text.pdf.PdfCopy.copyObject (PdfCopy.java:263)
at com.lowagie.text.pdf.PdfCopy.copyDictionary (PdfCopy.java:219)
at com.lowagie.text.pdf.PdfCopy.copyObject (PdfCopy.java:261)
at com.lowagie.text.pdf.PdfCopy.copyIndirect (PdfCopy.java:194)
at com.lowagie.text.pdf.PdfCopy.copyObject (PdfCopy.java:263)
at com.lowagie.text.pdf.PdfCopy.copyArray (PdfCopy.java:249)
at com.lowagie.text.pdf.PdfCopy.copyObject (PdfCopy.java:265)
at com.lowagie.text.pdf.PdfCopy.copyDictionary (PdfCopy.java:219)
at com.lowagie.text.pdf.PdfCopy.copyObject (PdfCopy.java:261)
at com.lowagie.text.pdf.PdfCopy.copyIndirect (PdfCopy.java:194)
at com.lowagie.text.pdf.PdfCopy.copyObject (PdfCopy.java:263)
at com.lowagie.text.pdf.PdfCopy.copyArray (PdfCopy.java:249)
at com.lowagie.text.pdf.PdfCopy.copyObject (PdfCopy.java:265)
at com.lowagie.text.pdf.PdfCopy.copyDictionary (PdfCopy.java:219)
at com.lowagie.text.pdf.PdfCopy.copyAcroForm (PdfCopy.java:388)
at rw.util.pdf.PDFFormular.copyFormToPDF(PDFFormular.java:268)
at rw.util.pdf.PDFFormular.copyFormToPDF(PDFFormular.java:247)
at rw.util.pdf.FormCopy.main(FormCopy.java:20)


I think I have found the reason for this Problem which is that PdfCopy uses the inherited Method

    RandomAccessFileOrArray getReaderFile(PdfReader reader) {
       return currentPdfReaderInstance.getReaderFile();
    }

from PdfWriter. However, the currentPdfReaderInstance ist the wrong one, being the one for the PdfReader which does not contain the form.
This method should be overwritten by something like (compare to other methods in PdfCopy)

    RandomAccessFileOrArray getReaderFile(PdfReader reader) {
       if (currentPdfReaderInstance != null) {
          if (currentPdfReaderInstance.getReader() != reader) {
            try {
               currentPdfReaderInstance.getReaderFile().close();
            }
            catch (IOException ioe) {
               // empty on purpose
            }
            currentPdfReaderInstance = reader.getPdfReaderInstance(this);
          }
       }
       else {
          currentPdfReaderInstance = reader.getPdfReaderInstance(this);
       }
       return currentPdfReaderInstance.getReaderFile();
    }


When I add this mehtod to PdfCopy the Exception disappears and the information about the form appears in the output-PDF-file.

However, the form does not display, when I open that file in e.g. Acrobat Reader.

I do not understand enough about the content of PDF-files to be able to figure out, what goes wrong now.

Perhaps someone can help me on this?


Thanks

Andreas

*******************************************************
* Dr. Andreas Wittek
* Ravenworks GbR           email: wittek@(protected)
* Fahrenheitstr. 1    Tel.: ++49-421-2239700
* 28359 Bremen       Fax.: ++49-421-2239825
* Germany               http: www.ravenworks.de
*******************************************************



-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server.
Download it for free - -and be entered to win a 42" plasma tv or your very
own Sony(tm)PSP. Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
iText-questions mailing list
iText-questions@(protected)
https://lists.sourceforge.net/lists/listinfo/itext-questions
©2008 junlu.com - Jax Systems, LLC, U.S.A.