Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
0


I have a pdf template with below content

First Name: {{FirstName}}
Contact Number: {{ContactNumber}}
Email Id: {{EmailId}}
Address 1: {{Address1}}
Address 2: {{Address2}}
Qualification: {{Qualification}}


Using itextsharp i need to replace the texts which is inside the curly braces to its actual texts. 

But when i check the generated pdf, the texts in the {{}} are NOT getting replaced. Any idea why?

Update: i heard to know that AcroFields not a simple text, its a form field. Can anyone tell me how to create this acrofields in my template?


What I have tried:

So for that i have coded like this

                    string formFile = path;

                    PdfReader reader = new PdfReader(formFile);
                    MemoryStream stream = new MemoryStream();
                    PdfStamper stamper = new PdfStamper(reader, stream);
                    AcroFields fields = stamper.AcroFields;

                    fields.SetField("{{FirstName}}", objUser.FirstName);
                    fields.SetField("{{ContactNumber}}", objUser.EmailId);
                    fields.SetField("{{Address1}}", objUser.AddressLine1);
                    fields.SetField("{{Address2}}", objUser.AddressLine2);
                    fields.SetField("{{Qualification}}", objUser.Education);
                    fields.SetField("{{AppNumber}}", "01A2021"+objUser.UserId.ToString());

                    stamper.Writer.CloseStream = false;

                    stamper.FormFlattening = true;
                    stamper.Close();

                    stream.Position = 0;
and then created my email attachment using

                    System.Net.Mime.ContentType contentType = new 
                    System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Application.Pdf);
                    var coverAttachment = new Attachment(stream, contentType);
                    coverAttachment.ContentDisposition.FileName = "CoverLetter.pdf";
                    emailMessage.Attachments.Add(coverAttachment);
Posted
Updated 26-Feb-21 22:53pm
v3
Comments
[no name] 27-Feb-21 12:38pm    
https://www.codeproject.com/Tips/679606/Filling-PDF-Form-using-iText-PDF-Library

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900