Articles in this section
Category / Section

How to export the PDF document into images in ASP.NET Core using Pdfium rendering engine?

2 mins read

At present we do not have support for EJ1 PDF Viewer in ASP.NET Core. However, you can export pages of the PDF document as images in ASP.NET Core using the Pdfium rendering engine. Find the code snippet for the same below,

C#

static void Main(string[] args)
        {
            PdfiumViewer.FPDF_AddRef();
 //Get the stream of the PDF document
            Stream input = File.OpenRead("../../../Data/HTTP Succinctly.pdf");
            int id = StreamManager.Register(input);
            IntPtr doc = PdfiumViewer.FPDF_LoadCustomDocument(input, null, id);
            PdfiumViewer.FPDF_GetDocPermissions(doc);
            PdfiumNative.PdfRenderFlags flags = PdfiumNative.PdfRenderFlags.CorrectFromDpi;
            int pageCount = PdfiumViewer.FPDF_GetPageCount(doc);
       
            for (var i = 0; i < pageCount; i++)
            {
                double width;
                double height;
    //Get the size of the PDF document page
                int pagesizes = PdfiumViewer.FPDF_GetPageSizeByIndex(doc, i, out width, out height);
                int Width = (int)width;
                int Height = (int)height;
                int rotation = 0;
                IntPtr pages = PdfiumViewer.FPDF_LoadPage(doc, i);
    //Creating new bitmap image
                Bitmap bitmapImage = new Bitmap(Width, Height);
                BitmapData data = bitmapImage.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, bitmapImage.PixelFormat);
                IntPtr createdpages = PdfiumViewer.FPDFBitmap_CreateEx(Width, Height, 4, data.Scan0, bitmapImage.Width * 4);
                uint background = (flags & PdfiumNative.PdfRenderFlags.Transparent) == 0 ? 0xFFFFFFFF : 0x00FFFFFF;
                PdfiumViewer.FPDFBitmap_FillRect(createdpages, 0, 0, Width, Height, background);
                PdfiumViewer.FPDF_RenderPageBitmap(createdpages, pages, 0, 0, Width, Height, rotation, 0);
                PdfiumViewer.FPDFBitmap_Destroy(createdpages);
                bitmapImage.UnlockBits(data);
                bitmapImage.Save("../../../Images/" + "exportedPage11_" + i.ToString() + ".png");
            }
        }

Sample link:
http://www.syncfusion.com/downloads/support/directtrac/general/ze/ExportAsImage-1859727000

 

Note:

The above sample will work in Windows OS only, the support for Linux and Mac will be included along with the support for PDF viewer in ASP.NET Core.

 

Follow the below steps to use Pdfium

1. Copy the Pdfium assembly’s folder to a local folder from the installation path. The folder name must be “Pdfium”.
2.The Pdfium assemblies will be available in {$SystemDrive}:\Program Files (x86)\Syncfusion\Essential Studio\{Essential Studio version}\Pdfium

Installation path of PDF 

Note:

Only 16.1.0.24 and higher versions of Essential Studio release will contain Pdfium assembly folder. The Pdfium folder will contain two folders namely X86 and X64, both would contain pdfium.dll assembly for the respective architecture.


  

  

  

  

  

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please  to leave a comment
Access denied
Access denied