Page 1 of 1

[Tutorial] Reducing PDF size by altering the embedded images

Posted: Sat Feb 18, 2012 2:13 pm
by Loïc
Hello,

This code demonstrates how to dramatically reduces a PDF file size by altering its embedded images.

Code: Select all

        Const CONVERT_1BPP As Boolean = False 'Set true to convert all bitmap to black & white
        Const SCALE_FACTOR As Single = 2.0 'Set 1 for no scale.
        Const JPEG_QUALITY_COLOUR As Integer = 60 'between 1 and 100
        Const JPEG_QUALITY_GRAY As Integer = 30 'between 1 and 100
        Const SOURCE_PATH As String = "c:\test.pdf"
        Const DEST_PATH As String = "c:\result.pdf"

        Using gdPicturePDF As New GdPicturePDF
            Dim oGdPictureImaging As New GdPictureImaging

            If gdPicturePDF.LoadFromFile(SOURCE_PATH, False) = GdPictureStatus.OK Then
                If gdPicturePDF.IsEncrypted() Then
                    If Not gdPicturePDF.SetPassword("") Then
                        MsgBox("password protected document")
                        Return
                    End If
                End If
                gdPicturePDF.SetCompressionForBitonalImage(PdfCompression.PdfCompressionJBIG2)
                gdPicturePDF.SetCompressionForColorImage(PdfCompression.PdfCompressionJPEG)
                For i As Integer = 1 To gdPicturePDF.GetPageCount()
                    gdPicturePDF.SelectPage(i)
                    Dim imageCount As Integer = gdPicturePDF.GetPageImageCount
                    If imageCount > 0 Then
                        For j As Integer = 0 To imageCount - 1
                            Dim ImageID As Integer = gdPicturePDF.ExtractPageImage(j + 1) 'Warning: 1-based
                            If ImageID <> 0 Then
                                If CONVERT_1BPP Then
                                    oGdPictureImaging.ConvertTo1Bpp(ImageID)
                                End If
                                Dim bitDepth As Integer = oGdPictureImaging.GetBitDepth(ImageID)
                                If bitDepth > 8 Then
                                    gdPicturePDF.SetJpegQuality(JPEG_QUALITY_COLOUR)
                                Else
                                    If bitDepth = 8 Then
                                        If oGdPictureImaging.IsGrayscale(ImageID) Then
                                            gdPicturePDF.SetJpegQuality(JPEG_QUALITY_GRAY)
                                        Else
                                            gdPicturePDF.SetJpegQuality(JPEG_QUALITY_COLOUR)
                                        End If
                                    End If
                                End If
                                Dim imageResName As String = gdPicturePDF.GetPageImageResName(j)

                                If SCALE_FACTOR <> 1.0F Then
                                    oGdPictureImaging.Scale(ImageID, 100 / SCALE_FACTOR, Drawing2D.InterpolationMode.HighQualityBicubic)
                                End If

                                gdPicturePDF.ReplaceImage(imageResName, ImageID, False, True)

                                Dim newImageResName As String = gdPicturePDF.AddImageFromGdPictureImage(ImageID, False, False)
                                oGdPictureImaging.ReleaseGdPictureImage(ImageID)
                            End If
                        Next
                    End If
                Next
                If gdPicturePDF.SaveToFile(DEST_PATH, True) = GdPictureStatus.OK Then
                    MsgBox("done")
                Else
                    MsgBox("error saving file file: " + DEST_PATH + ". Status: " + gdPicturePDF.GetStat.ToString)
                End If
                gdPicturePDF.CloseDocument()
            Else
                MsgBox("Can't open file: " + SOURCE_PATH + ". Status: " + gdPicturePDF.GetStat.ToString)
            End If
        End Using

