Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging PDFs and converting PDF to PNG image in .NET Core 2.0

I am looking for third party .dll which can support merging pdf's into one and also converting the merged pdf into one .PNG image file.

I know Ghostscript or pdfsharp supports .NET framework but not .NET core 2.0 framework.

If anyone can please suggest any third part party dll which can merge all the PDFs and also convert merged pdf into PNG image in .NET core 2.0.

Any help or suggestions to achieve this requirement?

like image 641
Roshan Avatar asked Mar 26 '18 14:03

Roshan


People also ask

How do I convert a PDF to an image in .NET core?

Use DynamicPDF Rasterizer for . NET to efficiently and easily convert any PDF to a variety of image formats. It supports rasterizing PDFs to common image formats and offers a robust set of images and quality settings. DynamicPDF Rasterizer supports the following image types.

Can you convert PDFs to PNG?

Follow these steps to convert a PDF to a PNG file. Click the Select A File button or drag and drop the file into the drop zone to upload your PDF. Select PNG from the file format drop-down menu. Or choose JPG, or TIFF instead if you wish. Click the Convert To PNG button, or whichever format you selected.

How do I convert a PDF to a PNG without losing quality?

Open the PDF file with preview in Mac and at the top, click on the File menu and select "Export." Step 2. On the Export window, change the format to "PNG" and adjust quality and resolution accordingly. Now hit "Save," and the PDF file would be converted to PNG.


3 Answers

I've been struggling with this myself lately, couldn't find a library that would suit my needs so I wrote a C# wrapper around PDFium which has BSD 3-clause license and my wrapper code is released under MIT so you can either use the NuGet package or use the code directly yourself. The repo can be found here docnet.

like image 119
phuldr Avatar answered Oct 27 '22 16:10

phuldr


I'm just answering the part about rendering a PDF and converting it to an image in .NET Core 3.1, which took a couple days to figure all out. I ended up using phuldr's Docnet.Core to get the image bytes and used Magick.NET-Q16-AnyCpu to save it to an image file.

There was a little extra work to re-arrange the image bytes to RGBA order and to turn the transparent pixels into a specific color (white in my case). Here's my code in case it helps:

public MemoryStream PdfToImage(byte[] pdfBytes /* the PDF file bytes */)
{
    MemoryStream memoryStream = new MemoryStream();
    MagickImage imgBackdrop;
    MagickColor backdropColor = MagickColors.White; // replace transparent pixels with this color 
    int pdfPageNum = 0; // first page is 0

    using (IDocLib pdfLibrary = DocLib.Instance)
    {
        using (var docReader = pdfLibrary.GetDocReader(pdfBytes, new PageDimensions(1.0d)))
        {
            using (var pageReader = docReader.GetPageReader(pdfPageNum))
            {
                var rawBytes = pageReader.GetImage(); // Returns image bytes as B-G-R-A ordered list.
                rawBytes = RearrangeBytesToRGBA(rawBytes);
                var width = pageReader.GetPageWidth();
                var height = pageReader.GetPageHeight();

                // specify that we are reading a byte array of colors in R-G-B-A order.
                PixelReadSettings pixelReadSettings = new PixelReadSettings(width, height, StorageType.Char, PixelMapping.RGBA);
                using (MagickImage imgPdfOverlay = new MagickImage(rawBytes, pixelReadSettings))
                {
                    // turn transparent pixels into backdrop color using composite: http://www.imagemagick.org/Usage/compose/#compose
                    imgBackdrop = new MagickImage(backdropColor, width, height);                            
                    imgBackdrop.Composite(imgPdfOverlay, CompositeOperator.Over);
                }
            }
        }
    }

    
    imgBackdrop.Write(memoryStream, MagickFormat.Png);
    imgBackdrop.Dispose();
    memoryStream.Position = 0;
    return memoryStream;
}

private byte[] RearrangeBytesToRGBA(byte[] BGRABytes)
{
    var max = BGRABytes.Length;
    var RGBABytes = new byte[max];
    var idx = 0;
    byte r;
    byte g;
    byte b;
    byte a;
    while (idx < max)
    {
        // get colors in original order: B G R A
        b = BGRABytes[idx];
        g = BGRABytes[idx + 1];
        r = BGRABytes[idx + 2];
        a = BGRABytes[idx + 3];

        // re-arrange to be in new order: R G B A
        RGBABytes[idx] = r;
        RGBABytes[idx + 1] = g;
        RGBABytes[idx + 2] = b;
        RGBABytes[idx + 3] = a;

        idx += 4;
    }
    return RGBABytes;
}
like image 41
HappyGoLucky Avatar answered Oct 27 '22 15:10

HappyGoLucky


you can use iTextSharp.LGPLv2.Core to merge pdf files, it works pretty well. Please check this tutorial. It supports .NETStandard as well.

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

    namespace HelveticSolutions.PdfLibrary
    {
      public static class PdfMerger
      {
        /// <summary>
        /// Merge pdf files.
        /// </summary>
        /// <param name="sourceFiles">PDF files being merged.</param>
        /// <returns></returns>
        public static byte[] MergeFiles(List<byte[]> sourceFiles)
        {
          Document document = new Document();
          using (MemoryStream ms = new MemoryStream())
          {
            PdfCopy copy = new PdfCopy(document, ms);
            document.Open();
            int documentPageCounter = 0;

            // Iterate through all pdf documents
            for (int fileCounter = 0; fileCounter < sourceFiles.Count; fileCounter++)
            {
              // Create pdf reader
              PdfReader reader = new PdfReader(sourceFiles[fileCounter]);
              int numberOfPages = reader.NumberOfPages;

              // Iterate through all pages
              for (int currentPageIndex = 1; currentPageIndex <= numberOfPages; currentPageIndex++)
              {
                documentPageCounter++;
                PdfImportedPage importedPage = copy.GetImportedPage(reader, currentPageIndex);
                PdfCopy.PageStamp pageStamp = copy.CreatePageStamp(importedPage);

                // Write header
                ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_CENTER,
                    new Phrase("PDF Merger by Helvetic Solutions"), importedPage.Width / 2, importedPage.Height - 30,
                    importedPage.Width < importedPage.Height ? 0 : 1);

                // Write footer
                ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_CENTER,
                    new Phrase(String.Format("Page {0}", documentPageCounter)), importedPage.Width / 2, 30,
                    importedPage.Width < importedPage.Height ? 0 : 1);

                pageStamp.AlterContents();

                copy.AddPage(importedPage);
              }

              copy.FreeReader(reader);
              reader.Close();
            }

            document.Close();
            return ms.GetBuffer();
          }
        }
      }
    }
like image 39
Dalton Avatar answered Oct 27 '22 16:10

Dalton