Merge PDF Using iTextSharp ACRO Fields

PDFescape
Pankaj Nihalani
24-Mar-2020
Reading Time: 3 minutes

In this article we’ll learn how we can merge PDF ACRO Fields using C#.

Prerequisites

  1. Prior knowledge of C#.
  2. A development machine with Visual Studio 2010 or Higher version installed.

Step-by-step tutorial for how we can merge PDF ACRO Fields using C#

  • Step 1: Prepare 2 or more PDF
  • Step 2: Open your URL in browser

URL: https://www.pdfescape.com/windows/

pdfescape Picture1 Merge PDF Using iTextSharp ACRO Fields
  • Step 3: Click on Free Online and Open PDF.
pdfescape Picture 2 Merge PDF Using iTextSharp ACRO Fields
pdfescape Picture 3 Merge PDF Using iTextSharp ACRO Fields
  • Step 4: Set the ACRO Field.
pdfescape Picture 4 Merge PDF Using iTextSharp ACRO Fields
  • Step 5: Enter and then Place your mouse cursor any where in the PDF in order to set the ACRO Field.
Set the ACRO Field Picture 5 in PDFescape Merge PDF Using iTextSharp ACRO Fields
  • Step 6: And then right click on object properties.
right click on object properties picture Merge PDF Using iTextSharp ACRO Fields
  • Step 7: Set the name of ACRO field from this pop up of field properties and select read only property
name of ACRO field from Merge PDF Using iTextSharp ACRO Fields
  • Step 8: After click on OK and in the header disable is there click on that in order to disable the form field and finally it will look like this.
disable the form field Picture Merge PDF Using iTextSharp ACRO Fields
  • Step 9: Click on green symbol you can see in left side bar it will download your PDF with ACRO field.
Set the ACRO Field Picture 5 in PDFescape 2 Merge PDF Using iTextSharp ACRO Fields
  • Step 10: Finally PDF is created with the ACRO Fields similarly you can create other two PDF which you want to merge using ACRO Field.
  • Step 11: We’re going to use this below function which will merge your PDF files including ACRO field.
    • How to use this function?
var myFileList = new List<string>();  // Prepare List of Files which we’re going to merge 
long fileTimeStamp = DateTime.Now.ToFileTime(); // prepare temp folder
System.IO.Directory.CreateDirectory(Server.MapPath("~/Document/"+ fileTimeStamp.ToString())); 
for (int i = 0; i < dir.GetFiles().Length; i++)
{
 	firstPDFPath = Server.MapPath("~/Document/Test" + (i + 1) + ".pdf");

       PdfReader _readerPayment = new PdfReader(firstPDFPath);

       using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
       {
           PdfStamper stamper = new PdfStamper(_readerPayment, ms);
           using (stamper)
           {
           	 Dictionary<string, string> info = _readerPayment.Info;
        stamper.MoreInfo = info;
       	 _commonService.SetPDFFields(stamper.AcroFields);
            }
   System.IO.MemoryStream msn = new System.IO.MemoryStream(ms.ToArray());
   System.IO.FileStream file = new System.IO.FileStream(Server.MapPath("~/Document/" +     fileTimeStamp.ToString() + "/" + (++counter) + ".pdf"), FileMode.Create, FileAccess.Write);
myFileList.Add(Server.MapPath("~/Document/" + fileTimeStamp.ToString() + "/" + (counter) + ".pdf"));
msn.WriteTo(file);
file.Close();
msn.Close();
          }
}
string[] _allFiles = System.IO.Directory.GetFiles(Server.MapPath("~/Document/" + fileTimeStamp.ToString()));
MergePDFs(Server.MapPath("~/Document/MainApp_"+fileTimeStamp+".pdf"), _allFiles);
public void MergePDFs(string outPutFilePath, params string[] filesPath)
        {
            try
            {
                List<PdfReader> readerList = new List<PdfReader>();
                foreach (string filePath in filesPath)
                {
                    PdfReader pdfReader = new PdfReader(filePath);
                    readerList.Add(pdfReader);
                }

                //Define a new output document and its size, type
iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0f, 0f, 0f, 20f);
       //Create blank output pdf file and get the stream to write on it.
       iTextSharp.text.pdf.PdfCopy writer = new iTextSharp.text.pdf.PdfCopy(document, new                             FileStream(outPutFilePath, FileMode.Create));

                document.Open();
                foreach (PdfReader reader in readerList)
                {
                    for (int i = 1; i <= reader.NumberOfPages; i++)
                    {
                        PdfImportedPage page = writer.GetImportedPage(reader, i);
                        document.Add(iTextSharp.text.Image.GetInstance(page));
                    }

                    writer.AddDocument(reader);
                    reader.Close();
                }
                document.Close();
                writer.Close();
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
 }

GitHub Repository URL: https://github.com/sandipmalaviya07/merge-pdf-accrofield

Our developers are always happy to assist you to turn your business ideas into beautiful reality; you can hire .NET developer with easy hiring model.

That’s it for now. Stay Connected for more tutorials, until than Happy Coding…

  1. How to Load Dynamic HTML content in Page using MVC
  2. Parallel ForEach Loop in C# to Improve App Performance
  3. Send Bulk Emails with Free Email Marketing Tool