vb.netpdf.net-core

Convert PDF to JPG


I need to convert a PDF to JPG in a VB.NET / .NET Core application. I'm using the library PdfiumViewer and this code:

Function ConvertPDFToJPG(pdfPath As String, outputPath As String) As String
    Try
        If Not File.Exists(pdfPath) Then
            Return "Error: El archivo PDF no existe."
        End If

        ' Cargar el PDF
        Dim pdf As PdfDocument = PdfDocument.Load(pdfPath)
        
        Dim image as Bitmap
        image = pdf.Render(0,10000,10000, false)
        image.RotateFlip(RotateFlipType.Rotate270FlipNone)

        Dim encoder As ImageCodecInfo = ImageCodecInfo.GetImageDecoders().First(Function(c) c.FormatID = ImageFormat.Jpeg.Guid)
        Dim encoderParams As New EncoderParameters(1)
        encoderParams.Param(0) = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L)

        Dim outputFile as String = outputPath & "_page-" & (1).ToString("D4") & ".jpg"
        image.Save(outputFile, encoder, encoderParams)
        Return "OK"
    Catch ex As Exception
        Return "Error: " & ex.Message
    End Try
End Function

It works, but the result image is only 200Kb more or less, and when I put in a Crystal Reports, it looks blurry. Any solutions?


Solution

  • I saw your request, then checked that library, it's a bit old. But here is the issue:

    In PdfiumViewer.Render(pageIndex, dpiX, dpiY, forPrinting:=True):

    Then I took the time to write a quick example and CLI for you. Check it out here. Basically, you would resize the image after the conversion. No other way to get a higher quality image.

    https://github.com/The-Running-Dev/ManagePDFs/blob/main/ManagePDFs-Legacy-Lib/PdfJpegConverter.vb