Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Tue May 12, 2020 9:51 am

Hi,

I'm using FreeSpire.PDF to convert JPEG-files to the PDF-format, I need that for a special application.
This is a very simple workflow in Spire.PDF, however I would like to have the imagequality as high as possible (off course the max quality is determined by the JPEG-sourcefiles).

I use the following code to put the image on the canvas:

page.Canvas.DrawImage(image, 0, 0, fitWidth, fitHeight)

In the lines before I have calculated the fitWidth and fiHeight for an optimal usage of the available space, which works fine.

I notice that the resulting PDF-files (each file just contains 1 image and nothing more) are much smaller than the original JPEG image, which imo means that I lose image quality.
It looks as if Spire.PDF compresses more heavy than the compression used in the Original JPEG-file
So I found the extra parameter of the page.canvas.DrawImage function that should give me control over the CompressionQuality in the PDF.
I played with values in the range 0~100 but constantly get an error in VB.net.
If I for instance try: page.Canvas.DrawImage(image, 50, 0, -575, fitWidth, fitHeight) Where 50 is the compressionquality, I get a System.NullReferenceException.

This happens with all values that I use for the compressionquality.
My questions are:
1. Do I oversee something?
2. How to preserve maximum quality of images with the Drawimage function?
3. Is this perhaps a limitation of the free version of Spire.PDF?

Finally, I want to compliment E-Iceblue with the product and thank you for the support in advance!

regards, Henk

henkjanssen
 
Posts: 2
Joined: Tue May 12, 2020 9:26 am

Tue May 12, 2020 11:08 am

Hi Henk,

Thanks for your inquiry.
I did an initial test with Free Spire.PDF for .NET Version:6.2 and the latest commercial version Spire.PDF Pack(Hot Fix) Version:6.5.6, and got the same error as yours. This issue has been logged into our bug tracking system with the ticket SPIREPDF-3249. We will let you know if there is any update.

Besides, in fact, the "DrawImage" method does not compress the image quality by default. If you want to improve the image quality, you can refer to the code below.
Code: Select all
    Sub Main()
        Dim pdf As PdfDocument = New PdfDocument
        Dim section As PdfSection = pdf.Sections.Add
        Dim page As PdfPageBase = pdf.Pages.Add
        Dim img As Image = System.Drawing.Image.FromFile("logo.jpg")
        Dim bit As Bitmap = ResizeImage(img, img.Size.Width * 10, img.Size.Height * 10)
        Dim image As PdfImage = PdfImage.FromImage(bit)
        Dim widthFitRate As Single = (image.PhysicalDimension.Width / page.Canvas.ClientSize.Width)
        Dim heightFitRate As Single = (image.PhysicalDimension.Height / page.Canvas.ClientSize.Height)
        Dim fitRate As Single = Math.Max(widthFitRate, heightFitRate)
        Dim fitWidth As Single = (image.PhysicalDimension.Width / fitRate)
        Dim fitHeight As Single = (image.PhysicalDimension.Height / fitRate)
        'Draw image
        page.Canvas.DrawImage(image, 0, 30, fitWidth, fitHeight)
        Dim output As String = "out.pdf"
        pdf.SaveToFile(output)
        pdf.Close()
    End Sub

    Function ResizeImage(ByVal bmp As Image, ByVal newW As Integer, ByVal newH As Integer) As Bitmap

        Dim b As Bitmap = New Bitmap(newW, newH)
        Dim g As Graphics = Graphics.FromImage(b)
        g.InterpolationMode = InterpolationMode.HighQualityBicubic
        g.DrawImage(bmp, New Rectangle(0, 0, newW, newH), New Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel)
        g.Dispose()
        Return b
    End Function


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Tue May 12, 2020 12:02 pm

Thank you for the quick response!

henkjanssen
 
Posts: 2
Joined: Tue May 12, 2020 9:26 am

Wed May 13, 2020 1:14 am

Hello,

You are welcome.
Btw, have you tested the code? Did it help you?

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Mon Aug 17, 2020 11:19 am

Hello,

Hope you are doing well.
Glad to inform you that we just released Spire.PDF Pack(Hot Fix) Version:6.8.5 which fixes the issue that the application threw NullReferenceException when setting the parameter "CompressionQuality". Welcome to download it from the following links.
Our website: https://www.e-iceblue.com/Download/down ... t-now.html
Nuget: https://www.nuget.org/packages/Spire.PDF/6.8.5

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.PDF