In This Topic
Programming / Document Conversion / Using JBIG2 compression when converting TIFF images to PDF documents

Using JBIG2 compression when converting TIFF images to PDF documents

In This Topic

In this tutorial, we will see how to convert TIFF images to PDF documents using GdPicture.NET. We take advantage of a lossless JBIG2 compression for bitonal images.

  • The first example shows you a very quick and easy one-step operation using the GdPictureDocumentConverter class.
    Copy Code
    'We assume that GdPicture has been correctly installed and unlocked.
    
    Using oConverter As GdPictureDocumentConverter = New GdPictureDocumentConverter()
    
        Dim status As GdPictureStatus = oConverter.LoadFromFile("input.tif", GdPicture14.DocumentFormat.DocumentFormatTIFF)
    
        If status = GdPictureStatus.OK Then
    
            MessageBox.Show("The file has been loaded successfully.", "JBIG2 Compression Example", MessageBoxButtons.OK, MessageBoxIcon.Information)
    
            'Setting the required compression parameter.
    
            oConverter.PdfBitonalImageCompression = PdfCompression.PdfCompressionJBIG2
    
            status = oConverter.SaveAsPDF("output.pdf", PdfConformance.PDF)
    
            If status = GdPictureStatus.OK Then
    
                MessageBox.Show("The file has been saved successfully.", "JBIG2 Compression Example", MessageBoxButtons.OK, MessageBoxIcon.Information)
    
            Else
    
                MessageBox.Show("The file has failed to save. Status: " + status.ToString(), "JBIG2 Compression Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
    
            End If
    
        Else
    
            MessageBox.Show("The file has failed to load. Status: " + status.ToString(), "JBIG2 Compression Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
    
        End If
    
    End Using
    Copy Code
    //We assume that GdPicture has been correctly installed and unlocked.
    
    using (GdPictureDocumentConverter oConverter = new GdPictureDocumentConverter())
    
    {
    
        GdPictureStatus status = oConverter.LoadFromFile("input.tif", GdPicture14.DocumentFormat.DocumentFormatTIFF);
    
        if (status == GdPictureStatus.OK)
    
        {
    
            MessageBox.Show("The file has been loaded successfully.", "JBIG2 Compression Example", MessageBoxButtons.OK, MessageBoxIcon.Information);
    
            //Setting the required compression parameter.
    
            oConverter.PdfBitonalImageCompression = PdfCompression.PdfCompressionJBIG2;
    
            status = oConverter.SaveAsPDF("output.pdf", PdfConformance.PDF);
    
            if (status == GdPictureStatus.OK)
    
            {
    
                MessageBox.Show("The file has been saved successfully.", "JBIG2 Compression Example", MessageBoxButtons.OK, MessageBoxIcon.Information);
    
            }
    
            else
    
            {
    
                MessageBox.Show("The file has failed to save. Status: " + status.ToString(), "JBIG2 Compression Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
    
            }
    
        }
    
        else
    
        {
    
            MessageBox.Show("The file has failed to load. Status: " + status.ToString(), "JBIG2 Compression Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
    
        }
    
    }
  • The second example demonstrates it using another approach by converting page by page to images through the GdPictureImaging class.
    Copy Code
    'We assume that GdPicture has been correctly installed and unlocked.
    
    Dim oGdPictureImaging As New GdPictureImaging()
    
    'Setting the option for subsequently opened multi-tiff images with read-only capabilities.
    
    oGdPictureImaging.TiffOpenMultiPageForWrite(False)
    
    'Loading an image from a file.
    
    Dim imageId As Integer = oGdPictureImaging.CreateGdPictureImageFromFile("Image.tif")
    
    If oGdPictureImaging.GetStat() = GdPictureStatus.OK Then
    
        Dim status As GdPictureStatus = GdPictureStatus.OK
    
        Dim oGdPicturePDF As New GdPicturePDF()
    
        status = oGdPicturePDF.NewPDF()
    
        If status = GdPictureStatus.OK Then
    
            oGdPicturePDF.EnableCompression(True)
    
            'Enabling the JBIG2 compression.
    
            oGdPicturePDF.SetCompressionForBitonalImage(PdfCompression.PdfCompressionJBIG2)
    
            If oGdPictureImaging.TiffIsMultiPage(imageId) = False Then
    
                'One-page tiff image.
    
                'Adding image as a resource and drawing it onto a new page.
    
                oGdPicturePDF.AddImageFromGdPictureImage(imageId, False, True)
    
                If oGdPicturePDF.GetStat() = GdPictureStatus.OK Then
    
                    'Saving the new PDF to a file.
    
                    status = oGdPicturePDF.SaveToFile("output.pdf")
    
                Else
    
                    status = oGdPicturePDF.GetStat()
    
                End If
    
            Else
    
                'Multi-page tiff image.
    
                Dim NumberOfPages As Integer = oGdPictureImaging.TiffGetPageCount(imageId)
    
                Dim savePDF As Boolean = True
    
                'Loop through pages.
    
                For i As Integer = 1 To NumberOfPages
    
                    'Selecting each page in the tiff file.
    
                    oGdPictureImaging.TiffSelectPage(imageId, i)
    
                    'Adding the selected tiff page as a resource to a PDF document and drawing it on a new page.
    
                    oGdPicturePDF.AddImageFromGdPictureImage(imageId, False, True)
    
                    If oGdPicturePDF.GetStat() <> GdPictureStatus.OK Then
    
                        status = oGdPicturePDF.GetStat()
    
                        'Raising a flag to cancel PDF saving.
    
                        savePDF = False
    
                        Exit For
    
                    End If
    
                Next
    
                'Checking whether any error occurred in adding any image to the PDF document.
    
                If savePDF Then
    
                    status = oGdPicturePDF.SaveToFile("output.pdf")
    
                End If
    
            End If
    
            oGdPicturePDF.CloseDocument()
    
        End If
    
        MessageBox.Show("Finished! Status: " + status.ToString(), "JBIG2 Compression Example", MessageBoxButtons.OK, MessageBoxIcon.Information)
    
        'Clearing resources.
    
        oGdPictureImaging.ReleaseGdPictureImage(imageId)
    
        oGdPicturePDF.Dispose()
    
    Else
    
        MessageBox.Show("The image file can't be loaded. Status: " + oGdPictureImaging.GetStat().ToString(), "JBIG2 Compression Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
    
    End If
    
    oGdPictureImaging.Dispose()
    Copy Code
    //We assume that GdPicture has been correctly installed and unlocked.
    
    GdPictureImaging oGdPictureImaging = new GdPictureImaging();
    
    //Setting the option for subsequently opened multi-tiff images with read-only capabilities.
    
    oGdPictureImaging.TiffOpenMultiPageForWrite(false);
    
    //Loading an image from a file.
    
    int imageId = oGdPictureImaging.CreateGdPictureImageFromFile("Image.tif");
    
    if (oGdPictureImaging.GetStat() == GdPictureStatus.OK)
    
    {
    
        GdPictureStatus status = GdPictureStatus.OK;
    
        GdPicturePDF oGdPicturePDF = new GdPicturePDF();
    
        status = oGdPicturePDF.NewPDF();
    
        if (status == GdPictureStatus.OK)
    
        {
    
            oGdPicturePDF.EnableCompression(true);
    
            oGdPicturePDF.SetCompressionForBitonalImage(PdfCompression.PdfCompressionJBIG2); //Enabling the JBIG2 compression.
    
            if (oGdPictureImaging.TiffIsMultiPage(imageId) == false) //One-page tiff image.
    
            {
    
                //Adding an image as a resource and drawing it onto a new page.
    
                oGdPicturePDF.AddImageFromGdPictureImage(imageId, false, true);
    
                if (oGdPicturePDF.GetStat() == GdPictureStatus.OK)
    
                {
    
                    //Saving the new PDF to a file.
    
                    status = oGdPicturePDF.SaveToFile("output.pdf");
    
                }
    
                else
    
                {
    
                    status = oGdPicturePDF.GetStat();
    
                }
    
            }
    
            else //Multi-page tiff image.
    
            {
    
                int NumberOfPages = oGdPictureImaging.TiffGetPageCount(imageId);
    
                bool savePDF = true;
    
                //Loop through pages.
    
                for (int i = 1; i <= NumberOfPages; i++)
    
                {
    
                    //Selecting each page in the tiff file.
    
                    oGdPictureImaging.TiffSelectPage(imageId, i);
    
                    //Adding the selected tiff page as a resource to a PDF document and drawing it on a new page.
    
                    oGdPicturePDF.AddImageFromGdPictureImage(imageId, false, true);
    
                    if (oGdPicturePDF.GetStat() != GdPictureStatus.OK)
    
                    {
    
                        status = oGdPicturePDF.GetStat();
    
                        //Raising a flag to cancel PDF saving.
    
                        savePDF = false;
    
                        break;
    
                    }
    
                }
    
                //Checking whether any error occurred in adding any image to the PDF document.
    
                if (savePDF)
    
                {
    
                    status = oGdPicturePDF.SaveToFile("output.pdf");
    
                }
    
            }
    
            oGdPicturePDF.CloseDocument();
    
        }
    
        MessageBox.Show("Finished! Status: " + status.ToString(), "JBIG2 Compression Example", MessageBoxButtons.OK, MessageBoxIcon.Information);
    
        //Clearing resources.
    
        oGdPictureImaging.ReleaseGdPictureImage(imageId);
    
        oGdPicturePDF.Dispose();
    
    }
    
    else
    
    {
    
        MessageBox.Show("The image file can't be loaded. Status: " + oGdPictureImaging.GetStat().ToString(), "JBIG2 Compression Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
    
    }
    
    oGdPictureImaging.Dispose();