« MBS Xojo Plugins, ver… | Home | MonkeyBread Software … »

WebPicture from PDF Page HiDPI


Today we updated our WebPicture from PDF Page example project for Xojo to better handle HiDPI support when rendering PDF pages with DynaPDFMBS class in MBS Xojo DynaPDF Plugin.

The WebPicture class has a couple of constructors, but sadly none to provide both width and height and image data. So we need to create the WebPicture based on a low resolution version to get the right size in. Later we replace data with high resolution image data to get the sharp image to show in browser. Here is our sample code:

Function RenderPDFWebPicture(file as FolderItem, pageIndex as integer, width as integer, height as integer) As WebPicture Dim pdf As New MyDynaPDFMBS // create PDF environment and setup If pdf.CreateNewPDF(Nil) Then if pdf.SetImportFlags(pdf.kifImportAsPage + pdf.kifImportAll) then // open source file If pdf.OpenImportFile(file, 0, "") >= 0 Then // import file if pdf.Append then If pdf.ImportPageEx(PageIndex, 1.0, 1.0) >= 0 Then if pdf.EndPage then // make a jpeg If pdf.SetJPEGQuality(80) Then // render 1x If pdf.RenderPageToImage(pageIndex, Nil, 72, width, height, pdf.krfDefault, pdf.kpxfRGB, pdf.kcfJPEG, pdf.kifmJPEG) Then Dim LowData As String = pdf.GetImageBuffer // render 2x If pdf.RenderPageToImage(pageIndex, Nil, 144, width, height, pdf.krfDefault, pdf.kpxfRGB, pdf.kcfJPEG, pdf.kifmJPEG) Then Dim HighData As String = pdf.GetImageBuffer // now build 1x picture Dim w As New WebPicture(LowData, "page.jpg") // and put in 2x data w.data = HighData // and return with HiDPI content Return w end if end if end if end if end if end if end if end if End If End Function

As you can see, we create new MyDynaPDFMBS object, our subclass to fill error event and learn about why things may go wrong. The OpenImportFile function may fail to open the PDF file on the server. Otherwise use OpenImportBuffer to pass PDF as data block directly. We import first page, set JPEG quality and then render two versions of the image, one with 72 dpi and one with 144 dpi. Both are then combined into a WebPicture object.

Please do not hesitate to contact us if you have questions. The example will be included in MBS Plugin 20.1 download.
09 03 20 - 09:15