Syncfusion.Ej2.PdfViewer no longer has WindowsPdfRenderer

Hi,

Since we are using dpi to determine the size of the output image, we have been using WindowsPdfRenderer up until now.

using var pdfExportImage = new WindowsPdfRenderer();

            object doc = pdfExportImage.Load(inputPdf);

            for (int page = 0; page < pdfExportImage.PageCount; page++)

            {

                using Bitmap bitmap = pdfExportImage.ExportAsImage(page, options.Dpi, options.Dpi);

                await using Stream outStream = await outputPage(page, pdfExportImage.PageCount);

                bitmap.Save(outStream, gdiOutputFormat);

            }

The WindowsPdfRenderer has been available in the version (20.4.0.53) of Syncfusion we have been using up until now.


Now we have to update to 21.2.6 and this WindowsPdfRenderer is no longer there.


Can you provide us with a workaround incase it was intended that WindowsPdfRenderer  was removed?




4 Replies

IJ Irfana Jaffer Sadhik Syncfusion Team May 24, 2023 12:49 PM UTC

We are not sure if you are using EJ2 PDF Viewer or Windows PDF Viewer.


For EJ2 PDF Viewer, we recommend using the PdfRenderer class to convert to an image. Refer to the below code to achieve the same.


public IActionResult OnGetImage()

        {

            PdfRenderer pdfviewer = new PdfRenderer();

            var imagePath = Path.Combine(_hostingEnvironment.WebRootPath, "data/tmp3Page.pdf");

            var docPath = System.IO.File.OpenRead(imagePath);

            pdfviewer.Load(docPath);

            SKBitmap bitmapimage = pdfviewer.ExportAsImage(1);

            var image = SKImage.FromBitmap(bitmapimage);

            using (var stream1 = new FileStream(Path.Combine(_hostingEnvironment.WebRootPath, "data/Page0.png"), FileMode.Create))

            {

                var data1 = image.Encode(SKEncodedImageFormat.Png, 100);

                data1.SaveTo(stream1);

            }

            var imageFileStream = System.IO.File.OpenRead(Path.Combine(_hostingEnvironment.WebRootPath, "data/Page0.png"));

            return File(imageFileStream, "image/png");


        }



Sample:  https://www.syncfusion.com/downloads/support/directtrac/general/7z/EXPORT~1-271549344.7z

For Windows PDF Viewer, we recommend using the ExportAsImage API to convert PDF to an image. Refer to the below code snippet and sample to achieve the same.


Bitmap image = pdfViewerControl1.ExportAsImage(0, pdfViewerControl1.PageCount – 1, 300, 300);

// Save the image.

image.Save("Sample.png", ImageFormat.Png);



UG link: https://help.syncfusion.com/windowsforms/pdf-viewer/working-with-pdf-viewer#exporting-pdf

Sample: https://www.dropbox.com/s/w6r6nnq1etjdfsn/WinFormsPdfViewer.zip?dl=0



ID Ian David Jensen May 24, 2023 02:20 PM UTC

We are using this in C# backend only.


We cannot use the example you provided since PdfRenderer does not allow us to use dpi to determine the size of the output image any longer.


19.4.0.53

https://www.nuget.org/packages/Syncfusion.EJ2.PdfViewer.AspNet.Core.Windows/19.4.0.53

Here we could use the method directly on the pdfRenderer

image

Then that one was removed when we upgraded to 20.4.0.53 Link to nuget and we switched to WindowsPdfRenderer because this one still gave us the option to utilize dpi


Now we want to upgrade again, to solve another issue that we reported last month, that was fixed in 21.2.6 Link to nuget, but now this version doesn't have the WindowsPdfRenderer


Did you remove all methods on pdfRenderer for dpi use?






VV Visvesvar Venkatesan Syncfusion Team May 26, 2023 05:47 PM UTC

Yes, we removed the method of the WindowsPdf renderer and we are validating the feasibility to provide the alternate solution. We will validate and provide details in two business days.



VV Visvesvar Venkatesan Syncfusion Team May 31, 2023 11:43 AM UTC

Thank you for your patience.


We have provided a sample to determine the size of the image using DPI. Kindly find the code snippet and sample below.


Code Snippet:

public IActionResult OnGetImage()

        {

            PdfRenderer pdfviewer = new PdfRenderer();

            var imagePath = Path.Combine(_hostingEnvironment.WebRootPath, "data/2pagepdf.pdf");

            var docPath = System.IO.File.OpenRead(imagePath);

            pdfviewer.Load(docPath);

            SKBitmap bitmapimage = pdfviewer.ExportAsImage(1);

 

            using (var stream1 = new MemoryStream())

            {

              

                double dpiX = 50;

                double dpiY = 50;

 

               

                int width = (int)(bitmapimage.Width * dpiX / 72);

                int height = (int)(bitmapimage.Height * dpiY / 72);

 

              

                var scaledBitmap = new SKBitmap(new SKImageInfo(width, height));

 

               

                using (var canvas = new SKCanvas(scaledBitmap))

                {

                    canvas.DrawBitmap(bitmapimage, SKRect.Create(0, 0, width, height));

                }

 

              

                using (var skData = scaledBitmap.Encode(SKEncodedImageFormat.Png, 100))

                {

                    skData.SaveTo(stream1);

                }

 

               

                stream1.Seek(0, SeekOrigin.Begin);

                var customDpiBitmap = new Bitmap(stream1);

 

              

                customDpiBitmap.SetResolution((float)dpiX, (float)dpiY);

 

              

                customDpiBitmap.Save(Path.Combine(_hostingEnvironment.WebRootPath, "data/Page2.png"), ImageFormat.Png);

            }

 

            var imageFileStream = System.IO.File.OpenRead(Path.Combine(_hostingEnvironment.WebRootPath, "data/Page2.png"));

            return File(imageFileStream, "image/png");

        }


Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/Export_as_image_-_skia_sharp1975381584.zip


Kindly try this and let us know if you have any concerns.


Loader.
Up arrow icon