I'm generating a new PDF from an existing template that has been created in LibreOffice. It contains one Text Box.
After the code compiles and successfully saves the PDF to a new file, if I open the newly created document in Acrobat Reader XI, it renders correctly, but, even if I don't modify the final document, upon closing the document, it asks "Do you want to save changes to "filename.pdf" before closing?"
I've read other posts on StackOverflow and their official site (iTextSharp), and found a solution, that maybe I'm implementing in a wrongful manner.
public string spdftemplate = @"C:\test\input.pdf";
public string newFile = @"C:\test\output.pdf";
private void FillFormsProperly()
{
PdfReader reader = new PdfReader(spdftemplate);
byte[] bytes;
using (MemoryStream ms = new MemoryStream())
{
PdfStamper stamper = new PdfStamper(reader, ms);
#region ForTesting
//PdfContentByte cb = stamper.GetOverContent(1);
//ColumnText ct = new ColumnText(cb);
//ct.SetSimpleColumn(100, 100, 500, 200);
//ct.AddElement(new Paragraph("This was added using ColumnText"));
//ct.Go();
#endregion ForTesting
AcroFields pdfFormFields = stamper.AcroFields;
foreach (DictionaryEntry de in reader.AcroFields.Fields)
{
pdfFormFields.SetField(de.Key.ToString(), "test"); //"Text Box 1"
}
//string sTmp = "W-4 Completed for " + pdfFormFields.GetField("Text Box 1");
//MessageBox.Show(sTmp, "Finished");
//Flush the PdfStamper's buffer
stamper.FormFlattening = true;
stamper.Close();
//Get the raw bytes of the PDF
bytes = ms.ToArray();
}
//Do whatever you want with the bytes
//Below I'm writing them to disk
using (FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
fs.Write(bytes, 0, bytes.Length);
}
}
The best answer I found was this : creating a pdf from a template in itextsharp and outputting as content disposition.
The above code is my (copy-paste more or less) implementation.
It's obvious that the file is corrupted (but still readable), how can I fix this ?