ImageGear for .NET User Guide > Using ImageGear for .NET > Using ImageGear.Formats.PDF Namespace > PDF Object Lifetime |
All PDF objects, such as ImGearPDEColorspace, ImGearPDFPage, and ImGearPDEContent, are based on underlying low-level PDF objects, which are not controlled by .NET resource manager and garbage collector. Due to complex dependencies between these objects, it is not possible to wrap these objects with a fully-automated .NET lifecycle management. For example, disposing an ImGearPDFPage object whose content was obtained with the GetContent method and was not released with ReleaseContent can lead to an internal PDF exception.
So, each PDF object that is obtained or created from scratch should be disposed explicitly using its Dispose() method (as an alternative, “using” syntax can be applied).
Failure to dispose of PDF Documents and Pages can cause significant memory issues in the running application and lingering issues, in the form of abandoned temp files, after the application has terminated. |
Also, an ImGearPDEContent object obtained from ImGearPDFPage should be released using the ImGearPDFPage.ReleaseContent() method in all cases.
An ImGearPDEContent object obtained from a Container, Group, or Form does not need to be released, so ImGearPDEContainer, ImGearPDEForm, and ImGearPDEGroup do not have such a method. |
Here is sample code to illustrate this:
C# |
Copy Code |
---|---|
public void IterateElements(ImGearDocument imageDocument) { ImGearPDFDocument pdfDocument = (ImGearPDFDocument)imageDocument; for (int index = 0; index < pdfDocument.Pages.Count; index++) { try { ImGearPage page = pdfDocument.Pages[index]; // content should be disposed ImGearPDEContent content = ((ImGearPDFPage)page).GetContent(); for (int elementIndex = 0; elementIndex < content.ElementCount; elementIndex++) { ImGearPDEElement element = content.GetElement(elementIndex); if (!TryLoadElement(element)) { throw new Exception(); } element.Dispose(); } // ReleaseContent should be called for a page ((ImGearPDFPage)page).ReleaseContent(); // dispose content content.Dispose(); } catch (Exception ex) { MessageBox.Show("Error”); } } // ImGearPDFDocument object should be disposed too. We’ve left it to caller. } |