ImageGear .NET
Image to PDF

The following example illustrates how to load an image file and save it as a single PDF page:

C#
Copy Code
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();
           }
       }
VB.NET
Copy Code
Imports System.IO

Imports ImageGear.Core
Imports ImageGear.Formats
Imports ImageGear.Formats.PDF
Imports ImageGear.Evaluation

Public Sub SaveImageAsPDF(inputFilePathName As String, outputFilePathName As String)
         Try
                 Const  FIRST_PAGE As Integer = 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.
                 Dim page As ImGearPage = Nothing
                 Using stream As Stream = New FileStream(inputFilePathName, FileMode.Open, FileAccess.Read)
                          page = ImGearFileFormats.LoadPage(stream, FIRST_PAGE)
                 End Using

                 ' Save page as PDF document to a file.
                 Using stream As Stream = New FileStream(outputFilePathName, FileMode.Create, FileAccess.Write)
                          ImGearFileFormats.SavePage(page, stream, FIRST_PAGE, ImGearSavingModes.OVERWRITE, ImGearSavingFormats.PDF)
                 End Using
         Catch exp As Exception
                 ' Write error to Console window.
                 Console.WriteLine(exp.Message)
         Finally
                 ' Call PDF engine terminating in any case.
                 ImGearPDF.Terminate()
         End Try
End Sub

 

 


©2016. Accusoft Corporation. All Rights Reserved.

Send Feedback