itextsharp with c#

ITsolutionWizard
ITsolutionWizard used Ask the Experts™
on
Below codes are working to me. but I would like to populate field on pdf.
like I have doc.pdf and have field name call 'firstname' and I want to populate value john.
How can I do that?


  protected void Page_Load(object sender, EventArgs e)
        {
ShowPdf(CreatePDF());
}
  private byte[] CreatePDF()
        {
            Document doc = new Document(PageSize.LETTER, 50, 50, 50, 50);

            using (System.IO.MemoryStream output = new MemoryStream())
            {
                PdfWriter wri = PdfWriter.GetInstance(doc, output);
                doc.Open();
                Paragraph header = new Paragraph("End Sheet") { Alignment = Element.ALIGN_CENTER };
                Paragraph paragraph = new Paragraph("Testing the iText pdf.");
                Phrase phrase = new Phrase("This is a phrase but testing some formatting also. \nNew line here.");
                Chunk chunk = new Chunk("This is a chunk.");
                doc.Add(header);
                doc.Add(paragraph);
                doc.Add(phrase);
                doc.Add(chunk);             
                doc.Close();
                return output.ToArray();
            }
        }
        private void ShowPdf(byte[] strS)
        {
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now);
            Response.BinaryWrite(strS);
            Response.End();
            Response.Flush();
            Response.Clear();
        }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Any helps?
Commented:
Try this piece of code -

        protected void Page_Load(object sender, EventArgs e)
        {

            ShowPdf(CreatePDF());
        }
        private byte[] CreatePDF()
        {

            var output = new MemoryStream();

            var reader = new PdfReader(("your WorkDirectory " + "\\" + "doc.pdf"));

            var stamper = new PdfStamper(reader, output);

            AddContent(stamper);

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

            return output.ToArray();

        }


        void AddContent(PdfStamper stamper)
        {

            var form = stamper.AcroFields;

            form.SetField("firstname", "John");
        }

        private void ShowPdf(byte[] strS)
        {
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now);
            Response.BinaryWrite(strS);
            Response.End();
            Response.Flush();
            Response.Clear();
        }

Open in new window

Author

Commented:
Appreciate it. Work perfectly fine.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial