Using iTextsharp to edit PDF

//Using iTextsharp to edit PDF

iTextSharp is a free open source PDF library that can enable your applications to, generate documents and reports, split or merge PDF documents, fill out forms and generally manipulate PDF documents such as password protecting a document or changing the permissions.
Below I describe the process I follow when I have PDF forms that are flattened and I need to fill them out but no acrofields are present. The only way I can do it without buying any tool is to either add acrobat fields to the document or save myself time and add directly the text I need to the document.
I choose the later because in both cases I will need to find the right position in the document to add the acrobat fields to the PDF or add the text. I will provide source code for both.
First I need is GIMP to find the right positions where I want to add my fields or the text. Be aware that GIMP is not aware of the PDF coordinate system, which starts at the lower left of each page. So after I import the page I flip it vertically and switch the metric system to points.

Once I have all the positions I need, I simply write my program to start manipulating the PDF.

Example for adding fields to the PDF

namespace AddAcroFieldsToPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfReader reader = new PdfReader(@"C:\test\pdf\input.pdf");
            PdfStamper stamper = new PdfStamper(reader, new FileStream(@"C:\test\pdf\fieldsAdded.pdf", FileMode.OpenOrCreate));

            AddAcroFieldToPdf(ref stamper, "txtMyField", new Rectangle(96f, 639f, 287f, 626f), page);
            stamper.Close();
            reader.Close();

            reader = new PdfReader(@"C:\test\pdf\fieldsAdded.pdf");
            stamper = new PdfStamper(reader, new FileStream(@"C:\test\pdf\output.pdf", FileMode.OpenOrCreate));
            SetAcroFieldValue(ref stamper, "txtMyField", "kalomeister.net");
            stamper.Close();
            stamper.Dispose();
            reader.Close();
            reader.Dispose();
        }

        private static void AddAcroFieldToPdf(ref PdfStamper stamper, string acroFieldKey, Rectangle rect, int page)
        {

            PdfContentByte pbover = stamper.GetOverContent(page);

            TextField field = new TextField(stamper.Writer, rect, acroFieldKey);
            field.BorderColor = BaseColor.RED;
            stamper.AddAnnotation(field.GetTextField(), page);
        }


        private static void SetAcroFieldValue(ref PdfStamper stamper, string acroFieldKey, string text)
        {
            stamper.AcroFields.SetField(acroFieldKey, text);
        }
    }
}

 

Example for adding text directly to PDF

using System.IO;
using iTextSharp.text.pdf;
using iTextSharp.text;
using iTextSharp.awt.geom;

namespace AddAcroFieldsToPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfReader reader = new PdfReader(@"C:\test\pdf\input.pdf");
            PdfStamper stamper = new PdfStamper(reader, new FileStream(@"C:\test\pdf\output.pdf", FileMode.Create));

            AddTextToPDF(ref stamper, "kalomeister.net", new Point(96, 639), 2);

            stamper.Close();
            stamper.Dispose();
            reader.Close();
            reader.Dispose();
        }

        private static void AddTextToPDF(ref PdfStamper stamper, string text, Point point, int page)
        {
            PdfContentByte pbover = stamper.GetOverContent(2);
            ColumnText.ShowTextAligned(pbover, Element.ALIGN_LEFT, new Phrase(text, new Font { Size = 12 }), (float)point.x, (float)point.y, 0);

        }
    }
}
2017-05-08T13:58:45+00:00 May 8th, 2017|C#|0 Comments

Leave A Comment