I have a piece of code that is attempting to add a text to a text field with
PdfStamper. I need to save the new file over the old one so I use a
MemoryStream. Everything works fine but the PDF is corrupt or the text is
missing.

When I use the following code the PDF is corrupt. It was also very small in
size so I thought that perhaps I should append to the old one instead of
saving it on its own. That didn't work either (the text was missing).

Below, `ret` becomes true (so everything seems OK) but, when saving the
memory stream, things are weird.

 

 

                 public void AddSignerComment(BaseDocumentFile pdf, string
comment)

                 {

                          var fileMSer =
IoC.GetInstance<Services.IFileManagementService>();

                          using (var memStrm = new MemoryStream(70000))

                          {

                                   // Open the file for read.

                                   using (var rStrm =
fileMSer.GetExistingTempFileStream(pdf, FileMode.Open, FileAccess.Read))

                                   using (var pdfReader = new
PdfReader(rStrm))

                                   {

                                            var pdfStamper = new
PdfStamper(pdfReader, memStrm);

                                            AcroFields fields =
pdfStamper.AcroFields;

                                            var blankSign =
fields.GetBlankSignatureNames();

                                            if (blankSign.Count > 0)

                                            {

                                                    var name = blankSign[0];

                                                     var indexStr =
name.Substring(SignatureFieldName.Length);

                                                     bool ret =
fields.SetField(TextFieldName + indexStr, comment, comment);

                                            }

                                            pdfReader.Close();

                                            //pdfStamper.Close(); // iText
closes the memory stream...!

                                   }

                                   // Overwrite the file for write and save
the mem. stream.

                                   using (var rStrm =
GetExistingTempFileStream(pdf, FileMode.Create, FileAccess.Write)

                                   {

                                            memStrm.Position = 0;

                                            fileMSer.CopyStream(memStrm,
rStrm);

                                   }

                          }

                 }

 

Any ideas on why the output file is small and corrupt?
I've spent a whole day fighting with this one. L

 

Thanks in advance,
Kostas.

------------------------------------------------------------------------------
How ServiceNow helps IT people transform IT departments:
1. A cloud service to automate IT design, transition and operations
2. Dashboards that offer high-level views of enterprise services
3. A single system of record for all IT processes
http://p.sf.net/sfu/servicenow-d2d-j
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
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