Reducing PDF size by altering the embedded images [UPDATED]

Discussions about PDF management.
Post Reply
User avatar
Loïc
Site Admin
Posts: 5592
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Reducing PDF size by altering the embedded images [UPDATED]

Post by Loïc » Sat Feb 18, 2012 2:13 pm

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

Chess
Posts: 22
Joined: Mon Apr 23, 2012 6:02 pm

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

Post by Chess » Tue Aug 07, 2012 11:57 am

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.

User avatar
Loïc
Site Admin
Posts: 5592
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

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

Post by Loïc » Tue Aug 07, 2012 2:20 pm

Hello,

This method has been introduced in GdPicture.NET 9.0.12.

Kind regards,

Loïc

gharpur
Posts: 3
Joined: Wed Jun 13, 2012 11:24 am

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

Post by gharpur » Fri Aug 31, 2012 3:50 pm

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.

rohrerb
Posts: 8
Joined: Wed Sep 26, 2012 3:06 pm

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

Post by rohrerb » Wed Sep 26, 2012 3:09 pm

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

dan276
Posts: 20
Joined: Fri Mar 15, 2013 1:07 am

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

Post by dan276 » Wed Apr 24, 2013 8:44 pm

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.

dan276
Posts: 20
Joined: Fri Mar 15, 2013 1:07 am

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

Post by dan276 » Thu Apr 25, 2013 10:47 pm

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?

User avatar
Loïc
Site Admin
Posts: 5592
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

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

Post by Loïc » Wed May 08, 2013 10:44 pm

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

gebd14
Posts: 1
Joined: Fri Jul 04, 2014 7:43 am

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

Post by gebd14 » Fri Jul 04, 2014 10:08 am

This is really great sample. I can do it. Thanks

hagenthorn
Posts: 1
Joined: Thu Mar 05, 2015 10:18 pm

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

Post by hagenthorn » Thu Mar 05, 2015 10:36 pm

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest