Why don't you write both at the same time? Either create your own Stream that duplicates the data to 2 other streams, or maybe C#/.Net natively has something like that.

(that's how I would do it in Java )

On 25/06/2011 13:15, Karsten Brandt wrote:
Hello,

i've created an pdf by using an template. I do it with the PdfReader and
the PdfStamper by creating a memorystream. Sending the Stream to the
Client works very well.
But I need to convert the memorystream to an filestream for other
purposes (e.g. to save to local disk). How can I do that?
The code I posted below didn't work. The file is corrupt.

My Code:

              bool error;
              MemoryStream ms = new MemoryStream();
              MemoryStream ms1 = new MemoryStream();

              // read existing PDF document, optimize memory
              PdfReader reader = new PdfReader(new
RandomAccessFileOrArray(PDFTemplate),null);
              if (reader != null)
              {
                  // add content to existing PDF document with PdfStamper
                  PdfStamper stamper = null;
                  Boolean res;

                      stamper = new PdfStamper(reader, ms);

                      if (stamper != null)
                      {
                          //Fill out Template
                          try
                          {
                              // retrieve properties of PDF form
w/AcroFields object
                              AcroFields af = stamper.AcroFields;
                              // fill in PDF fields by parameter
                              error |= af.SetField("Field1", string1);
                              error |= af.SetField("Field2", string2);

                              // make resultant PDF read-only for end-user
                              stamper.FormFlattening = true;
                              //copy stream to ms1
                              ms.WriteTo(ms1);
                              stamper.Close();
                              reader.Close();

                              //Write to File
                              FileStream fs = File.Create("C:\\Test.pdf");
                              ms1.WriteTo(fs);
                              fs.Close();
                          }
                      }
              }

best regards

Karsten


--
@redlabbe <http://twitter.com/redlabbe>
redlab-log <http://www.redlab.be/blog>
------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security 
threats, fraudulent activity, and more. Splunk takes this data and makes 
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to