Skip to Main Content

How to Convert Files to PDF with ImageGear

Having the right file conversion tools in place can make or break an application. Developers frequently face the challenge of managing multiple file types within a consolidated workflow. Without effective conversion tools, users are forced to rely on external applications that compromise both efficiency and security.

Out of all the file formats developers must account for, PDFs remain among the most important. The ability to convert a wide variety of document and image file types into PDF format can provide an application with unmatched versatility. In fact, PDF conversion support is one of the keys to unlocking better workflow performance, security, and collaboration.

5 Reasons to Convert Files to PDF


1. PDF Format is Consistent

Sharing documents and images across different devices and operating systems can sometimes create problems if the recipient lacks the up-to-date software necessary to view the file properly. This is a particular challenge with documents created using Microsoft Word since the formatting could look quite different across different versions of the program. Since PDF files are designed to look the same no matter how they’re being viewed, the format is ideal for sharing. Both documents and images can display equally well as PDFs, so converting files into this format is a quick and easy way to make them accessible for viewing.

2. PDF Files Are Easily Compressed

Sharing large image files can be a challenge for many organizations. High-resolution JPEG or TIFF files are often too large to share over email or web-based applications. Converting them to compressed PDFs is a quick way to reduce file size for easier sharing while still retaining a copy of the original file. Since the compressed version is in PDF format, there is less chance of version confusion when someone needs to access the original source image.

3. PDFs Are Widely Supported

Although PDFs once required specialized viewing software, thanks to JavaScript-based libraries like PDF.js, they can now be viewed by a conventional web browser. For all intents and purposes, this has made PDF a universal file format that can be viewed on any device. Converting a file into PDF ensures that it will be accessible to anyone who is granted access to it, regardless of the device or operating system they’re using.

4. PDFs Offer Security Protections

For many organizations, protecting privacy and confidential information is incredibly important. Converting document and image files into PDF format allows them to take advantage of the standard’s security features. Passwords can be set to authorize viewing and editing access to a file, which not only helps to ensure privacy but also limits who can make changes to a file so version control is easier to maintain. Files can also be converted into PDF/A format for secure archival purposes.

5. PDFs Support Annotation Markups

Most PDF viewing solutions support some form of annotation markups, which allows multiple contributors to make notes and comments on a file. Converting documents or images into a PDF facilitates this collaboration while safely preserving the original version of the file for future reference. Since PDF viewers provide a variety of annotation tools, they offer a great deal of flexibility when it comes to marking up images and documents without having to depend upon specialized software. Image and document files with additional annotation layers can also be converted into flattened PDFs for easier viewing.

Converting Files to PDF Using ImageGear

Accusoft’s ImageGear provides an extensive array of file conversion tools that allow developers to easily save multiple document and image file types into PDF format. With these conversion capabilities built into the back end of their applications, developers can help customers streamline their file management.

Converting Microsoft Documents to PDF

ImageGear supports the conversion of multiple Microsoft Office documents, including Word (DOCX/DOC), Excel (XLSX/XLS), and PowerPoint (PPTX/PPT). The conversion engine supports all text elements, raster images, and graphic shapes for Microsoft Office Open XML and Microsoft Office 97-2003 formats. It can convert the entire document into a PDF as well as any designated page or page ranges. The following examples show how this can be done using C#.

Converting Microsoft Word to PDF

To convert a Microsoft Word document in its entirety, the first step involves loading the ImageGear filters to create the input and output instances: 

ImGearFileFormats.Filters.Add(ImGearOffice.CreateWordFormat());
ImGearFileFormats.Filters.Add(ImGearPDF.CreatePDFFormat());

For the next step, the PDF library needs to be initialized:

ImGearPDF.Initialize();

The ImGearFileFormats.LoadDocument method is then used to read all pages of the file:

ImGearDocument igDocument;
using (FileStream fileStream = new FileStream(inputFileName, FileMode.Open,
       FileAccess.Read, FileShare.Read))
{
   igDocument = ImGearFileFormats.LoadDocument(fileStream);
}
Finally, the ImGearFileFormats.SaveDocument method is used to save the output PDF: 
using (FileStream fileStream = new FileStream(outputFileName, FileMode.Create,
       FileAccess.ReadWrite))
{
   ImGearFileFormats.SaveDocument(igDocument, fileStream, 0,
       ImGearSavingModes.OVERWRITE, ImGearSavingFormats.PDF, null);
}

Converting Microsoft Excel and PowerPoint to PDF

The process for converting Excel and PowerPoint files follows the same basic format as converting Word files. First, initialize the input, then modify the sample code from above for the appropriate formats.

To initialize Excel:

ImGearFileFormats.Filters.Add(ImGearOffice.CreateExcelFormat());

To modify sample’s open file dialog for XLSX/XLS extensions:

ofd.Filter = @"DOCX files (*.docx)|*.docx|XLSX files 
(*.xlsx)|*.xlsx|XLS files (*.xls)|*.xls";

To initialize PowerPoint:


ImGearFileFormats.Filters.Add(ImGearOffice.CreatePowerPointFormat());

To modify sample’s open file dialog for PPTX/PPT extensions:

ofd.Filter = @"DOCX files (*.docx)|*.docx|PPTX files 
(*.pptx)|*.pptx|PPT files (*.ppt)|*.ppt";

Converting an Image File to PDF

ImageGear PDF supports the conversion of multiple image types into PDF format just as easily as it converts documents, but the process looks a bit different in code. After initializing PDF support for ImageGear.NET, the following C# example can be used to load an image file and then save it as a PDF page. The conversion process can be used for any file format that ImageGear supports.

using System;
using System.IO;

using ImageGear.Core;
using ImageGear.Formats;
using ImageGear.Formats.PDF;
using ImageGear.Evaluation;

public void SaveImageAsPDF(string inputFilePathName, string outputFilePathName)
       {
           try
           {
               const int FIRST_PAGE = 0;

               // Initialize evaluation license.
               ImGearEvaluationManager.Initialize();
               ImGearEvaluationManager.Mode = ImGearEvaluationMode.Watermark;

               // Initialize common formats.
               ImageGear.Formats.ImGearCommonFormats.Initialize();

               // Add support for PDF and PS files.
               ImGearFileFormats.Filters.Insert(0, ImGearPDF.CreatePDFFormat());
               ImGearPDF.Initialize();

               // Load required page from a file.
               ImGearPage page = null;
               using (Stream stream = new FileStream(inputFilePathName, FileMode.Open, FileAccess.Read))
                   page = ImGearFileFormats.LoadPage(stream, FIRST_PAGE);

               // Save page as PDF document to a file.
               using (Stream stream = new FileStream(outputFilePathName, FileMode.Create, FileAccess.Write))
                   ImGearFileFormats.SavePage(page, stream, FIRST_PAGE, ImGearSavingModes.OVERWRITE, ImGearSavingFormats.PDF);
           }
           catch (Exception exp)
           {
               // Write error to Console window.
               Console.WriteLine(exp.Message);
           }
           finally
           {
               // Call PDF engine terminating in any case.
               ImGearPDF.Terminate();
           }
       }

 

Add Conversion Flexibility to Your Application with ImageGear

Accusoft’s ImageGear provides applications with comprehensive conversion, annotation, and viewing support for PDF files. As part of the broader ImageGear collection, it also delivers powerful image processing capabilities and support for multiple document and image file types. These features can help turn any application into a robust document management platform capable of streaming workflows and empowering collaboration.

If you’re ready to see how the SDK will function as part of your development environment, start your free trial and get straight to the code.