ImageGear .NET v24.12 - Updated
Tutorial: Create Your First Office Project - Console Application
User Guide > How to Work with... > Office > Getting Started with ImageGear Office > Tutorial: Create Your First Office Project - Console Application

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

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_Office_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\, if not, create it.
  3. Copy the OfficeCore 64-bit dependencies found in $INSTALLDIR\ImageGear .NET v24 64-bit\Bin\ to the same directory where ImageGear24.Formats.Office.dll is located.
  4. 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 packages:
    • Manually:
      1. Copy all files (and folders) inside $INSTALLDIR\ImageGear .NET v24 64-bit\Bin\ to your local output bin directory in your project (i.e., $YOURLOCALPROJ\bin\x64\Debug\ ).
      2. Add the following references to your project from $YOURLOCALPROJ\bin\x64\Debug\:
        • ImageGear24.Core.dll
        • ImageGear24.Evaluation.dll
        • ImageGear24.Formats.Common.dll
        • ImageGear24.Formats.Office.dll
        • ImageGear24.Formats.Pdf.dll
        • ImageGear24.Formats.Vector.dll
        • ImageGear24.Presentation.dll
Your output target directory should be set to $YOURLOCALPROJ\bin\x64\Debug\  .
  1. Before you can use the Office and PDF functionality, you need to add support for those formats. Add the following code to set up evaluation licensing and add support for common image formats, Office files, and PDF files.
    C# Example
    Copy Code
    // Initialize evaluation license.
    ImGearEvaluationManager.Initialize();
    ImGearEvaluationManager.Mode = ImGearEvaluationMode.Watermark;
    
    // Initialize common formats.
    ImGearCommonFormats.Initialize();
    
    // Add support for Office file types.
    ImGearFileFormats.Filters.Add(ImGearOffice.CreateWordFormat());
    ImGearFileFormats.Filters.Add(ImGearOffice.CreatePowerPointFormat());
    ImGearFileFormats.Filters.Add(ImGearOffice.CreateExcelFormat());
    
    // Add support for PDF files.
    ImGearFileFormats.Filters.Add(ImGearPDF.CreatePDFFormat());
    ImGearPDF.Initialize();
    VB.NET Example
    Copy Code
    ' Initialize evaluation license.
    ImGearEvaluationManager.Initialize()
    ImGearEvaluationManager.Mode = ImGearEvaluationMode.Watermark
    
    ' Initialize common formats.
    ImGearCommonFormats.Initialize()
    
    ' Add support for Office file types.
    ImGearFileFormats.Filters.Add(ImGearOffice.CreateWordFormat())
    ImGearFileFormats.Filters.Add(ImGearOffice.CreatePowerPointFormat())
    ImGearFileFormats.Filters.Add(ImGearOffice.CreateExcelFormat())
    
    ' Add support for PDF files.
    ImGearFileFormats.Filters.Add(ImGearPDF.CreatePDFFormat())
    ImGearPDF.Initialize()
  1. For a web application, you can optionally pass the location of OfficeCore binaries, PDF binaries and resources directly in the web.config file of the application:
    Web.config
    Copy Code
    <appSettings>
      ......
      <add key="igOfficeCoreDir" value="C:\Users\Public\Documents\Accusoft\ImageGear.NET v24\Bin\OfficeCore" />
      <add key="igPdfResources" value="C:\Users\Public\Documents\Accusoft\ImageGear.NET v24\Bin" />
      ......
    </appSettings>
  2. Now that you have added support for the file types you will be using, you can actually work with them. The following code enables you to load a Word document into an ImGearDocument, which will hold the entire loaded document. You can save that ImGearDocument as a PDF.
    C# Example
    Copy Code
    using (FileStream inStream = new FileStream(fileIn, FileMode.Open, FileAccess.Read))
    using (FileStream outStream = new FileStream(fileOut, FileMode.Create, FileAccess.Write))
    {
      int startPageNumber = 0;
    
      // Load Office document.
      ImGearDocument igDocument = ImGearFileFormats.LoadDocument(inStream);
    
      // Save PDF.
      ImGearPDFSaveOptions pdfOptions = new ImGearPDFSaveOptions();
      ImGearFileFormats.SaveDocument(igDocument, outStream, startPageNumber, ImGearSavingModes.OVERWRITE, ImGearSavingFormats.PDF, pdfOptions);
    }
    VB.NET Example
    Copy Code
    Using inStream As New FileStream(fileIn, FileMode.Open, FileAccess.Read)
    Using outStream As New FileStream(fileOut, FileMode.Create, FileAccess.Write)
     Dim startPageNumber As Integer = 0
    
     ' Load Office document.
     Dim igDocument As ImGearDocument = ImGearFileFormats.LoadDocument(inStream)
    
     ' Save PDF.
     Dim pdfOptions As New ImGearPDFSaveOptions()
     ImGearFileFormats.SaveDocument(igDocument, outStream, startPageNumber, ImGearSavingModes.OVERWRITE, ImGearSavingFormats.PDF, pdfOptions)
    End Using
    End Using
  1. Once you are done with the PDF component, you should dispose it.
    C# Example
    Copy Code
    ImGearPDF.Terminate();
    VB.NET Example
    Copy Code
    ImGearPDF.Terminate()
  2. Finally, by putting all the above code snippets together your console application should look like the code below:
    Console Application: Open .docx, save to PDF.
    Copy Code
    using ImageGear.Core;
    using ImageGear.Evaluation;
    using ImageGear.Formats;
    using ImageGear.Formats.Office;
    using ImageGear.Formats.PDF;
    using System.IO;
    
    namespace my_first_Office_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 Office file types.
                ImGearFileFormats.Filters.Add(ImGearOffice.CreateWordFormat());
                ImGearFileFormats.Filters.Add(ImGearOffice.CreatePowerPointFormat());
                ImGearFileFormats.Filters.Add(ImGearOffice.CreateExcelFormat());
    
                // Add support for PDF files.
                ImGearFileFormats.Filters.Add(ImGearPDF.CreatePDFFormat());
                ImGearPDF.Initialize();
                using (FileStream inStream = new FileStream(fileIn, FileMode.Open, FileAccess.Read))
                using (FileStream outStream = new FileStream(fileOut, FileMode.Create, FileAccess.Write))
                {
                    int startPageNumber = 0;
    
                    // Load Office document.
                    ImGearDocument igDocument = ImGearFileFormats.LoadDocument(inStream);
    
                    // Save PDF.
                    ImGearPDFSaveOptions pdfOptions = new ImGearPDFSaveOptions();
                    ImGearFileFormats.SaveDocument(igDocument, outStream, startPageNumber, ImGearSavingModes.OVERWRITE, ImGearSavingFormats.PDF, pdfOptions);
                }
    
                // Dispose of the PDF component.
                ImGearPDF.Terminate();
            }
            static void Main(string[] args)
            {
                Program myProgram = new Program();
                myProgram.SaveAs(@"C:\PATHTOWORDDOC\FILENAME.docx", @"C:\PATHTOPDF\NEWNAME.pdf");
            }
        }
    }
    Console Application: Open .docx, save to PDF.
    Copy Code
    Imports ImageGear.Core
    Imports ImageGear.Evaluation
    Imports ImageGear.Formats
    Imports ImageGear.Formats.Office
    Imports ImageGear.Formats.PDF
    Imports System.IO
    Namespace my_first_Office_project
     Class Program
      Public Sub SaveAs(fileIn As String, fileOut As String)
    
       ' Initialize evaluation license.
       ImGearEvaluationManager.Initialize()
       ImGearEvaluationManager.Mode = ImGearEvaluationMode.Watermark
    
       ' Initialize common formats.
       ImGearCommonFormats.Initialize()
       ImGearFileFormats.Filters.Add(ImGearOffice.CreateWordFormat())
       ImGearFileFormats.Filters.Add(ImGearOffice.CreatePowerPointFormat())
       ImGearFileFormats.Filters.Add(ImGearOffice.CreateExcelFormat())
       ImGearFileFormats.Filters.Add(ImGearPDF.CreatePDFFormat())
       Using inStream As New FileStream(fileIn, FileMode.Open)
        Using outStream As New FileStream(fileOut, FileMode.Create)
         Dim startPageNumber As Integer = 0
    
         ' Load Office document.
         Dim igDocument As ImGearDocument = ImGearFileFormats.LoadDocument(inStream)
    
         ' Save PDF.
         Dim pdfOptions As New ImGearPDFSaveOptions()
         ImGearFileFormats.SaveDocument(igDocument, outStream, startPageNumber, ImGearSavingModes.OVERWRITE, ImGearSavingFormats.PDF, pdfOptions)
        End Using
       End Using
    
       ' Dispose of the PDF component.
       ImGearPDF.Terminate()
      End Sub
      Private Shared Sub Main(args As String())
       Dim myProgram As New Program()
       myProgram.SaveAs("C:\PATHTOWORDDOC\FILENAME.docx", "C:\PATHTOPDF\NEWNAME.pdf")
      End Sub
     End Class
    End Namespace