Reputation: 2973
First of all, sorry for the low effort question.. I have used these questions as a reference:
Merging multiple PDFs using iTextSharp in c#.net
C# iTextSharp Merge multiple pdf via byte array
My requirements are a bit different.
I get an input of multiple files, either images and/or pdf's, and I need to merge them together into 1 pdf.
Each image gets it own page. So if you have 2 images, you get a pdf with 1 image on each page. If you have 1 pdf with 2 pages, 1 image and another pdf with 3 images, the resulting PDF will have 6 pages.
Currently the code I use with IText7 (The newer version) is as follows:
using iText.IO.Image;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using System.Collections.Generic;
using System.IO;
namespace Example
{
public class ITextPdfCreator : IPdfCreator
{
public MemoryStream Create(IEnumerable<(string ContentType, byte[] Content)> blobs)
{
using (var memoryStream = new MemoryStream())
{
using (var writer = new PdfWriter(memoryStream))
{
var pdf = new PdfDocument(writer);
var document = new Document(pdf);
var firstIteration = true;
foreach (var blob in blobs)
{
if (!firstIteration)
{
document.Add(new AreaBreak(iText.Layout.Properties.AreaBreakType.NEXT_PAGE));
}
if (blob.ContentType.StartsWith("image/"))
{
var content = new Image(ImageDataFactory.Create(blob.Content));
document.Add(content);
}
else if (blob.ContentType.StartsWith("application/pdf"))
{
Stream stream = new MemoryStream(blob.Content);
var d = new PdfDocument(new PdfReader(stream));
d.CopyPagesTo(1, d.GetNumberOfPages(), pdf, pdf.GetNumberOfPages() + 1);
}
firstIteration = false;
}
document.Close();
}
return memoryStream;
}
}
}
}
I was wondering if anyone had the know-how to implement this with https://github.com/VahidN/iTextSharp.LGPLv2.Core. Previously mentioned questions merge multiple pdf's together, but I need to merge images as well.
In the end, my code needs to run on .NET 5.0 and windows and linux. I believe that this lib supports it :)
If anyone has any clue, that'd be amazing! If not, feel free to be annoyed at me for a low effort question.. When I figure it out myself I will post an update!
Upvotes: 2
Views: 2174
Reputation: 2973
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();
}
}
}
Upvotes: 4