Merging Multiple PDFs Using Itextsharp in C#.Net

Merging multiple PDFs using iTextSharp in c#.net

I found the answer:

Instead of the 2nd Method, add more files to the first array of input files.

public static void CombineMultiplePDFs(string[] fileNames, string outFile)
{
// step 1: creation of a document-object
Document document = new Document();
//create newFileStream object which will be disposed at the end
using (FileStream newFileStream = new FileStream(outFile, FileMode.Create))
{
// step 2: we create a writer that listens to the document
PdfCopy writer = new PdfCopy(document, newFileStream);

// step 3: we open the document
document.Open();

foreach (string fileName in fileNames)
{
// we create a reader for a certain document
PdfReader reader = new PdfReader(fileName);
reader.ConsolidateNamedDestinations();

// step 4: we add content
for (int i = 1; i <= reader.NumberOfPages; i++)
{
PdfImportedPage page = writer.GetImportedPage(reader, i);
writer.AddPage(page);
}

PRAcroForm form = reader.AcroForm;
if (form != null)
{
writer.CopyAcroForm(reader);
}

reader.Close();
}

// step 5: we close the document and writer
writer.Close();
document.Close();
}//disposes the newFileStream object
}

Merge multiple files into a pdf with ITextSharp (Old version)

I have found a solution!

My code is as follows:

using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;

public class ITextSharpPdfMerger
{
private const float _imageLeftMargin = 10f;
private const float _imageRightMargin = 10f;
private const float _imageBottomMargin = 30f;
private const float _imageTopMargin = 30f;

// https://github.com/VahidN/iTextSharp.LGPLv2.Core
// https://stackoverflow.com/a/6056801/3013479
public byte[] Create(IEnumerable<(string ContentType, byte[] Content)> blobs)
{
Document document = null;
PdfCopy copy = null;

using (var stream = new MemoryStream())
{

try
{
document = new Document();
copy = new PdfCopy(document, stream);

document.Open();

foreach (var blob in blobs)
{
if (blob.ContentType.StartsWith("image/"))
{
AddImage(copy, blob.Content);
}
else if (blob.ContentType == "application/pdf")
{
AddPdf(copy, blob.Content);
}
else
{
throw new ArgumentException($"Blob with ContentType {blob.ContentType} is not supported for merging.");
}
}
}
finally
{
document?.Close();
copy?.Close();
}

return stream.ToArray();
}
}

private static void AddPdf(PdfCopy copy, byte[] content)
{
PdfReader reader = null;
try
{
reader = new PdfReader(content);

// Grab each page from the PDF and copy it
for (int i = 1; i <= reader.NumberOfPages; i++)
{
var page = copy.GetImportedPage(reader, i);
copy.AddPage(page);
}

// A PDF can have a form; we copy it into the resulting pdf.
// Example: https://web.archive.org/web/20210322125650/https://www.windjack.com/PDFSamples/ListPrograming_Part1_AcroForm.pdf
var form = reader.AcroForm;
if (form != null)
{
copy.CopyAcroForm(reader);
}

}
finally
{
reader?.Close();
}
}

private static void AddImage(PdfCopy copy, byte[] content)
{
// We have a little workaround to add images because we use PdfCopy which is only useful for COPYING and doesn't work for adding pages manually.
// So we create a new PDF in memory containing the image, and then we copy that PDF into the resulting PDF.
// https://stackoverflow.com/a/26111677/3013479
Document document = null;
PdfWriter writer = null;
PdfReader reader = null;
using (var stream = new MemoryStream())
{
try
{
document = new Document();
writer = PdfWriter.GetInstance(document, stream);

document.Open();
if (!document.NewPage())
{
throw new Exception("New page could not be created");
}

var image = Image.GetInstance(content);

// Make the image fit on the page
// https://stackoverflow.com/q/4932187/3013479
image.Alignment = Element.ALIGN_MIDDLE;
var pageWidth = copy.PageSize.Width - (_imageLeftMargin + _imageRightMargin);
var pageHeight = copy.PageSize.Height - (_imageBottomMargin + _imageTopMargin);
image.ScaleToFit(pageWidth, pageHeight);

if (!document.Add(image))
{
throw new Exception("Unable to add image to page");
}

// These are required for the PdfReader instantation to succeed
document.Close();
writer.Close();

reader = new PdfReader(stream.ToArray());

copy.AddPage(copy.GetImportedPage(reader, 1));
}
finally
{
document?.Close();
reader?.Close();
writer?.Close();
}
}
}

Merging PDFs with ITextSharp

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();
});
}
}

C# iTextSharp Merge multiple pdf via byte array

This is pretty much just a C# version of Bruno's code here.

This is pretty much the simplest, safest and recommended way to merge PDF files. The PdfSmartCopy object is able to detect redundancies in the multiple files which can reduce file size some times. One of the overloads on it accepts a full PdfReader object which can be instantiated however you want.

public static byte[] concatAndAddContent(List<byte[]> pdfByteContent) {

using (var ms = new MemoryStream()) {
using (var doc = new Document()) {
using (var copy = new PdfSmartCopy(doc, ms)) {
doc.Open();

//Loop through each byte array
foreach (var p in pdfByteContent) {

//Create a PdfReader bound to that byte array
using (var reader = new PdfReader(p)) {

//Add the entire document instead of page-by-page
copy.AddDocument(reader);
}
}

doc.Close();
}
}

//Return just before disposing
return ms.ToArray();
}
}

Combine two (or more) PDF's

I had to solve a similar problem and what I ended up doing was creating a small pdfmerge utility that uses the PDFSharp project which is essentially MIT licensed.

The code is dead simple, I needed a cmdline utility so I have more code dedicated to parsing the arguments than I do for the PDF merging:

using (PdfDocument one = PdfReader.Open("file1.pdf", PdfDocumentOpenMode.Import))
using (PdfDocument two = PdfReader.Open("file2.pdf", PdfDocumentOpenMode.Import))
using (PdfDocument outPdf = new PdfDocument())
{
CopyPages(one, outPdf);
CopyPages(two, outPdf);

outPdf.Save("file1and2.pdf");
}

void CopyPages(PdfDocument from, PdfDocument to)
{
for (int i = 0; i < from.PageCount; i++)
{
to.AddPage(from.Pages[i]);
}
}


Related Topics



Leave a reply



Submit