Itext Sharp Merge Pdfs with acrofields - c#

I'm using itext sharp to fill my form fields on my template with values.
I created the template using pdfescape.com
Here is my code that I use to place the values in the pdf template.
private static byte[] GeneratePdf(Dictionary<String, String> formKeys, String pdfPath)
{
var templatePath = System.Web.HttpContext.Current.Server.MapPath(pdfPath);
var reader = new PdfReader(templatePath);
var outStream = new MemoryStream();
var stamper = new PdfStamper(reader, outStream);
var form = stamper.AcroFields;
var fieldKeys = form.Fields.Keys;
// "Flatten" the form so it wont be editable/usable anymore
// stamper.FormFlattening = true;
foreach (KeyValuePair<String, String> pair in formKeys)
{
if (fieldKeys.Any(f => f == pair.Key))
{
form.SetField(pair.Key, pair.Value);
form.SetFieldProperty(pair.Key, "setfflags", PdfFormField.FF_READ_ONLY, null);
}
}
stamper.Close();
reader.Close();
return outStream.ToArray();
}
I first used the stamper.FormFlattening = true, but then the values weren't visible. So Instead of using the form flattening I just set the values as ready only and everything works fine.
Now I want to merge multiple of these pdf files using the the pdf merger by smart-soft
Once the merging is complete the values aren't visible. When I highlight over the form it highlights all the text, but I can't read it.I did research on this and read that the fields need to be flattened.
Here is an image of how it looks on the pdf when I highlight everything:
I don't know why my fields aren't visible when they are flattened, even if I don't use the merger. Is there something wrong with the code or the template? Alternatives will also be appreciated.
Btw my project is an asp-mvc project if that is relevant.
EDIT
I added the following code so that I first read the template, write the values to the form fields, close it, reopen it, flatten and then close it again as suggested by one of the comments. I just pass the result I get from the GeneratePdf function to this function:
private static byte[] flattenPdf(byte[] pdf)
{
var reader = new PdfReader(pdf);
var outStream = new MemoryStream();
var stamper = new PdfStamper(reader, outStream);
stamper.FormFlattening = true;
stamper.Close();
reader.Close();
return outStream.ToArray();
}
I still get the same result

I found a solution to this problem thanks to this answer by rhens
All I had to do is modify my GeneratePdf function by adding one line:
form.GenerateAppearances = true;
Here is the end result:
private static byte[] GeneratePdf(Dictionary<String, String> formKeys, String pdfPath)
{
var templatePath = System.Web.HttpContext.Current.Server.MapPath(pdfPath);
var reader = new PdfReader(templatePath);
var outStream = new MemoryStream();
var stamper = new PdfStamper(reader, outStream);
var form = stamper.AcroFields;
form.GenerateAppearances = true; //Added this line, fixed my problem
var fieldKeys = form.Fields.Keys;
foreach (KeyValuePair<String, String> pair in formKeys)
{
if (fieldKeys.Any(f => f == pair.Key))
{
form.SetField(pair.Key, pair.Value);
}
}
stamper.Close();
reader.Close();
return flattenPdf(outStream.ToArray());
}
and the flattenPdf stays the same as in my question.

Related

iTextSharp - Merging a Batch of PDF Byte Arrays

