The code works fine. If you dump the ByteArrayOutputStream to a file, like
this...

  FileOutputStream fos = new FileOutputStream("mynewdocument.pdf",false);
  fos.write(bos.toByteArray());
  fos.close();

...the pdf is just fine. Opening a new document is useless, but not harmful.
Your error is occurring with whatever you are doing with the
ByteArrayOutputStream. If you are running a servlet, all you have to do is
set the http headers, then write to the response OutputStream just like
this...

      HttpServletResponse res.setContentType("application/pdf");
      ServletOutputStream out = res.getOutputStream();
      res.setBufferSize(bos.size());
      res.setContentLength(bos.size());
      bos.writeTo(out);
      out.flush();
      bos.close();

...easy peasy!

----- Original Message ----- 
From: "wmhfitz" <[email protected]>
To: <[email protected]>
Sent: Friday, December 19, 2008 12:00 PM
Subject: Re: [iText-questions] The document has no pages.


>
> Here's my class...
>
> public class FillEnabledForm {
>
> public static final String ENABLED_FORM = "CAI_0708.pdf";
> public static final String RESULT = "ENABLED_CAI.07.10.08.pdf";
> static String readPdf =
>
"C:\\TRMWorkspace\\ClearView\\WebContent\\WEB-INF\\resources\\forms\\CAI0708
.pdf";
>
> public static void main(String[] args) {
>
> Document document = new Document();
> ByteArrayOutputStream bos = new ByteArrayOutputStream();
>
> try {
> PdfReader reader = new PdfReader(readPdf);
> System.out.println("Pdf Version: "+reader.getPdfVersion());
> System.out.println("Num of Pages: "+ reader.getNumberOfPages());
> System.out.println("File Length: "+reader.getFileLength());
> System.out.println("Encrypted: "+reader.isEncrypted());
>
> document = new Document();
>
> PdfStamper stamper = new PdfStamper(reader, bos);
> // PdfWriter writer = PdfWriter.getInstance(document, bos);
> document.open();
>
> AcroFields form = stamper.getAcroFields();
> form.setField("fullName", "Blackie Lawless");
> form.setField("representativeName", "Rob Halford");
>
> // writer = stamper.getWriter();
> document.close();
> // writer.close();
> stamper.close();
>
> } catch (IOException e) {
> e.printStackTrace();
> } catch (DocumentException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> }
> }
>
> When the PdfWriter is not commented the same error occurs on the
> document.close() line.
>
>
> Steve Cole-6 wrote:
> >
> > Create small class file that can be executed from a command line that
> > creates the error. Submit it and the pdf used for input.
> >
> > ----- Original Message ----- 
> > From: "wmhfitz" <[email protected]>
> > To: <[email protected]>
> > Sent: Friday, December 19, 2008 10:04 AM
> > Subject: Re: [iText-questions] The document has no pages.
> >
> >
> >>
> >> I have tried that. When I replace the line
> >>     PdfStamper stamper = new PdfStamper(reader, bos);
> >> with
> >>     PdfStamper stamper = new PdfStamper(reader, new
> >> FileOutputStream(result));
> >> I get a new pdf in that directory, still with the same error. I guess
> > there
> >> actually 2 problems, getting rid of the error and figuring out how to
> > serve
> >> pdf to browser.
> >>
> >> any help is much appreciated.
> >>
> >>
> >>
> >> Steve Cole-6 wrote:
> >> >
> >> > I didn't see a problem with your code in the original post. Have you
> > tried
> >> > dumping the ByteOutputStream to a file see what the pdf looks like?
> > Could
> >> > be
> >> > how you're trying to get the contents of your ByteOutputStream to the
> >> > servlet response OutputStream.
> >> >
> >> >
> >> > ----- Original Message ----- 
> >> > From: "wmhfitz" <[email protected]>
> >> > To: <[email protected]>
> >> > Sent: Friday, December 19, 2008 9:17 AM
> >> > Subject: Re: [iText-questions] The document has no pages.
> >> >
> >> >
> >> >>
> >> >> I removed the line
> >> >>     writer = writer.getInstance(document, bos);
> >> >> That was not supposed to be there, it was left over from trying
> > different
> >> >> things to get pdf to the browser.
> >> >>
> >> >> But with that said, I still get the same error and the following
> > message
> >> >> displayed on screen.
> >> >>
> >> >> Instead of saying "it's bound to fail" do you have any real
> >> suggestions
> >> >> of
> >> >> what is I need to change to accomplish this?
> >> >>
> >> >>
> >> >> 1T3XT info wrote:
> >> >> >
> >> >> > wmhfitz wrote:
> >> >> >> This same code works fine in a standalone.
> >> >> >
> >> >> >> PdfStamper stamper = new PdfStamper(reader, bos);
> >> >> >> writer = writer.getInstance(document, bos);
> >> >> >
> >> >> > What is bos? A ByteArrayOutputStream?
> >> >> > Why are you writing to it from PdfStamper AND PdfWriter?
> >> >> > This is bound to fail.
> >> >> > -- 
> >> >> > This answer is provided by 1T3XT BVBA
> >> >> > http://www.1t3xt.com/ - http://www.1t3xt.info
> >> >> >
> >> >>
> >>
>
>>> ------------------------------------------------------------------------
-
> > -
> >> > ----
> >> >> > _______________________________________________
> >> >> > iText-questions mailing list
> >> >> > [email protected]
> >> >> > https://lists.sourceforge.net/lists/listinfo/itext-questions
> >> >> >
> >> >> > Buy the iText book: http://www.1t3xt.com/docs/book.php
> >> >> >
> >> >> >
> >> >>
> >> >> -- 
> >> >> View this message in context:
> >> >
> >
http://www.nabble.com/The-document-has-no-pages.-tp21080269p21092018.html
> >> >> Sent from the iText - General mailing list archive at Nabble.com.
> >> >>
> >> >>
> >>
>
>>> ------------------------------------------------------------------------
-
> > -
> >> > ----
> >> >> _______________________________________________
> >> >> iText-questions mailing list
> >> >> [email protected]
> >> >> https://lists.sourceforge.net/lists/listinfo/itext-questions
> >> >>
> >> >> Buy the iText book: http://www.1t3xt.com/docs/book.php
> >> >>
> >> >
> >> >
> >> >
> >>
>
>> -------------------------------------------------------------------------
-
> > ----
> >> > _______________________________________________
> >> > iText-questions mailing list
> >> > [email protected]
> >> > https://lists.sourceforge.net/lists/listinfo/itext-questions
> >> >
> >> > Buy the iText book: http://www.1t3xt.com/docs/book.php
> >> >
> >> >
> >>
> >> -- 
> >> View this message in context:
> >
http://www.nabble.com/The-document-has-no-pages.-tp21080269p21092802.html
> >> Sent from the iText - General mailing list archive at Nabble.com.
> >>
> >>
>
>> -------------------------------------------------------------------------
-
> > ----
> >> _______________________________________________
> >> iText-questions mailing list
> >> [email protected]
> >> https://lists.sourceforge.net/lists/listinfo/itext-questions
> >>
> >> Buy the iText book: http://www.1t3xt.com/docs/book.php
> >>
> >
> >
> >
>
> --------------------------------------------------------------------------
----
> > _______________________________________________
> > iText-questions mailing list
> > [email protected]
> > https://lists.sourceforge.net/lists/listinfo/itext-questions
> >
> > Buy the iText book: http://www.1t3xt.com/docs/book.php
> >
> >
>
> -- 
> View this message in context:
http://www.nabble.com/The-document-has-no-pages.-tp21080269p21094638.html
> Sent from the iText - General mailing list archive at Nabble.com.
>
>
> --------------------------------------------------------------------------
----
> _______________________________________________
> iText-questions mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/itext-questions
>
> Buy the iText book: http://www.1t3xt.com/docs/book.php
>



------------------------------------------------------------------------------
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php

Reply via email to