Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » iText »

[iText-questions] Storing PDF with repetetive page in
 ByteArrayOutputStream

dreamboy

2007-08-13



Hi,

I would like to store the pdf in ByteArrayOutputStream (which is later will
be saved in the DB). Im confused what baos I will be saved, due to some
repetetive page. This is my code:

    PdfReader reader = new
PdfReader(context.getResourceAsStream("//file"));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfStamper stamper = new PdfStamper(reader, baos);

    Properties props = getSARFormProperties("mappingFile");


    AcroFields form = stamper.getAcroFields();

    Iterator<SarSubjectDTO> assocSubjects = null;
    if (formData.getSubjects() != null && formData.getSubjects().size()
> 0) {
       assocSubjects = formData.getSubjects().iterator();
       SarSubjectDTO subject = assocSubjects.next();
       fillSubjectInfoSection(props, form, subject);
       
    }
    else{
     
form.setField(props.getProperty(SUBJECT_INFO_UNAVAILABLE),"Yes");
    }

    stamper.setFormFlattening(true);
    stamper.close();


    reader = new PdfReader(baos.toByteArray());
    Document document = new Document(reader.getPageSizeWithRotation(1));
    PdfCopy writer = new PdfCopy(document, response.getOutputStream());
    document.open();
    writer.addPage(writer.getImportedPage(reader, 1));
    if (assocSubjects != null) {
       /*
        * Iterate over remaining associated subjects: for each one
create a separate
        * form in memory and copy Page 1 to the original final
document.
        */
       while (assocSubjects.hasNext()) {
          SarSubjectDTO subject = assocSubjects.next();
          PdfReader reader2 = new
PdfReader(context.getResourceAsStream("//file"));
          baos = new ByteArrayOutputStream();
          stamper = new PdfStamper(reader2, baos);
          form = stamper.getAcroFields();
          fillSubjectInfoSection(props, form, subject);
          stamper.setFormFlattening(true);
          stamper.close();
          reader2 = new PdfReader(baos.toByteArray());
          writer.addPage(writer.getImportedPage(reader2, 1));
       }

writer.addPage(writer.getImportedPage(reader, 2));


    }




Can I save the file using,

    ServletOutputStream out = response.getOutputStream();
    baos.writeTo(out);  // is this correct??
    byte[] pdfByte = baos.toByteArray();
    System.out.println("file Size:"+pdfByte.length);

Is this correct? Why is the pdfByte.length smaller than file size in windows
explorer when I saved it in the local machine? Please advice.
--
Sent from the iText - General mailing list archive at Nabble.com.


-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
iText-questions mailing list
iText-questions@(protected)
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/
©2008 junlu.com - Jax Systems, LLC, U.S.A.