How to fill an existing PDF document programmatically

I have a PDF document which wasn’t created using Adobe LifeCycle Designer. What I am looking to do is pre-fill out the common fields in the document.

I have looked into this many of the options available to me from using iTextSharp and PDFSharp but unsure of how to use it correctly.

Read More

I recently came across this article: FillPDF which has a good documentation but didn’t match with what I was creating.

I also was reading about iTextSharp which can be imported in VS and used but I am not sure where to start. I saw a lot of tutorials but none of them describe how to start.

Please help…

Related posts

Leave a Reply

1 comment

  1. I recently worked on a huge project with itextsharp

    http://www.mikesdotnetting.com/Article/88/iTextSharp-Drawing-shapes-and-Graphics
    http://www.mikesdotnetting.com/Article/81/iTextSharp-Working-with-Fonts

    here are some things to get you started

    but as far as reading from a pdf and then outputting back
    you will need some regex to help you with that.

    here is one of the sample code i have ( this creates a header or footer on every new page event )

    using CMS;
    using CMS.Tags;
    using CMS.Pages;
    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Linq;
    using System.Globalization;
    using System.Net;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using iTextSharp.text;
    using iTextSharp.text.html;
    using iTextSharp.text.xml;
    using iTextSharp.text.pdf;
    using iTextSharp.text.pdf.draw;
    using System.IO;
    using System.Text.RegularExpressions;
    using System.Text;
    using System.util;
    
    public class pdfPage : iTextSharp.text.pdf.PdfPageEventHelper
    {
    public override void OnEndPage(PdfWriter writer, Document doc)
        {
            PdfContentByte cb = writer.DirectContent;
            cb.SetLineWidth(1f);
            cb.SetCMYKColorStroke(66, 59, 57, 38);
            cb.MoveTo(30, 55);     
            cb.LineTo(doc.PageSize.Width - 30 , 55);     
            cb.Stroke();
            cb.MoveTo(185, 80);
            cb.LineTo(185, 25);
            cb.Stroke();
    
            ColumnText ct = new ColumnText(cb);
            BaseFont bfTimes = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, false);
            Font times = new Font(bfTimes, 10);
            times.SetColor(90, 90, 90);
            ct.SetSimpleColumn(new Phrase("text text", times), 60, 60, 175, 78, 15, Element.ALIGN_MIDDLE);
            ct.Go();
            times.SetColor(1, 73, 144);
            ct.SetSimpleColumn(new Phrase("text textn", times), 60, 38, 175, 55, 15, Element.ALIGN_MIDDLE);
            ct.Go();
            times.SetColor(90, 90, 90);
            ct.SetSimpleColumn(new Phrase("text here", times), 190, 60, doc.PageSize.Width - 32 , 78, 15, Element.ALIGN_RIGHT);
            ct.Go();
            times.SetColor(90, 90, 90);
            ct.SetSimpleColumn(new Phrase("text here", times), 190, 38, doc.PageSize.Width - 32 , 55, 15, Element.ALIGN_RIGHT);
            ct.Go();
        }
    }
    

    this is part of my code that starts a pdf

    using (var ms = new MemoryStream())
            {
                using (var doc = new Document(PageSize.LETTER, 220f, 30f, 115f, 100f)){
    
                    try
                    {
                      pdfPage page = new pdfPage();
                      PdfWriter writer = PdfWriter.GetInstance(doc, ms);
                      writer.PageEvent = page;
                      doc.Open();
    
                    iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(RESOURCE);
                    img.ScalePercent(49f);
                    //img.Width = doc.PageSize.Width;
                    //img.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
                    img.SetAbsolutePosition(-8, 
                        doc.PageSize.Height - 180.6f);
                    doc.Add(img);
    

    and this is my output code ( as a download pdf directly created from server *notsaved on server )

    }
                    catch (Exception ex)
                    {
                      //Log error;
                    }
                    finally
                    {
                      doc.Close();
                    }
    
                }
                Response.Clear();
                  //Response.ContentType = "application/pdf";
                  Response.ContentType = "application/octet-stream";
                  Response.AddHeader("content-disposition", "attachment;filename= Company " + namefile + ".pdf");
                  Response.Buffer = true; 
                  Response.Clear();
                  var bytes = ms.ToArray();
                  Response.OutputStream.Write(bytes, 0, bytes.Length);
                  Response.OutputStream.Flush();
    
            }