Image Resuse
DynamicPDF Core Suite for .NET allows for highly efficient image reuse within or across several documents. The ImageData class is included for this purpose. This class holds the pixel data for an image and is used in the Image and BackgroundImage page elements constructors. When the same ImageData object is used multiple times in a PDF document, its data is only included once in the resulting PDF document.
If the same Image or BackgroundImage page element is used multiple times in the same document, image reuse is handled automatically because the same ImageData object is used each time it is displayed. This also applies to images used in a single section or document template.
Using The Same Image Multiple Times In A Document
When using an image multiple times in a document, ensure that the image data is included only a few times in the PDF output. This is accomplished by creating an ImageData object and using that in the Image or BackgroundImage constructors. The ImageData classes static GetImage method can create an image data object. Using GetImage, the same image can appear at different locations and scales in the document without increasing the overall size of the document.
// Create an ImageData object from the image.
ImageData imageData = ImageData.GetImage( @"C:\MyLogo.gif" );
// Add the image to the fist page twice.
page1.Elements.Add( new BackgroundImage( imageData ) );
page1.Elements.Add( new Image( imageData, 0, 0, 0.24f ) );
// Add the image to the second page at a differant location and scale.
page2.Elements.Add( new Image( imageData, 100, 100, 0.48f ) );
' Create an ImageData object from the image.
Dim MyImageData As ImageData = ImageData.GetImage( "C:\MyLogo.gif" )
' Add the image to the fist page twice.
MyPage1.Elements.Add( New BackgroundImage( imageData ) )
MyPage1.Elements.Add( New Image( imageData, 0, 0, 0.24f ) )
' Add the image to the second page at a differant location and scale.
MyPage2.Elements.Add( New Image( imageData, 100, 100, 0.48f ) )
Using The Same Image Across Documents
When an ImageData object is created, resources are required to parse information from the image. Suppose the image format does not allow for our highly efficient pass-through process. In that case, additional resources are needed to reformat the contents of the image so that it can be included in the PDF document. For images that appear in multiple documents (such as logos) it is best to create an ImageData object and set it to a static member variable of your class. You can then use that static member variable in your Image or BackgroundImage constructors. If the image is used in a single document or section template, the template object can be set to a static member field instead.
For web applications that require the MapPath method, you will not be able to use the static constructor to initialize your ImageData static (Shared in VB) member variables. This is required because the MapPath method can not be used from a static constructor of a Page object. In this case, you can test if the static member variable is null in your Page_Load event and initialize it.