ceTe Software Help Library for Java August - 2020
DynamicPDF Generator for Java / Programming with Generator for Java / Images / Image Reuse
In This Topic
    Image Reuse
    In This Topic

    DynamicPDF Generator for Java is designed to allow highly efficient reuse of images within a document or across several documents. The ImageData class is included for this purpose. This class holds the pixel data for an image and can be used in the constructors of the Image and BackgroundImage Page Elements. When the same ImageData object is used multiple times in a PDF document, its data will only be included once in the resulting PDF document.

    NOTE: 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 that are 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, care must be taken to ensure that the image data is not included multiple times in the PDF output. This is accomplished by creating an ImageData object and using that in the Image or BackgroundImage constructors. An image data object can be created using the ImageData classes static getImage method. Using this technique, the same image can appear at different locations and scale in the document without increasing the overall size of the document:

    [Java]
        // Create an ImageData object from the image.
        ImageData imageData = ImageData.getImage("[PhysicalPath]/MyLogo.gif");
        // Add the image to the fist page twice.
        page1.getElements().add(new BackgroundImage(imageData));
        page1.getElements().add(new Image(imageData, 0, 0, 0.24f));
        // Add the image to the second page at a differant location and scale.
        page2.getElements().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. If the Image format does not allow for our highly efficient pass through process, additional resources are required to reformat the contents of the image so that it can be included in the PDF document. For images that will 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 all of your Image or BackgroundImage constructors.

    If the image is being used in a single document or section template, the template object itself can be set to a static member field instead. 

    See Also