Some test text!

Discord Logo

Chat with us

PDFTron is now Apryse, learn more here.

PDF image JBIG2 compression in VB (lossy & lossless)

More languages

More languages
Java (Android)
C# (.NET Core)
JS (Node.js)
C# (UWP)
C# (Xamarin)

Sample VB code for using PDFTron SDK to recompress bitonal (black and white) images in existing PDF documents using JBIG2 compression (lossless or lossy). The sample is intended to show how to specify hint information for the image encoder and is not meant to be a generic PDF optimization tool. To demonstrate the possible compression rates, we recompressed a document containing 17 scanned pages. The original input document is ~1.4MB and is using standard CCITT Fax compression. Lossless JBIG2 compression shrunk the filesize to 641KB, while lossy JBIG2 compression shrunk it to 176KB. Learn more about our VB PDF Library.

Get Started Samples Download

To run this sample, get started with a free trial of Apryse SDK.

' Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
Imports System.Windows.Forms

Imports pdftron
Imports pdftron.Common
Imports pdftron.Filters
Imports pdftron.SDF
Imports pdftron.PDF

' This sample project illustrates how to recompress bi-tonal images in an 
' existing PDF document using JBIG2 compression. The sample is not intended 
' to be a generic PDF optimization tool.
' You can download a sample scanned document using the following link:
' Also a sample page compressed using CCITT Fax compression is located under 
' 'PDFNet/Samples/TestFiles' folder.

Module Program
    Dim pdfNetLoader As PDFNetLoader
    Sub New()
        pdfNetLoader = pdftron.PDFNetLoader.Instance()
    End Sub

    Sub Main(args As String())
        ' Initialize PDFNet before calling any other PDFNet function.

        Dim pdfdoc As PDFDoc = New PDFDoc("../../../../TestFiles/US061222892-a.pdf")
        Dim cos_doc As SDFDoc = pdfdoc.GetSDFDoc()
        Dim num_objs As Integer = cos_doc.XRefSize()

        For i As Integer = 1 To num_objs - 1
            Dim obj As Obj = cos_doc.GetObj(i)
            If Not (obj Is Nothing Or obj.IsFree()) Then
                ' Process only images
                If obj.IsStream() Then
                    Dim itr As DictIterator = obj.Find("Subtype")
                    If itr.HasNext() Then
                        If itr.Value().GetName() = "Image" Then
                            Dim input_image As pdftron.PDF.Image = New pdftron.PDF.Image(obj)
                            Dim new_image As pdftron.PDF.Image = Nothing

                            ' Process only gray-scale images
                            If input_image.GetComponentNum() = 1 Then
                                Dim bpc As Integer = input_image.GetBitsPerComponent()
                                If bpc = 1 Then
                                    Dim reader As FilterReader = New FilterReader(obj.GetDecodedStream())

                                    Dim hint_set As ObjSet = New ObjSet
                                    Dim hint As Obj = hint_set.CreateArray()
                                    ' hint.PushBackName("Lossless")

                                    new_image = pdftron.PDF.Image.Create(cos_doc, reader, input_image.GetImageWidth(), input_image.GetImageHeight(), 1, ColorSpace.CreateDeviceGray(), hint)
                                End If

                                If Not new_image Is Nothing Then
                                    Dim new_img_obj As Obj = new_image.GetSDFObj()

                                    ' Copy any important entries from the image dictionary
                                    itr = obj.Find("ImageMask")
                                    If itr.HasNext() Then
                                        new_img_obj.Put("ImageMask", itr.Value())
                                    End If
                                    itr = obj.Find("Mask")
                                    If itr.HasNext() Then
                                        new_img_obj.Put("Mask", itr.Value())
                                    End If
                                    cos_doc.Swap(i, new_image.GetSDFObj().GetObjNum())
                                End If
                            End If
                        End If
                    End If
                End If
            End If

        pdfdoc.Save("../../../../TestFiles/Output/US061222892_JBIG2.pdf", SDFDoc.SaveOptions.e_remove_unused)
    End Sub
End Module