Do not forget to install iTextSharp.dll using Manage NuGet Packages
using iTextSharp.text.pdf;
using System.IO;
namespace ConsoleApplication1
{
public class MergePDF
{
public void ProcessMergePDF()
{
string[] files = Directory.GetFiles(@"E:\Form\");
string outputPath = @"E:\Output\Merge.pdf";
using (Document doc = new Document())
{
using (PdfCopy writer = new PdfCopy(doc, new FileStream(outputPath, FileMode.Create)))
{
if (writer == null)
return;
doc.Open();
foreach (string file in files)
{
using (PdfReader reader = new PdfReader(file))
{
reader.ConsolidateNamedDestinations();
for (int i = 1; i <= reader.NumberOfPages; i++)
{
PdfImportedPage page = writer.GetImportedPage(reader, i);
writer.AddPage(page);
}
}
}
}
}
}
}
}
No comments:
Post a Comment