I am using iTextSharp to fill a few fields on a pdf. I need to be able to combine a series of these pdfs into one single batch pdf file. Below I am looping through a SQL result set, filling the fields of the pdf with values corresponding to the current record, storing that as a byte array, and consolidating all of those into a list of byte arrays. I am then attempting to merge each byte array in that list into a single byte array, and serve that as a pdf to the user.
It seems to work, generating a single file containing presumably as many individual pages as were in my result set, but with all the fields blank on each page. It works as expected when using FillForm() to serve a single pdf. What am I doing wrong?
byte[] pdfByteArray = new byte[0];
List<byte[]> pdfByteArrayList = new List<byte[]>();
byte[] pdfByteArrayItem = new byte[0];
foreach (DataRow row in results.Rows)
{
certNum = row[1].ToString();
certName = row[2].ToString();
certDate = row[3].ToString();
pdfByteArrayItem = FillForm(certType, certName, certNum, certDate);
pdfByteArrayList.Add(pdfByteArrayItem);
}
using (var ms = new MemoryStream()) {
using (var doc = new Document()) {
using (var copy = new PdfSmartCopy(doc, ms)) {
doc.Open();
//Loop through each byte array
foreach (var p in pdfByteArrayList) {
//Create a PdfReader bound to that byte array
using (var reader = new PdfReader(p)) {
//Add the entire document instead of page-by-page
copy.AddDocument(reader);
}
}
doc.Close();
}
}
pdfByteArray = ms.ToArray();
context.Response.ContentType = "application/pdf";
context.Response.BinaryWrite(pdfByteArray);
context.Response.Flush();
context.Response.End();
private byte[] FillForm(string certType, string certName, string certNum, string certDate)
{
string pdfTemplate = string.Format(#"\\filePath\{0}.pdf", certType);
PdfReader pdfReader = new PdfReader(pdfTemplate);
MemoryStream stream = new MemoryStream();
PdfStamper pdfStamper = new PdfStamper(pdfReader, stream);
AcroFields pdfFormFields = pdfStamper.AcroFields;
// set form pdfFormFields
pdfFormFields.SetField("CertName", certName);
pdfFormFields.SetField("CertNum", certNum);
pdfFormFields.SetField("CertDate", certDate);
// flatten the form to remove editting options, set it to false
// to leave the form open to subsequent manual edits
pdfStamper.FormFlattening = false;
// close the pdf
pdfStamper.Close();
stream.Flush();
stream.Close();
byte[] pdfByte = stream.ToArray();
return pdfByte;
}
Adding the line below after setting the value for the fields seems to have fixed it:
pdfFormFields.GenerateAppearances = true;

How can I add PdfTable to an existing pdf document with iTextSharp

Here is the way I open an existing pdf and add some values for the fields:
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)
formFields.SetField(fieldName, formFieldMap[fieldName]);
stamper.FormFlattening = true;
stamper.Close();
reader.Close();
return output.ToArray();
}
Can you tell me how can I add a PdfTable object in this context?

Fill PDF Form with dynamic values using ASP.Net

I am stuck here. actually I am trying to fill a PDF form using asp.net. I get some help and write the following code:
private void fillForm()
{
try
{
string formFile = Server.MapPath("") + #"\Forms\fw4.pdf";
string savepath = Server.MapPath("") + #"\Forms\new_fw4.pdf";
PdfReader pdfReader = new PdfReader(formFile);
using (FileStream stream = new FileStream(savepath, FileMode.Create))
{
PdfStamper pdfStamper = new PdfStamper(pdfReader, stream);
AcroFields formFields = pdfStamper.AcroFields;
foreach (DictionaryEntry de in formFields.Fields)
{
formFields.SetField("field name", "field value");
}
pdfStamper.FormFlattening = true;
pdfStamper.Close();
}
}
catch
{
}
}
I want the program to show all fields in a List.
I am unable to iterate all available fields using the foreach loop. Its giving me this error:
Cannot convert type System.Collections.Generic.KeyValuePair<string,iTextSharp.text.pdf.AcroFields.Item> to System.Collections.DictionaryEntry
any help would be greatly appreciated.
As you have updated KeyValuePair try using item.Key & item.Value

Populate Multiple PDFs

