How to Convert TIFF to PDF in C#

How to Convert TIFF to PDF in C#

This quick tutorial explains how to convert TIFF images to PDF format using C#. It details the necessary requirements to configure your environment and provides step-by-step guidance to accomplish the conversion.

Benefits of Converting TIFF to PDF

  1. Universal Compatibility:
    • PDF files can be viewed on virtually any device or platform.
  2. Document Preservation:
    • Maintains the original quality and layout of the TIFF file.
  3. Enhanced Security:
    • PDFs allow for encryption and password protection.

Prerequisites: Preparing the Environment

  1. Set up Visual Studio or any compatible .NET IDE.
  2. Install the Aspose.Imaging library via NuGet Package Manager.

Step-by-Step Guide to Convert TIFF to PDF

Step 1: Configure the Project

Install the Aspose.Imaging library using NuGet.

Install-Package Aspose.Imaging

Step 2: Include the Namespace

Add a reference to the Aspose.Imaging namespace to access its functionalities.

using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Tiff;
using Aspose.Imaging.ImageOptions;

Step 3: Load the TIFF File

Load the source TIFF file using the Image.Load method.

using (Image TifImage = Image.Load("Original.tif"))
{
    // Further processing steps follow here
}

Step 4: Create PDF Options

Create and initialize a PdfOptions object to customize the output settings.

TiffImage tiffImage = (TiffImage)TifImage;
PdfOptions pdfOptions = new PdfOptions()
{
    ResolutionSettings = new ResolutionSetting(
        tiffImage.HorizontalResolution,
        tiffImage.VerticalResolution
    )
};

Step 5: Save the PDF

Save the generated PDF file to disk using the Save method.

TifImage.Save("ExportedTiff.pdf", pdfOptions);

Complete Code Example to Convert TIFF to PDF

Here’s a complete example that demonstrates how to convert a TIFF file to a PDF:

using (Image TifImage = Image.Load("Original.tif"))
{
    TiffImage tiffImage = (TiffImage)TifImage;

    PdfOptions pdfOptions = new PdfOptions()
    {
        ResolutionSettings = new ResolutionSetting(
            tiffImage.HorizontalResolution,
            tiffImage.VerticalResolution
        )
    };

    TifImage.Save("ExportedTiff.pdf", pdfOptions);
}

Additional Information

  • The PdfOptions class also includes properties like PageSize and MultiPageOptions, enabling more customization.
  • You can set compression and quality settings via PdfOptions for a tailored output.

Conclusion

This tutorial has illustrated how to convert TIFF images to PDF format using C#. With just a few lines of code, you can easily manage and manipulate image files. For more functionality, explore additional image manipulation operations such as compressing images or altering formats.

 English