Hello,
thanks for your reply. I've found a solution. It is very important to
close the stamper before creating the memorystream. I thing the stamper
must closed to get the correct stamper stream. If I write the stamper
Stream to file before closing the stamper the file is corrupted.
I did the follwing:
1) close stamper:
MemoryStream stamperStream=newMemoryStream();
PdfStamper stamper = new PdfStamper(reader, stamperStream);
.
//do something
.
stamper.close();
2) write the memory stream created by the stamper in another memorystream:
MemoryStream anotherStream=new MemoryStream();
anotherStream.WriteTo(stamperStream.ToArray(),0,stamperStream.ToArray().Length);
now I can use the anotherStream do to something. Works perfectly.
I would be usefull to put this in your documentation.
Karsten
LISA office:
fon: +49-5556-9938-0
fax: +49-5556-9938-10
mail: [email protected]
web : www.lisalaser.de
--------------------------------------------------------------------------
LISA laser products OHG
Fuhrberg& Teichmann
Max-Planck-Str. 1
D- 37191 Katlenburg-Lindau
Germany
Amtsgericht Göttingen HRA 130261
UST-ID-Nr:DE116210331
Am 26.06.2011 10:48, schrieb Balder:
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
------------------------------------------------------------------------------
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