Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# merge PDF solution without using iTextSharp

Tags:

c#

merge

pdf

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?

like image 932
sǝɯɐſ Avatar asked Oct 23 '25 23:10

sǝɯɐſ


2 Answers

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");
                }
            }
        }
    }
}
like image 176
sǝɯɐſ Avatar answered Oct 25 '25 19:10

sǝɯɐſ


This is a free and open source solution: SharpPDF

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

like image 30
Jordan Kaye Avatar answered Oct 25 '25 19:10

Jordan Kaye



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!