Look at Docotic.Pdf library. This library supports .NET Core without any dependencies and unsafe code.
Docotic's PDF to image renderer does not depend on GDI+ (System.Drawing). That's important for reliable running of your code in ASP.NET context or on Linux.
Merge PDF documents:
public void MergeDocuments(string firstPath, string secondPath)
{
using (var pdf = new PdfDocument(firstPath))
{
pdf.Append(secondPath); // or append stream or byte array
pdf.ReplaceDuplicateObjects(); // useful when merged files contain common objects like fonts and images
pdf.Save("merged.pdf");
}
}
Convert PDF page to PNG image:
using (var pdf = new PdfDocument(@"merged.pdf"))
{
PdfDrawOptions options = PdfDrawOptions.Create();
options.Compression = ImageCompressionOptions.CreatePng();
options.BackgroundColor = new PdfRgbColor(255, 255, 255);
options.HorizontalResolution = 600;
options.VerticalResolution = 600;
pdf.Pages[0].Save("result.png", options);
}
More samples for PDF to image conversion
You mentioned conversion of the merged PDF document to a single PNG image. PNG does not support multi-frame images (more detail). So you can only do the following:
- Merge all PDF document pages to the single page
- Render this page as described above
Here is the sample for this case (merge 2 pages to one and save as PNG):
using (var other = new PdfDocument(@"merged.pdf"))
{
using (var pdf = new PdfDocument())
{
PdfXObject firstXObject = pdf.CreateXObject(other.Pages[0]);
PdfXObject secondXObject = pdf.CreateXObject(other.Pages[1]);
PdfPage page = pdf.Pages[0];
double halfOfPage = page.Width / 2;
page.Canvas.DrawXObject(firstXObject, 0, 0, halfOfPage, 400, 0);
page.Canvas.DrawXObject(secondXObject, halfOfPage, 0, halfOfPage, 400, 0);
PdfDrawOptions options = PdfDrawOptions.Create();
options.BackgroundColor = new PdfRgbColor(255, 255, 255);
page.Save("result.png", options);
}
}