Re: Reducing PDF size by altering the embedded images [UPDA

Posted: Tue Aug 07, 2012 11:57 am
by Chess
Hello,

Thanks for this sample, but I don't find the ReplaceImage function in API (8 or 9).

Code: Select all

gdPicturePDF.ReplaceImage(imageResName, ImageID, False, True)
I try use this sample in C#, with GdPicture .net 9.

Thanks.

Re: Reducing PDF size by altering the embedded images [UPDA

Posted: Tue Aug 07, 2012 2:20 pm
by Loïc
Hello,

This method has been introduced in GdPicture.NET 9.0.12.

Kind regards,

Loïc

Re: Reducing PDF size by altering the embedded images [UPDA

Posted: Fri Aug 31, 2012 3:50 pm
by gharpur
This is great, but I'd really like to be able to recompress images to JPEG2000 format. This doesn't appear to be a supported PDF compression option. Is there any reason for this, and is there any possible workaround?

Thanks.

Re: Reducing PDF size by altering the embedded images [UPDA

Posted: Wed Sep 26, 2012 3:09 pm
by rohrerb
Hello,

How I can reduce the size of my pdf using version 8? I have a pdf which is 708 KB and I can get it down to 104KB, which is good but not the best. Some scanners can take the same file and get it to 24 kb.

Currently I am using the following code to shrink it to 104KB.

Code: Select all

Public Shared Function CompressToNewPDFWith1Bpp(imageBytes As Byte()) As Byte()
        Dim rBytes As Byte() = Nothing

        Dim oPDF As New GdPicturePDF
        oPDF.EnableCompression(Compress:=True)
        oPDF.NewPDF(PDFA:=False)

        Using ms As New IO.MemoryStream(imageBytes)
            Dim workingPDF As New GdPicturePDF
            If workingPDF.LoadFromStream(ms) = GdPictureStatus.OK Then
                For i As Int32 = 0 To workingPDF.GetPageCount - 1
                    workingPDF.SelectPage(i + 1)

                    Dim oPImaging As New GdPictureImaging
                    Dim imageID = workingPDF.RenderPageToGdPictureImageEx(100, RenderFormFields:=False)
                    oPImaging.ConvertTo1Bpp(imageID)

                    oPDF.AddImageFromGdPictureImage(imageID, ImageMask:=False, DrawImage:=True)
                Next

                workingPDF.CloseDocument()
            End If
        End Using

        Using rStream As New IO.MemoryStream
            oPDF.SaveToStream(rStream, PackDocument:=True)

            rBytes = rStream.ToArray
        End Using

        oPDF.CloseDocument()

        Return rBytes
    End Function
Any help would be appreciated.

Thank you
B

Re: Reducing PDF size by altering the embedded images [UPDA

Posted: Wed Apr 24, 2013 8:44 pm
by dan276
Which product/products do I need to buy to be able to do this? I currently have partner because of sync fusion. I want to be able to convert documents to pdf and compress them and save them as a certain PDF format.
I would need the pdf plugin, but would I also need the viewer? I might be interested in the WPF viewer when it comes out, but not now. I mostly just want to manipulate pdfs and tiffs and convert between them.

Re: Reducing PDF size by altering the embedded images [UPDA

Posted: Thu Apr 25, 2013 10:47 pm
by dan276
If my PDF doesn't contain any images, it seems to increase the size of the PDF (about times 3). I'm guessing that it add some settings that are not set in the original that I load in. What should I check?

Re: Reducing PDF size by altering the embedded images [UPDA

Posted: Wed May 08, 2013 10:44 pm
by Loïc
If my PDF doesn't contain any images, it seems to increase the size of the PDF (about times 3).
Hum, this sounds impossible to me. Could you try to open a standalone ticket and describe exactly what you are doing? Obviously you are duplicating embedded bitmap resources instead of optimizing them.

Kind regards,

Loïc

Re: Reducing PDF size by altering the embedded images [UPDA

Posted: Fri Jul 04, 2014 10:08 am
by gebd14
This is really great sample. I can do it. Thanks

Re: Reducing PDF size by altering the embedded images [UPDA

Posted: Thu Mar 05, 2015 10:36 pm
by hagenthorn
Hello Loïc,

Thank you for this code sample. Could you please clarify the following:

1) What happens if I do not replace images by ReplaceImage method, but just open the file, set compression options (method and quality) and save the file? Are the images repacked this way?

2) I really like the GdPictureImaging.PdfCreateFromMultipageTIFF method that accept PDF compression settings as parameters. But that method does not allow to specify the quality settings for JPEG compression. Is there a way to control the JPEG quality for GdPictureImaging.PdfCreateFromMultipageTIFF? If not, could you at least tell what value is used by default?

I use GdPicture10

Thank you in advance.
Alexander.

[Tutorial] ReplaceImageMRC - How to reduce PDF size by altering the embedded images

Posted: Thu Jan 31, 2019 5:22 pm
by Gabriela
Hello,

Here is the "bomb" in reducing PDF size related to embedded images:
https://www.gdpicture.com/guides/gdpicture/web ... geMRC.html