ImageGear v26.3 - Updated
Developer Guide / How to Work with ... / PDF / How to... / Convert... / Image to PDF
In This Topic
    Image to PDF
    In This Topic

    The following example illustrates how to load an image file and save it as a single PDF page. Any image file format supported by ImageGear can be used.

    PDF support needs to be initialized first for this snippet to work. To get familiar with initializing IGNET, initializing PDF support, loading a PDF, saving a PDF, and terminating PDF support, refer to our samples on github.

    C#

    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());
            ImGearFileFormats.Filters.Insert(0, ImGearPDF.CreatePSFormat());
            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();
        }
    }