Images in PDF

From
Revision as of 14:54, 22 February 2018 by Adminko (talk | contribs) (Created page with "==Image XObject== Images in PDF are represented by the special type of XObject called ''image XObject''. Like the form XObject this type of XObjects can be us...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Image XObject

Images in PDF are represented by the special type of XObject called image XObject. Like the form XObject this type of XObjects can be used to place its content repeatedly when it’s needed, using the single registered instance contained in document's or page's resources.

Image's internal coordinate system in PDF differs from the one defined for drawing paths and other primitives and described in article Graphics System in PDF. It has its Y-axis inverted (and pointing to the bottom), similar to the coordinate system in Windows GDI. It’s important to take this into account for proper positioning of the image using regular drawing commands. We also designed several helper methods taking into account this difference and providing a more convenient way to draw an image.

Standard images in common formats

Apitron PDF Kit supports the following source image formats for creation of image XObjects: BMP, PNG, JPEG, TIFF, JP2. It has a special Image resource class defined in Apitron.PDF.Kit.FixedLayout.Resources.XObjects namespace that can be used to create such XObjects. This class has common properties such as Width, Height, Interpolate etc. accessible for developers. Images and their attributes are discussed in depth in section 8.9 “Images” of the PDF specification.

The code below demonstrates how to use image XObject for drawing images:

// create output PDF file
using (FileStream outputStream = new FileStream("xobjects.pdf", FileMode.Create, FileAccess.Write))
{
    // create new PDF document
    using(FixedDocument document = new FixedDocument())
    {
        document.Pages.Add(new Page());
        // create XObject
        Image xObject = new Image("myImageXObject","apitron.png");            

        // register XObject
        document.ResourceManager.RegisterResource(xObject);

        // append two identical images using image XObject
        document.Pages[0].Content.AppendImage("myImageXObject", 10,760, xObject.Width,xObject.Height);
        document.Pages[0].Content.AppendImage("myImageXObject", 100, 760, xObject.Width, xObject.Height);

        // save document
        document.Save(outputStream);
    }
}
 

The result is shown below; please note that we used a special method AppendImage() which performs all transformations needed to draw an image. Usually, drawing an image requires to set current scale transform because image is considered to be of size 1x1 by default. The invoked member AppendImage() is simply a wrapper that does necessary scaling for us and invokes AppendXObject().

Drawing an Image XObject