iTextSharp Fill Pdf Form Image Field - c#

I created a pdf form with Acrobat DC 2015. I have a image field on it. I fill text field succesfully. But I don't know how to fill image field. Do you help me?
private static void FillPdfForm()
{
// Original File
const string pdfTemplate = #"pdf\form.pdf";
// New file which will be created after fillin PDF
var newFile = #"pdf\FilledCV.PDF";
var pdfReader = new PdfReader(pdfTemplate);
var pdfStamper = new PdfStamper(pdfReader, new FileStream(
newFile, FileMode.Create));
var pdfFormFields = pdfStamper.AcroFields;
// So one of our fields in PDF is FullName I am filling it with my full name
pdfFormFields.SetFieldProperty("01", "textsize", 8f, null);
pdfFormFields.SetField("01", "Example");
// flatten the form to remove editting options, set it to false
// to leave the form open to subsequent manual edits
foreach (var de in pdfReader.AcroFields.Fields)
{
pdfFormFields.SetFieldProperty(de.Key.ToString(),
"setfflags",
PdfFormField.FF_READ_ONLY,
null);
}
pdfStamper.FormFlattening = false;
pdfStamper.FormFlattening = false;
pdfStamper.Close();
}

I fixed it.
private static void FillPdfForm()
{
const string pdfTemplate = #"pdf\form.pdf";
var newFile = #"pdf\FilledCV.PDF";
var pdfReader = new PdfReader(pdfTemplate);
var pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create));
var pdfFormFields = pdfStamper.AcroFields;
string TestImage = #"pdf\test.jpg";
PushbuttonField ad = pdfFormFields.GetNewPushbuttonFromField("08");
ad.Layout = PushbuttonField.LAYOUT_ICON_ONLY;
ad.ProportionalIcon = true;
ad.Image = Image.GetInstance(TestImage);
pdfFormFields.ReplacePushbuttonField("08", ad.Field);
pdfFormFields.SetFieldProperty("01", "textsize", 8f, null);
pdfFormFields.SetField("01", "Example");
foreach (var de in pdfReader.AcroFields.Fields)
{
pdfFormFields.SetFieldProperty(de.Key,"setfflags",PdfFormField.FF_READ_ONLY,null);
}
pdfStamper.FormFlattening = false;
pdfStamper.Close();
}

Related

How to replace PDF field value by Image using iText7 in .NETCore

I've implemented it in iTextSharp but now i want to convert into iText7.
var pdfReader = new PdfReader(pdfPath1);
string outputfile = string.Empty;
var pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create));
var pdfFormFields = pdfStamper.AcroFields;
outputfile = string.Empty;
if (!String.IsNullOrEmpty(sign1))
{
PushbuttonField pushbuttonField = pdfFormFields.GetNewPushbuttonFromField("imgSign1");
pushbuttonField.Layout = PushbuttonField.LAYOUT_ICON_ONLY;
pushbuttonField.ProportionalIcon = true;
pushbuttonField.Image = iTextSharp.text.Image.GetInstance(sign1);
pdfFormFields.ReplacePushbuttonField("imgSign1", pushbuttonField.Field);
}
i've tried to convert it but not getting proper way
var pdfReader = new PdfReader(pdfPath1);
var fstream = new FileStream(newFile, FileMode.Create);
var pdfwriter = new PdfWriter(fstream);
var document = new PdfDocument(pdfReader, pdfwriter);
var acroForm = PdfAcroForm.GetAcroForm(document, true);
outputfile = string.Empty;
if (!string.IsNullOrEmpty(sign1))
{
//what will be the alternative for PushbuttonField in iText7.
}

I want add digital signature in my dynamic pdf with using itext sharp and in signature insert acrofield

I was trying to insert textfield in digtal signature with using itextsharp but not able to insert that text in esignature.
My code please check Also I want verify that signature still not able to do this code any suggestion would be appriciate..
https://imgur.com/SF11oH1
public void Sign(string SigReason, string SigContact, string SigLocation, bool visible)
{
string password = #"123";
PdfReader reader = new PdfReader(this.inputPDF,new System.Text.ASCIIEncoding().GetBytes(password));
//Activate MultiSignatures
PdfStamper st = PdfStamper.CreateSignature(reader, new FileStream(this.outputPDF, FileMode.Create, FileAccess.Write), '\0', null, true);
//To disable Multi signatures uncomment this line : every new signature will invalidate older ones !
//PdfStamper st = PdfStamper.CreateSignature(reader, new FileStream(this.outputPDF, FileMode.Create, FileAccess.Write), '\0');
//string pdfTemplate = Server.MapPath("~/Data/PADForm.pdf");
//string newFile = Server.MapPath("~/Data/newFile.pdf");
//signPdfFile(sourceDocument, destinationPath, stream, "123", "testing", "testing");
//PdfReader pdfReader = new PdfReader(pdfTemplate, new System.Text.ASCIIEncoding().GetBytes(password));
//PdfStamper pdfStamper = new PdfStamper(reader, new FileStream(this.outputPDF, FileMode.Create));
string imageURL = #"C:\Users\amitrs\Desktop\FSI SS\204-25.png";
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageURL);
// Resize image depend upon your need
jpg.ScaleToFit(140f, 120f);
//Give space before image
jpg.SpacingBefore = 10f;
//Give some space after the image
jpg.SpacingAfter = 1f;
jpg.Alignment = Element.ALIGN_LEFT;
AcroFields pdfFormFields = st.AcroFields;
pdfFormFields.SetField("DigitalSignature", "Amit");
pdfFormFields.SetField("Signature", "Amit Saini");
st.MoreInfo = this.metadata.getMetaData();
st.XmpMetadata = this.metadata.getStreamedMetaData();
PdfSignatureAppearance sap = st.SignatureAppearance;
sap.SetCrypto(this.myCert.Akp, this.myCert.Chain, null, PdfSignatureAppearance.VERISIGN_SIGNED);
sap.Reason = SigReason;
sap.Contact = SigContact;
sap.Location = SigLocation;
if (visible)
sap.SetVisibleSignature(new iTextSharp.text.Rectangle(100, 100, 250, 300), 1, null);
st.Close();
}
iTextSharp signature appearance object have 2 property, you have to set property.
PdfSignatureAppearance sap = st.SignatureAppearance;
sap.Layer2Text = "XYZ";
Hope this helps

How can I save as word.docx in open xml

I need to open a template, add some text, and then save it with a new name.
How can I save it with a new name?
String Dir = #"F:\template.docx";
WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(Dir, true);
Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
Paragraph paragraph1 = body.AppendChild(new Paragraph());
Run run1 = new Run();
RunProperties runProperties1 = new RunProperties();
Bold bold2 = new Bold();
FontSize fontSize2 = new FontSize() { Val = "28" };
runProperties1.Append(bold2);
runProperties1.Append(fontSize2);
Text text1 = new Text();
text1.Text = "text";
run1.Append(runProperties1);
run1.Append(text1);
paragraph1.Append(Hearder1);
paragraph1.Append(run1);
wordprocessingDocument.Close();
byte[] byteArray = File.ReadAllBytes("D:\Document.docx");
using (var stream = new MemoryStream())
{
stream.Write(byteArray, 0, (int)byteArray.Length);
using (var document = WordprocessingDocument.Open(stream, true))
{
//Here fill document
}
// And save file
File.WriteAllBytes("D:\NewDocument.docx", stream.ToArray());
}

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?

Itext Sharp Merge Pdfs with acrofields

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.

Categories