What is the optimum way to merge 2 PDF files with ITextSharp in C#? I'm using ASP.NET/.NET3.5.
Asked
Active
Viewed 4.7k times
20
-
1I've used code based on the following article with success: [Simple .NET PDF Merger](http://www.codeproject.com/KB/files/SimplePdfMerger.aspx) – Jay Riggs Feb 09 '10 at 22:57
-
iTextSharp is now called "iText 7 for .NET"or "itext7-dotnet" on github: [link](https://github.com/itext/itext7-dotnet). It's recommended to add itext7 with Nuget to your solution. – Peter Huber Aug 29 '20 at 04:44
2 Answers
31
public static void Merge(List<String> InFiles, String OutFile)
{
using (FileStream stream = new FileStream(OutFile, FileMode.Create))
using (Document doc = new Document())
using (PdfCopy pdf = new PdfCopy(doc, stream))
{
doc.Open();
PdfReader reader = null;
PdfImportedPage page = null;
//fixed typo
InFiles.ForEach(file =>
{
reader = new PdfReader(file);
for (int i = 0; i < reader.NumberOfPages; i++)
{
page = pdf.GetImportedPage(reader, i + 1);
pdf.AddPage(page);
}
pdf.FreeReader(reader);
reader.Close();
});
}
}
-
How can I use this function? Say, you have a generated pdf and append it to a physical file pdf? – Boy Pasmo Oct 13 '15 at 15:34
-
2Please note that this answer is wrong. It throws away all interactivity and it can result in bloated files if the different PDFs contain identical resources. The only correct answer is an answer that uses `PdfSmartCopy` which is another class that is available in iTextSharp. – Bruno Lowagie Apr 01 '16 at 10:19
-
I'd also take a look at this answer: https://stackoverflow.com/questions/38339151/c-sharp-itextsharp-merge-multiple-pdf-via-byte-array – Ricardo Appleton May 26 '17 at 10:37
-
I would vote down @BrunoLowagie comment if I could, it isn't actually relevant to question and it is just him advertising his proprietary version of iTextSharp – Joe Tyman Jul 28 '17 at 15:41
-
@BrunoLowagie does the PdfSmartCopy retains the ADA compliance properties if merged two ADA compliant pdfs? – ScottS Jun 15 '23 at 12:30
1
The last answer works if you don't want to delete the original files. In my case, I want to delete and when I tried I got exception. My solution is:
public static bool MergePDFs(List<String> InFiles, String OutFile)
{
bool merged = true;
try
{
List<PdfReader> readerList = new List<PdfReader>();
foreach (string filePath in InFiles)
{
PdfReader pdfReader = new PdfReader(filePath);
readerList.Add(pdfReader);
}
//Define a new output document and its size, type
Document document = new Document(PageSize.A4, 0, 0, 0, 0);
//Create blank output pdf file and get the stream to write on it.
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(OutFile, FileMode.Create));
document.Open();
foreach (PdfReader reader in readerList)
{
PdfReader.unethicalreading = true;
for (int i = 1; i <= reader.NumberOfPages; i++)
{
PdfImportedPage page = writer.GetImportedPage(reader, i);
document.Add(iTextSharp.text.Image.GetInstance(page));
}
}
document.Close();
foreach (PdfReader reader in readerList)
{
reader.Close();
}
}
catch (Exception ex)
{
merged = false;
}
return merged;
}
I copied the code from Original Code

Pafcosta
- 59
- 4
-
Slight modifications to handle rotation of pages, etc, but apart from that, it easily replaced the code in my project that was locking files.. – Ads May 03 '16 at 02:25