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!