4

I have some PDFs which I would like to fill out automatically using C#. I know about iTextSharp, but I am unsure about licensing issues for business use, and would rather find a different solution.

Basically, I would like to open a PDF, specify the field (or give coordinates for a textbox) and be able to insert text (& possibly small images?). Then I need to merge and save the pdf.

Any suggestions for a good way to accomplish this where I don't have to purchase / worry about licenses?

Rayshawn
  • 2,603
  • 3
  • 27
  • 46
sǝɯɐſ
  • 2,470
  • 4
  • 33
  • 47

3 Answers3

4

Not sure what the advantages of iTextSharp are, but I found a solution that is working perfectly so far using PDFSharp! As documentation seems to be a little on the light side for PDFSharp, I also found this thread to be very helpful...

If this helps anybody, here is my sample program:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using PdfSharp.Fonts;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using PdfSharp.Pdf.AcroForms;

namespace PDFSharpTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void goButton_Click(object sender, EventArgs e)
        {
            //TestPDF();  //uncomment this to find out whether acroform will work correctly         

            //open file
            PdfDocument pdf = PdfReader.Open(@"YOURFILEPATHandNAME", PdfDocumentOpenMode.Modify);

            //fix some odd setting where filled fields don't always show                   SetupPDF(pdf);

            //find and fill fields
            PdfTextField txtEmployerName = (PdfTextField)(pdf.AcroForm.Fields["txtEmployerName"]);
            txtEmployerName.Value = new PdfString("My Name");

            PdfTextField txtEmployeeTitle = (PdfTextField)(pdf.AcroForm.Fields["txtEmployeeTitle"]);
            txtEmployeeTitle.Value = new PdfString("Workin'");

            PdfCheckBoxField chxAttached = (PdfCheckBoxField)(pdf.AcroForm.Fields["chxAttached"]);
            chxAttached.Checked = true;

            //save file
            pdf.Save(@"NEWFILEPATHandNAMEHERE");
        }

        private void SetupPDF(PdfDocument pdf)
        {
            if (pdf.AcroForm.Elements.ContainsKey("/NeedAppearances") == false)
            {
                pdf.AcroForm.Elements.Add("/NeedAppearances", new PdfSharp.Pdf.PdfBoolean(true));
            }
            else
            {
                pdf.AcroForm.Elements["/NeedAppearances"] = new PdfSharp.Pdf.PdfBoolean(true);
            }
        }

        private void PDFTest()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                PdfDocument _document = null;
                try { _document = PdfReader.Open(ofd.FileName, PdfDocumentOpenMode.Modify); }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "FATAL");             //do any cleanup and return             
                    return;
                }
                if (_document != null)
                {
                    if (_document.AcroForm != null)
                    {
                        MessageBox.Show("Acroform is object", "SUCCEEDED");
                        //pass acroform to some function for processing          
                        _document.Save(@"C:\temp\newcopy.pdf");
                    }
                    else
                    {
                        MessageBox.Show("Acroform is null", "FAILED");
                    }
                }
                else
                {
                    MessageBox.Show("Unknown error opening document", "FAILED");
                }
            }
        }
    }
}
Community
  • 1
  • 1
sǝɯɐſ
  • 2,470
  • 4
  • 33
  • 47
  • 1
    this is required for pdf newer than 1.4 (Acrobat 6): http://forum.pdfsharp.net/viewtopic.php?f=2&t=693&p=5855#p5855 I would also recommend using [PDF Clown](http://www.stefanochizzolini.it/en/projects/clown/), it has licensing which is ok for commercial products – Răzvan Flavius Panda Apr 18 '13 at 10:44
  • @RăzvanPanda you are correct sir... and I actually ran into a bunch of issues with PDF versions, and ended up switching to using iTextSharp after all... PDFSharp works fine for CREATING PDFs, but it's not really made for filling existing PDF form fields (which is what I was trying to accomplish)... – sǝɯɐſ Apr 18 '13 at 16:20
  • What problems did you run into with PDFsharp? I need to use it for filling form fields also, until now it worked for that purpose, but I haven't tested all the common cases. – Răzvan Flavius Panda Apr 18 '13 at 16:24
  • I mostly had issues with PDF versions... the code from your link seemed to take care of the first few, but then I had other PDFs where it didn't work... tried converting them manually a few times, couldn't get it to work... I decided to give iTextSharp a shot, and haven't looked back... – sǝɯɐſ Apr 18 '13 at 18:46
  • Also, I couldn't find any way to flatten PDFs after filling in the form fields with PDFSharp... with iTextSharp, it's a simple line... – sǝɯɐſ Apr 18 '13 at 18:47
  • As long as the pdf version used is Acrobat 5 or lower it works. To flatten the pdf you need to set `ReadOnly` to false on all form fields. I am sure iTextSharp is awesome but since it is now AGPL/Copyleft is not as useful for comercial products. – Răzvan Flavius Panda Apr 19 '13 at 15:24
1

This is a free and open source solution: SharpPDF

But in all honesty iTextSharp is probably the best thing you'll find.

Jordan Kaye
  • 2,837
  • 15
  • 15
  • 2
    Not sure if "SharpPDF" and "PDFSharp" are completely different programs, but the dll your link points to didn't work for me... I found a working solution using [PDFSharp](http://sourceforge.net/projects/pdfsharp/). But thank you for pointing me in the right direction! – sǝɯɐſ Oct 19 '12 at 20:52
0

You might try Docotic.Pdf library for your task. The library is not free but probably there is nothing to worry about (licensing wise).

Here are samples available online that might help you:

Other samples from Forms and Annotations group could also prove to be useful in your case.

Disclaimer: I work for the vendor of the library.

Bobrovsky
  • 13,789
  • 19
  • 80
  • 130
  • 1
    I did see this as well, but I like free... =) – sǝɯɐſ Oct 19 '12 at 20:42
  • I'm tempted to downvote this answer because the OP specifically asks for free options: "_Any suggestions for a good way to accomplish this where I don't have to purchase / worry about licenses?_" – Bernhard Hofmann Jul 14 '14 at 10:03