0

I'm using iTextSharp for generation PDF file.Here is the code.

 public static byte[] GeneratePDF(string pdfPath, Dictionary<string, string> formFieldMap)
            {
                var output = new MemoryStream();
                var reader = new PdfReader(pdfPath);
                var stamper = new PdfStamper(reader, output);
                var formFields = stamper.AcroFields;
                foreach (var fieldName in formFieldMap.Keys)
                {
                    if (formFieldMap[fieldName] != null)
                    {
                        formFields.SetField(fieldName, formFieldMap[fieldName]);
                    }
                }
                stamper.FormFlattening = true;
                stamper.Close();
                reader.Close();

                return output.ToArray();
            }

Where,

  1. pdfPath - Where to download in Local System.

  2. Dictionary formFieldMap - Having PDF Field Id,Value to show in Field.


In output.ToArray(); I'm getting Values like below.

enter image description here

PDF is downloading, but if I try to open I get "File is Corrupted". Can I get help to resolve this issue?

Amedee Van Gasse
  • 7,280
  • 5
  • 55
  • 101
kavie
  • 2,154
  • 4
  • 28
  • 53
  • Apart from the answer to the question I used to close yours, you may also want to read [Getting PdfStamper to work with MemoryStreams (c#, itextsharp)](https://stackoverflow.com/a/27209002/1622493) and [Using iTextSharp's PdfStamper...](https://stackoverflow.com/a/9098077/1622493). The culprit is using the `ToArray()` method on a `MemoryStream` that is already closed. – Bruno Lowagie Jul 20 '17 at 10:52
  • now getting WriteTimeout = 'output.WriteTimeout' threw an exception of type 'System.InvalidOperationException' & ReadTimeout = 'output.ReadTimeout' threw an exception of type 'System.InvalidOperationException' – kavie Jul 20 '17 at 10:56
  • That's not an iText problem. Reproduce the problem without using iTextSharp (e.g. read bytes from `pdfPath` and write them to `output` without using `PdfReader` and `PdfStamper`). That should reproduce the problem. Post that problem as a different question (not tagged as an iText question). – Bruno Lowagie Jul 20 '17 at 10:59

0 Answers0