ImageGear for .NET
Adding PDF Support

Once the base of the tutorial application is completed, you can extend it with PDF support using the following steps:

  1. First, you need to add a reference to the ImageGear for .NET PDF Assembly.
    1. In the Solution Explorer, right-click on References and choose Add Reference.
    2. Choose the Browse tab.
    3. Navigate to "ImageGear for .NET v21\Bin" directory, select ImageGear21.Formats.Pdf.dll, and click OK.
      You should now have the ImageGear21.Formats.Pdf assembly listed under References in the Solution Explorer.


  2. Next, add the PDF using statement. Open the code for Window1.xaml.cs. At the top of the code, add the following statement:
    C#
    Copy Code
    using ImageGear.Formats.PDF;
  3. Add the following statements to Window1 constructor right after ImGearCommonFormats.Initialize() call:
    C#
    Copy Code
    ImGearFileFormats.Filters.Insert(0, ImGearPDF.CreatePDFFormat());
    ImGearFileFormats.Filters.Insert(0, ImGearPDF.CreatePSFormat());
    ImGearPDF.Initialize();

    This will add PDF and PS formats to the ImageGear formats list and initialize the PDF engine. ImGearPDF.Initialize() must be called once per process, unless you are using multi-threading.

  4. Now add a call to terminate the PDF engine. The best place to terminate the PDF engine is the Closed() event handler. Add it using the Events tab in Window property window (design view) and paste the following code:
    Example Title
    Copy Code
     ImGearPDF.Terminate();
    This will terminate the PDF engine. Every ImGearPDF.Initialize() must have a corresponding ImGearPDF.Terminate() call.
  5. The next step is to open project properties and change the output path to "...\ ImageGear for .NET v21\Bin". (As an alternative, you can define a directory containing ImageGear binaries and PDF resources explicitly, as a parameter for ImGearPDF.CreatePDFFormat, ImGearPDF.CreatePSFormat and ImGearPDF.Initializemethods).
  6. Now, add the following code to the Window1 class to dispose the PDF page object:
    C#
    Copy Code
    void DisposePDFObjects()
    {
     if (imGearPage != null && imGearPage is ImGearPDFPage)
     {
      imGearPageView1.Display = null;
      ((ImGearPDFPage)imGearPage).Dispose();
     }
    }
  7. Add a call to DisposePDFObjects()to Window1_Closed event handler before terminating the PDF engine.
  8. Remove all "!imGearPage.DIB.IsEmpty()" checks in the code, both in OpenExecuted and CanExecutePageAvailable handlers. 
  9. In OpenExecuted, add a call to DisposePDFObjects()right before the ImGearFileFormats.LoadPage call. It is required to clean up the unmanaged PDF page resources.
  10. And now you are ready to compile and run the finished application supporting PDF pages. If you want to deal with PDF documents, here is what needs to be added/changed:
    1. In Window1 class, add the following field:
      C#
      Copy Code
      private ImGearDocument igDocument = null;
    2. In OpenExecuted find the following code:
      C#
      Copy Code
      // Load the image into the page  
      igPage = ImGearFileFormats.LoadPage(fileContent, 0);
      and replace it with:
      C#
      Copy Code
      igDocument = ImGearFileFormats.LoadDocument(fileContent, 0, 1);
      igPage = igDocument.Pages[0];

      It does the same thing, but using the ImGearDocument API.

    3. Note that ImGearPDFDocument must be disposed just like the ImGearPDFPage. To do it, replace the implementation of DisposePDFObjects() method with the following:
      C#
      Copy Code
      void DisposePDFObjects()
      {
       if (igDocument != null && igDocument is ImGearPDFDocument)
       {
        imGearPageView1.Display = null;
        ((ImGearPDFDocument) igDocument).Dispose();
       }
      }
  11. Now you are ready to compile and run the finished application working with PDF documents. 

 

 


©2014. Accusoft Corporation. All Rights Reserved.

Send Feedback