Once the base of the tutorial application is completed, you can extend it with PDF support using the following steps:
- First, you need to add a reference to the ImageGear for .NET PDF Assembly.
- In the Solution Explorer, right-click on References, and click Add Reference.
- Choose the Browse tab and go 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.
- Next, add the PDF 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# |
Copy Code |
using ImageGear.Formats.PDF; |
- Add the following statements right before a call to InitializeComponent()in Form1 constructor:
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.
- Now we should add a call to terminate the PDF engine. Note that the main Form destructor, even if added, won't be called, because it is suppressed by Form1 Dispose default implementation, and the Dispose method cannot be modified, because it resides in Form1.Designer.cs, which is automatically regenerated. The best place to terminate the PDF engine would be the FormClosed() event handler. Add it using the Events tab in the Form1 property window and paste the following code:
C# |
Copy Code |
ImGearPDF.Terminate(); |
This will terminate the PDF engine. Every ImGearPDF.Initialize() must have a corresponding ImGearPDF.Terminate() call.
- 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.Initialize methods).
- Now, add the following code to the main form class to dispose the PDF page object:
C# |
Copy Code |
void DisposePDFObjects()
{
if (imGearPage != null && imGearPage is ImGearPDFPage)
{
imGearPageView1.Display = null;
((ImGearPDFPage)imGearPage).Dispose();
}
} |
- Add a call to DisposePDFObjects()to Form1_FormClosed event handler before terminating the PDF engine.
- Remove all "!imGearPageDisplay.Page.DIB.IsEmpty()" and "!imGearPage.DIB.IsEmpty()" checks from all menu handlers, because PDF Page always has an empty DIB field. These checks will occur six times; disable all of them.
- In loadPageToolStripMenuItem_Click, add a call to DisposePDFObjects()right before the ImGearFileFormats.LoadPage call. It is required to clean up the unmanaged PDF page resources.
- 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:
- In Form1 class, add the following field:
C# |
Copy Code |
private ImGearDocument imGearDocument = null; |
- In loadPageToolStripMenuItem_Click find the following code:
C# |
Copy Code |
// Load the image into the page
imGearPage = ImGearFileFormats.LoadPage(stream, 0); |
and replace it with:
C# |
Copy Code |
imGearDocument = ImGearFileFormats.LoadDocument(stream, 0, 1);
imGearPage = imGearDocument.Pages[0]; |
It does the same thing, but using the ImGearDocument API.
- Note that ImGearPDFDocument must be disposed, just like ImGearPDFPage. To do it, replace the implementation of DisposePDFObjects() method with the following:
C# |
Copy Code |
void DisposePDFObjects()
{
if (imGearDocument != null && imGearDocument is ImGearPDFDocument)
{
imGearPageView1.Display = null;
((ImGearPDFDocument) imGearDocument).Dispose();
}
} |
- Now you are ready to compile and run the finished application working with PDF documents.