In This Topic
Programming / PDF / Converting TIFF images to PDF documents

Converting TIFF images to PDF documents

In This Topic

TIFF images and PDF documents are very important formats. For documents, they are probably the most important. Converting from one to another is probably one of the basic needs of any document management system.

In this tutorial, we will see how to convert TIFF images to PDF documents using GdPicture.NET.

  • Now it is 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.", "TIFF to PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Information)
            status = oConverter.SaveAsPDF("output.pdf", PdfConformance.PDF)
            If status = GdPictureStatus.OK Then
                MessageBox.Show("The file has been saved successfully.", "TIFF to PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show("The file has failed to save. Status: " + status.ToString(), "TIFF to PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            MessageBox.Show("The file has failed to load. Status: " + status.ToString(), "TIFF to PDF 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.", "TIFF to PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Information);
            status = oConverter.SaveAsPDF("output.pdf", PdfConformance.PDF);
            if (status == GdPictureStatus.OK)
            {
                MessageBox.Show("The file has been saved successfully.", "TIFF to PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("The file has failed to save. Status: " + status.ToString(), "TIFF to PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        else
        {
            MessageBox.Show("The file has failed to load. Status: " + status.ToString(), "TIFF to PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
  • You can compare it with another approach by converting page by page using 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
            If oGdPictureImaging.TiffIsMultiPage(imageId) = False Then
                'One-page tiff image.
                'Adding an image as a resource and drawing it onto a new page.
                oGdPicturePDF.AddImageFromGdPictureImage(imageId, PdfAdvancedImageCompression.PdfAdvancedImageCompressionMRC)
                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 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, PdfAdvancedImageCompression.PdfAdvancedImageCompressionMRC)
                    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 a PDF document.
                If savePDF Then
                    status = oGdPicturePDF.SaveToFile("output.pdf")
                End If
            End If
            oGdPicturePDF.CloseDocument()
        End If
        MessageBox.Show("Finished! Status: " + status.ToString(), "Converting TIFF to PDF 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(), "Converting TIFF to PDF 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)
        {
            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, PdfAdvancedImageCompression.PdfAdvancedImageCompressionMRC);
                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 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, PdfAdvancedImageCompression.PdfAdvancedImageCompressionMRC);
                    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 a PDF document.
                if (savePDF)
                {
                    status = oGdPicturePDF.SaveToFile("output.pdf");
                }
            }
            oGdPicturePDF.CloseDocument();
        }
        MessageBox.Show("Finished! Status: " + status.ToString(), "Converting TIFF to PDF 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(), "Converting TIFF to PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    oGdPictureImaging.Dispose();