I can use PdfStamper to populate a blank PDF form with data values, and the result document can be changed and saved by the user. When I needed to stream 2 copies of the same document with different data as a single download, I modified my code to use PdfCopyFields to merge the two documents, which works, but the result document can't be saved. It can be modified however.
Has anyone ever gotten this to work? Is there a better way to do this?
Here's the code...
var pc = new PdfCopyFields(Response.OutputStream, '\0');
using (var ms = new MemoryStream())
{
var pdfReader1 = new PdfReader(templateFile);
var pdfs = new PdfStamper(pdfReader1, ms, '\0', true);
// populate fields here using pdfs.AcroFields);
pdfs.FormFlattening = false;
pdfs.Close();
var rdr2 = new PdfReader(ms.ToArray());
pc.AddDocument(rdr2);
rdr2.Close();
}
using (var ms = new MemoryStream())
{
var pdfReader1 = new PdfReader(templateFile);
var pdfs = new PdfStamper(pdfReader1, ms, '\0', true);
// populate fields here using pdfs.AcroFields);
pdfs.FormFlattening = false;
pdfs.Close();
var rdr2 = new PdfReader(ms.ToArray());
pc.AddDocument(rdr2);
rdr2.Close();
}
pc.Close();