I am using itextsharp to populate my PDFs. I have no issues with this. Basically what I am doing is getting the PDF and populating the fields in memory then passing back the MemoryStream to be displayed on a webpage. All this is working with a single document PDF.
What I am trying to figure out now, is merging multiple PDFs into one MemoryStream. The part I cant figure out is, the documents I am populating are identical. So for example, I have a List<Person> that contains 5 persons. I want to fill out a PDF for each person and merge them all into one, in memory. Bare in mind I am going to fill out the same type of document for each person.
The problem I am getting is that when I try to add a second copy of the same PDF to be filled out for the second iteration, it just overwrites the first populated PDF, since it's the same document, therefore not adding a second copy for the second Person at all.
So basically if I had the 5 people, I would end up with a single page with the data of the 5th person, instead of a PDF with 5 like pages that contain the data of each person respectively.
Here's some code...
MemoryStream ms = ms = new MemoryStream();
PdfReader docReader = null;
PdfStamper Stamper = null;
List<Person> persons = new List<Person>() {
new Person("Larry", "David"),
new Person("Dustin", "Byfuglien"),
new Person("Patrick", "Kane"),
new Person("Johnathan", "Toews"),
new Person("Marian", "Hossa")
};
try
{
// Iterate thru all persons and populate a PDF for each
foreach(var person in persons){
PdfCopyFields Copier = new PdfCopyFields(ms);
Copier.AddDocument(GetReader("Person.pdf"));
Copier.Close();
docReader = new PdfReader(ms.ToArray());
Stamper = new PdfStamper(docReader, ms);
AcroFields Fields = Stamper.AcroFields;
Fields.SetField("FirstName", person.FirstName);
}
}catch(Exception e){
// handle error
}finally{
if (Stamper != null)
{
Stamper.Close();
}
if (docReader != null)
{
docReader.Close();
}
}
I have created a working solution, I hope this helps someone along the way.
Create a PopulatePDF() method that takes the Person object and returns a byte[]:
private byte[] PopulatePersonPDF(Person obj)
{
MemoryStream ms = new MemoryStream();
PdfStamper Stamper = null;
try
{
PdfCopyFields Copier = new PdfCopyFields(ms);
Copier.AddDocument(GetReader("Person.pdf"));
Copier.Close();
PdfReader docReader = new PdfReader(ms.ToArray());
ms = new MemoryStream();
Stamper = new PdfStamper(docReader, ms);
AcroFields Fields = Stamper.AcroFields;
Fields.SetField("FirstName", obj.FirstName);
}
finally
{
if (Stamper != null)
{
Stamper.Close();
}
}
return ms.ToArray();
}
Create a MergePDFs() method that returns the MemoryStream:
private MemoryStream MergePDFs(List<byte[]> pdfs)
{
MemoryStream ms = new MemoryStream();
PdfCopyFields Copier = new PdfCopyFields(ms);
foreach (var pdf in pdfs)
Copier.AddDocument(new PdfReader(pdf));
Copier.Close();
return ms;
}
Example Implementation:
List<Person> persons = new List<Person>() {
new Person("Larry", "David"),
new Person("Dustin", "Byfuglien"),
new Person("Patrick", "Kane"),
new Person("Johnathan", "Toews"),
new Person("Marian", "Hossa")
};
List<byte[]> pdfs = new List<byte[]>();
foreach(var person in persons)
pdfs.Add(PopulatePersonPDF(person));
MemoryStream ms = MergePDFs(pdfs);
Check the PdfStamper constructor signature there is an overload that takes a boolean value that tells it to append to the current document.
here might be another answer to your solution: Batch Pdf generation

How to flatten already filled out PDF form using iTextSharp

I'm using iTextSharp to merge a number of pdf files together into a single file.
I'm using method described in iTextSharp official tutorials, specifically here, which merges files page by page via PdfWriter and PdfImportedPage.
Turns out some of the files I need to merge are filled out PDF Forms and using this method of merging form data is lost.
I've see several examples of using PdfStamper to fill out forms and flatten them.
What I can't find, is a way to flatten already filled out PDF Form and hopefully merge it with the other files without saving it flattened out version first.
Thanks
Just setting .FormFlattening on PdfStamper wasn't quite enough...I ended up using a PdfReader with byte array of file contents that i used to stamp/flatten the data to get the byte array of that to put in a new PdfReader. Below is how i did it. works great now.
private void AppendPdfFile(FileDTO file, PdfContentByte cb, iTextSharp.text.Document printDocument, PdfWriter iwriter)
{
var reader = new PdfReader(file.FileContents);
if (reader.AcroForm != null)
reader = new PdfReader(FlattenPdfFormToBytes(reader,file.FileID));
AppendFilePages(reader, printDocument, iwriter, cb);
}
private byte[] FlattenPdfFormToBytes(PdfReader reader, Guid fileID)
{
var memStream = new MemoryStream();
var stamper = new PdfStamper(reader, memStream) {FormFlattening = true};
stamper.Close();
return memStream.ToArray();
}
When creating the files to be merged, I changed this setting:
pdfStamper.FormFlattening = true;
Works Great.
I think this problem is same with this one: AcroForm values missing after flattening
Based on the answer, this should do the trick:
pdfStamper.FormFlattening = true;
pdfStamper.AcroFields.GenerateAppearances = true;
This is the same answer as the accepted one but without any unused variables:
private byte[] GetUnEditablePdf(byte[] fileContents)
{
byte[] newFileContents = null;
var reader = new PdfReader(fileContents);
if (reader.AcroForm != null)
newFileContents = FlattenPdfFormToBytes(reader);
else newFileContents = fileContents;
return newFileContents;
}
private byte[] FlattenPdfFormToBytes(PdfReader reader)
{
var memStream = new MemoryStream();
var stamper = new PdfStamper(reader, memStream) { FormFlattening = true };
stamper.Close();
return memStream.ToArray();
}

Categories