Reference Guide
Programming / Document Conversion / Using MRC compression during the conversion from TIFF to PDF
In This Topic
    Using MRC compression during the conversion from TIFF to PDF
    In This Topic

    Mixed Raster Content (MRC) Compression is the method for compressing compound images. You can read more about this powerful tool on our blog. Below is a useful example of how to convert TIFF images to PDF documents benefiting from the enhanced MRC Compression mechanism.

    This feature is only available in the optional GdPicture.NET Managed PDF Plugin.
    Copy Code
    'We assume that GdPicture has been correctly installed and unlocked.
    Using oGdPictureImaging As GdPictureImaging = New GdPictureImaging()
        Dim imageId As Integer = oGdPictureImaging.CreateGdPictureImageFromFile("Image.tif")
        'Loading an image from a file.
        If oGdPictureImaging.GetStat() = GdPictureStatus.OK Then
            Using oGdPicturePDF As GdPicturePDF = New GdPicturePDF()
                Dim status As GdPictureStatus = oGdPicturePDF.NewPDF()
                If status = GdPictureStatus.OK Then
                    If oGdPictureImaging.TiffIsMultiPage(imageId) = False Then
                        'Setting the resolution for the background layer of the resulting image.
                        oGdPicturePDF.SetMRCImageBackgroundResolution(100)
                        'Enabling smoothing.
                        oGdPicturePDF.SetMRCPreserveSmoothing(True)
                        'You can also set the compression values for images here, for example:
                        oGdPicturePDF.SetCompressionForBitonalImage(PdfCompression.PdfCompressionJBIG2)
                        oGdPicturePDF.SetCompressionForColorImage(PdfCompression.PdfCompressionJPEG2000)
                        'Using the negative value permits to specify better quality factor versus a compression rate.
                        oGdPicturePDF.SetJpeg2000Quality(-60)
                        'Distinguishing by the number of pages.
                        'One-page tiff image.
                        'Adding an image as a resource and drawing it onto a new page using MRC compression.
                        status = oGdPicturePDF.AddImageFromGdPictureImage(imageId, PdfAdvancedImageCompression.PdfAdvancedImageCompressionMRC)
                        If status = GdPictureStatus.OK Then
                            'Saving the new PDF to a file.
                            status = oGdPicturePDF.SaveToFile("output.pdf", True)
                        End If
                    Else
                        'Multi-page tiff image.
                        Dim NumberOfPages As Integer = oGdPictureImaging.TiffGetPageCount(imageId)
                        'Selecting each page in the tiff file and subsequently adding it as a resource to a PDF document
                        'and drawing it on a new page using MRC compression.
                        For i As Integer = 1 To NumberOfPages
                            If (oGdPictureImaging.TiffSelectPage(imageId, i) <> GdPictureStatus.OK) OrElse
                                (oGdPicturePDF.AddImageFromGdPictureImage(imageId, PdfAdvancedImageCompression.PdfAdvancedImageCompressionMRC) <> GdPictureStatus.OK) Then
                                status = oGdPicturePDF.GetStat()
                                Exit For
                            End If
                        Next
                        'Checking whether an error occurred in adding any image to the PDF document.
                        If status = GdPictureStatus.OK Then
                            'Saving the new PDF to a file if no error occured.
                            status = oGdPicturePDF.SaveToFile("output.pdf", True)
                        End If
                    End If
                    oGdPicturePDF.CloseDocument()
                End If
                MessageBox.Show("Finished! Status: " + status.ToString(), "MRC Compression Example", MessageBoxButtons.OK, MessageBoxIcon.Information)
                'Clearing resources.
                oGdPictureImaging.ReleaseGdPictureImage(imageId)
            End Using
        Else
            MessageBox.Show("The image file can't be loaded. Status: " + oGdPictureImaging.GetStat().ToString(), "MRC Compression Example", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End If
    End Using
    Copy Code
    //We assume that GdPicture has been correctly installed and unlocked.
    using (GdPictureImaging oGdPictureImaging = new GdPictureImaging())
    {
        //Loading an image from a file.
        int imageId = oGdPictureImaging.CreateGdPictureImageFromFile("Image.tif");
        if (oGdPictureImaging.GetStat() == GdPictureStatus.OK)
        {
            using (GdPicturePDF oGdPicturePDF = new GdPicturePDF())
            {
                GdPictureStatus status = oGdPicturePDF.NewPDF();
                if (status == GdPictureStatus.OK)
                {
                    //Setting the resolution for the background layer of the resulting image.
                    oGdPicturePDF.SetMRCImageBackgroundResolution(100);
                    //Enabling smoothing.
                    oGdPicturePDF.SetMRCPreserveSmoothing(true);
                    //You can also set the compression values for images here, for example:
                    oGdPicturePDF.SetCompressionForBitonalImage(PdfCompression.PdfCompressionJBIG2);
                    oGdPicturePDF.SetCompressionForColorImage(PdfCompression.PdfCompressionJPEG2000);
                    //Using the negative value permits to specify better quality factor versus a compression rate.
                    oGdPicturePDF.SetJpeg2000Quality(-60);
                    //Distinguishing by the number of pages.
                    if (oGdPictureImaging.TiffIsMultiPage(imageId) == false) //One-page tiff image.
                    {
                        //Adding an image as a resource and drawing it onto a new page using MRC compression.
                        status = oGdPicturePDF.AddImageFromGdPictureImage(imageId, PdfAdvancedImageCompression.PdfAdvancedImageCompressionMRC);
                        if (status == GdPictureStatus.OK)
                        {
                            //Saving the new PDF to a file.
                            status = oGdPicturePDF.SaveToFile("output.pdf", true);
                        }
                    }
                    else //Multi-page tiff image.
                    {
                        int NumberOfPages = oGdPictureImaging.TiffGetPageCount(imageId);
                        //Loop through pages.
                        for (int i = 1; i <= NumberOfPages; i++)
                        {
                            //Selecting each page in the tiff file and subsequently adding it as a resource to a PDF document
                            //and drawing it on a new page using MRC compression.
                            if ((oGdPictureImaging.TiffSelectPage(imageId, i) != GdPictureStatus.OK) ||
                                (oGdPicturePDF.AddImageFromGdPictureImage(imageId, PdfAdvancedImageCompression.PdfAdvancedImageCompressionMRC) != GdPictureStatus.OK))
                            {
                                status = oGdPicturePDF.GetStat();
                                break;
                            }
                        }
                        //Checking whether an error occurred in adding any image to the PDF document.
                        if (status == GdPictureStatus.OK)
                        {
                            //Saving the new PDF to a file if no error occured.
                            status = oGdPicturePDF.SaveToFile("output.pdf", true);
                        }
                    }
                    oGdPicturePDF.CloseDocument();
                }
                MessageBox.Show("Finished! Status: " + status.ToString(), "MRC Compression Example", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //Clearing resources.
                oGdPictureImaging.ReleaseGdPictureImage(imageId);
            }
        }
        else
        {
            MessageBox.Show("The image file can't be loaded. Status: " + oGdPictureImaging.GetStat().ToString(), "MRC Compression Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }