ImageGear for .NET
Adding Office Support

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

  1. First, you need to add a reference to the ImageGear for .NET Office Assembly.
    1. In the Solution Explorer, right-click on References, and choose Add Reference.
    2. Choose the Browse tab, navigate to "ImageGear for .NET v21\Bin" directory, select ImageGear21.Formats.Office.dll, and click OK.

    You should now have the ImageGear21.Formats.Office assembly listed under References in the Solution Explorer.

  2. Next, add the Office using statement. Open the code for Form1.cs by right-clicking on the form and clicking View Code. At the top of the code add the following statement:
    C# Example
    Copy Code
    using ImageGear.Formats.Office;
  3. Add the following statements to Form1 constructor after the ImGearCommonFormats.Initialize() call:
    C# Example
    Copy Code
    // Add support for Word files
    ImGearFileFormats.Filters.Add(ImGearOffice.CreateWordFormat());
  4. Under the File menu, add the Load Document menu item. Double-click it to create a handler.
  5. Copy the code from the Load Page menu handler for the Load Document menu handler.
  6. Add the following fields to Form1 class:
    C# Example
    Copy Code
    private ImGearDocument imGearDocument;
    private int currPageNumber = -1;
  7. Add the following method to Form1 class:
    C# Example
    Copy Code
    private void UpdateMainView()
    {
    imGearPageView1.Page = imGearPage;
    imGearPageView1.Update();
    }
  8. In loadDocumentToolStripMenuItem_Click find the following code:
    C# Example
    Copy Code
    imGearPage = ImGearFileFormats.LoadPage(stream, 0);

    and replace it with:

    C# Example
    Copy Code
    imGearDocument = ImGearFileFormats.LoadDocument(stream, 0, -1);
    currPageNumber = 0;
    imGearPage = imGearDocument.Pages[currPageNumber];

    Also, replace the following code:

    C# Example
    Copy Code
    if (null != imGearPage && null != imGearPage.DIB &&
    !imGearPage.DIB.IsEmpty())
    {
    // create a new page display
    imGearPageDisplay = new ImGearPageDisplay(imGearPage);
    // associate the page display with the page view
    imGearPageView1.Display = imGearPageDisplay;
    // cause the page view to repaint
    imGearPageView1.Invalidate();
    }

    with:

    C# Example
    Copy Code
    if (imGearPage != null)
    {
    UpdateMainView();
    }
  9. To enable navigation between Office document pages do the following:
    1. Under the View menu, add ‘First Page’, ‘Previous Page’, ‘Next Page’, ‘Last Page’ menu items. Double-click them to create handlers.
    2. In firstPageToolStripMenuItem_Click add the following code:
      C# Example
      Copy Code
      imGearPage = imGearDocument.Pages[currPageNumber = 0];
      UpdateMainView();
    3. In previousPageToolStripMenuItem_Click add the following code:
      C# Example
      Copy Code
      if (currPageNumber - 1 >= 0)
      {
      imGearPage = imGearDocument.Pages[--currPageNumber];
      UpdateMainView();
      }
    4. In nextPageToolStripMenuItem_Click add the following code:
      C# Example
      Copy Code
      if (currPageNumber + 1 < imGearDocument.Pages.Count)
      {
      imGearPage = imGearDocument.Pages[++currPageNumber];
      UpdateMainView();
      }
    5. In lastPageToolStripMenuItem_Click add the following code:
      C# Example
      Copy Code
      currPageNumber = imGearDocument.Pages.Count - 1;
      imGearPage = imGearDocument.Pages[currPageNumber];
      UpdateMainView();

Now you are ready to compile and run the finished application working with Office documents.

 

 


©2014. Accusoft Corporation. All Rights Reserved.

Send Feedback