Hello - I have the following code:
public Stream GenerateReport(Stream sourceTemplate, IList<IDictionary> reportPages)
{
Settings.Strategy = FileProcessingStrategy.OptimizeMemoryUsage;
IList pages = new List();
reportPages.ToList().ForEach(page =>
{
MemoryStream currentPageOutput = new MemoryStream();
Form form = new Form(sourceTemplate, currentPageOutput);
sourceTemplate.Position = 0;
page.ToList().ForEach(field =>
{
form.FillField(field.Key, field.Value != null ? field.Value : string.Empty);
});
form.FlattenAllFields();
form.Save();
currentPageOutput.Flush();
pages.Add(currentPageOutput);
});
Stream outputStream = new MemoryStream();
PdfFileEditor editor = new PdfFileEditor();
if (pages.Count > 1)
editor.Concatenate(pages.ToArray(), outputStream);
else
return pages[0];
return outputStream;
}
The idea is that I want to pass into this method, a stream containing a PDF form and a list containing a dictionary of field names/values to apply to the form. Each dictionary element in the list represents a separate page in the final PDF. So, first I loop through the list of dictionaries. For each dictionary, I create a new memory stream to store a new PDF based on the template passed into the method. I then loop through each entry in the current dictionary and fill the form fields. I then flatten, save, flush and add that stream to a list. After completing this for all items in the list of dictionaries, I attempt to concatenate the streams in the list of streams and return the resulting stream of all pages.
What I'm seeing is that when I call Concatenate on a list of streams, the resuling PDF is damaged when I open it and view the second page. The PDF itself is fine, but Acrobat reports that it is damaged.
I tried working with only one dictionary, and thus only one PDF and no need to concatenate. I hadded a check at the end of my method to skip the concatenation if there was only one page. When I do this, the resulting PDF is filled with NULL characters, rather than the actual PDF content.
Any insight into what's going on here would be appreciated. I'm attaching my source template, the resulting damaged PDF and the resulting NULL filled PDF.
Thanks!
Chris