ImageGear .NET
Console Application

In this tutorial, you will configure a C# or VB.NET project for a console application and use ImageGear .NET PDF capabilities. You will also learn how to open a PDF or PS file and save it as a new file.

The following tutorial refers specifically to 64-bit installations; for 32-bit installations:

  • Your project should already be set to compile to target Debug and x86, and you should have the directory: $YOURLOCALPROJ\bin\x86\Debug\.
  • Throughout these instructions, replace x64 with x86.
  • The 32-bit ImageGear binaries are found in $INSTALLDIR\ImageGear for .NET v22\Bin\

Using the desired version of Visual Studio (2010 or later):

  1. Create a new "Console Application" project, using C# or VB.NET, and name the project: my_first_PDF_project
  2. If you installed ImageGear .NET 64-bit, using the Configuration Manager, create a new project platform (if you don't have one already) for x64. Make sure your project is set to compile targeting Debug and x64. Confirm you now have  $YOURLOCALPROJ\bin\x64\Debug\, and if it's not there, create it.
  3. Add references and required resources into your projects in one of the following ways:
    • Recommended: use our NuGet Packages. For this project, you need the following package:
      Accusoft.ImageGear.PDF.nupkg (https://www.nuget.org/packages/Accusoft.ImageGear.PDF/ )
    • Manually:
      • Copy all files (and folders) inside $INSTALLDIR\ImageGear for .NET v22 64-bit\Bin\ to your local output bin directory in your project (i.e., $YOURLOCALPROJ\bin\x64\Debug\ ).
      • Add the following references to your project from $YOURLOCALPROJ\bin\x64\Debug\:
        • ImageGear22.Core.dll
        • ImageGear22.Evaluation.dll
        • ImageGear.22.Art.dll
        • ImageGear22.Formats.Common.dll
        • ImageGear22.Formats.Pdf.dll
        • ImageGear22.Formats.Vector.dll

    Your output target directory should be set to $YOURLOCALPROJ\bin\x64\Debug\  .

  4. At this point all necessary assembly references and resources have been added to the project. The following code snippet can be used to load a PDF and save it as new file. Make sure to replace C:\PATHTOPDF\ with your own PDF file name and path:
    C#
    Copy Code
    using System.IO;
    
    using ImageGear.Core;
    using ImageGear.Formats;
    using ImageGear.Formats.PDF;
    using ImageGear.Evaluation;
    
    namespace my_first_PDF_project
    {
       class Program
       {
           public void SaveAs(string fileIn, string fileOut)
           {
               // Initialize evaluation license.
               ImGearEvaluationManager.Initialize();
               ImGearEvaluationManager.Mode = ImGearEvaluationMode.Watermark;
    
               // Initialize common formats.
               ImGearCommonFormats.Initialize();
    
               // Add support for PDF and PS files.
               ImGearFileFormats.Filters.Insert(0, ImGearPDF.CreatePDFFormat());
               ImGearFileFormats.Filters.Insert(0, ImGearPDF.CreatePSFormat());
               ImGearPDF.Initialize();
    
               using (FileStream inStream = new FileStream(fileIn, FileMode.Open))
               {
                   using (FileStream outStream = new FileStream(fileOut, FileMode.Create))
                   {
                       // Load PDF document.
                       ImGearDocument igDocument = ImGearFileFormats.LoadDocument(inStream);
    
                       // Save PDF.
                       ImGearPDFSaveOptions pdfOptions = new ImGearPDFSaveOptions();
                       ImGearFileFormats.SaveDocument(igDocument, outStream, 0, ImGearSavingModes.OVERWRITE, ImGearSavingFormats.PDF, pdfOptions);
                   }
               }
               // Dispose of the PDF component.
               ImGearPDF.Terminate();   
           }
    
           static void Main(string[] args)
           {
               Program myProgram = new Program();
               myProgram.SaveAs(@"C:\PATHTOPDF\FILENAME.pdf",@"C:\PATHTOPDF\NEWNAME.pdf");
           }
       }
    }
    VB.NET
    Copy Code
    Imports System.IO
    
    Imports ImageGear.Core
    Imports ImageGear.Formats
    Imports ImageGear.Formats.PDF
    Imports ImageGear.Evaluation
    
    Module Module1
       Public Sub SaveAs(fileIn As String, fileOut As String)
           ' Initialize evaluation license.
           ImGearEvaluationManager.Initialize()
           ImGearEvaluationManager.Mode = ImGearEvaluationMode.Watermark
    
           ImGearCommonFormats.Initialize()
    
           ' Add support for PDF and PS files.
           ImGearFileFormats.Filters.Insert(0, ImGearPDF.CreatePDFFormat())
           ImGearFileFormats.Filters.Insert(0, ImGearPDF.CreatePSFormat())
           ImGearPDF.Initialize()
    
           Using inStream As New FileStream(fileIn, FileMode.Open)
               Using outStream As New FileStream(fileOut, FileMode.Create)
                   ' Load PDF document.
                   Dim igDocument As ImGearDocument = ImGearFileFormats.LoadDocument(inStream)
    
                   ' Save PDF.
                   Dim pdfOptions As New ImGearPDFSaveOptions()
                   ImGearFileFormats.SaveDocument(igDocument, outStream, 0, ImGearSavingModes.OVERWRITE, ImGearSavingFormats.PDF, pdfOptions)
               End Using
           End Using
           ' Dispose of the PDF component.
           ImGearPDF.Terminate()
       End Sub
    
       Sub Main()
           SaveAs("C:\PATHTOPDF\FILENAME.pdf", "C:\PATHTOPDF\NEWNAME.pdf.pdf")
       End Sub
    
    End Module
  5. Now, let's go over some of the important areas in the sample code with more detail. To initialize and support processing of PDF and PS files we need:
    C#
    Copy Code
    // Initialize evaluation license.
    ImGearEvaluationManager.Initialize();
    ImGearEvaluationManager.Mode = ImGearEvaluationMode.Watermark;
    
    // Initialize common formats.
    ImGearCommonFormats.Initialize();
    
    // Add support for PDF and PS files.
    ImGearFileFormats.Filters.Insert(0, ImGearPDF.CreatePDFFormat());
    ImGearFileFormats.Filters.Insert(0, ImGearPDF.CreatePSFormat());
    ImGearPDF.Initialize();
    VB.NET
    Copy Code
    ' Initialize evaluation license.
    ImGearEvaluationManager.Initialize()
    ImGearEvaluationManager.Mode = ImGearEvaluationMode.Watermark
    
    ' Initialize common formats.
    ImGearCommonFormats.Initialize()
    
    ' Add support for PDF and PS files.
    ImGearFileFormats.Filters.Insert(0, ImGearPDF.CreatePDFFormat())
    ImGearFileFormats.Filters.Insert(0, ImGearPDF.CreatePSFormat())
    ImGearPDF.Initialize()
  6. There is one main data structure that is used in this sample code: The ImGearDocument that holds the entire loaded document.
    C#
    Copy Code
    // Load PDF document.
    ImGearDocument igDocument = ImGearFileFormats.LoadDocument(inStream);
    VB.NET
    Copy Code
    ' Load PDF document.
    Dim igDocument As ImGearDocument = ImGearFileFormats.LoadDocument(inStream)
  7. You can save the loaded document using:
    C#
    Copy Code
    // Save PDF.
    ImGearPDFSaveOptions pdfOptions = new ImGearPDFSaveOptions();
    ImGearFileFormats.SaveDocument(igDocument, outStream, 0, ImGearSavingModes.OVERWRITE, ImGearSavingFormats.PDF, pdfOptions);
    VB.NET
    Copy Code
    ' Save PDF.
    Dim pdfOptions As New ImGearPDFSaveOptions()
    ImGearFileFormats.SaveDocument(igDocument, outStream, 0, ImGearSavingModes.OVERWRITE, ImGearSavingFormats.PDF, pdfOptions)

This sample project was created as a simple introduction to using the PDF functionality in ImageGear. For systems designed for production environments, you need to configure your projects more efficiently by only adding the resources that you need. For more information, refer to Deploying Your Product.

 

 


©2016. Accusoft Corporation. All Rights Reserved.

Send Feedback