Home » Microsoft Technologies » Merge PDF Using iTextSharp ACRO Fields
Merge PDF Using iTextSharp ACRO Fields
In this article we’ll learn how we can merge PDF ACRO Fields using C#.
Prerequisites
- Prior knowledge of C#.
- 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/
- Step 3: Click on Free Online and Open PDF.
- Step 4: Set the ACRO Field.
- Step 5: Enter and then Place your mouse cursor any where in the PDF in order to set the ACRO Field.
- Step 6: And then right click on object properties.
- Step 7: Set the name of ACRO field from this pop up of field properties and select read only property
- 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.
- Step 9: Click on green symbol you can see in left side bar it will download your PDF with ACRO field.
- 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…
Related C# MVC Development Resource
I’m .NET C# Developer working at Samarpan Infotech and I spend my days with my hands in many different areas of web development from back end programming (.NET C#,SQL Server) to front end engineering (HTML, CSS, and jQuery/Javascript). I’m extremely passionate about to learn new things and I enjoy spending my time with my friends and discover new places.