Making sure Image names are unique in PDF files

All images have a name in the PDF image XObject, usually along the lines of IM0, IM1, IM2, etc. This is unique for each page, so there will be only one Im0 on each page.

If the PDF page contains XForm objects, they can also contain images. These need to be unique for the XForm, but can be the same as a value used on a page. Similarly, the XForm can contain XForms with their own images, and so forth. So you could have this situation.

Main page with images Im0, Im1, Im2, Im3 and XForm XF1. This could have images Imo, Im1, Im2 another XForm with yet more images.

I was recently caught out by this because I was using the image name (Im0) as a key (I should have been using something like Im0, XF1.Im0 and XF1.XF1.Im0 to provide unique keys.

The name is used to reference to Images in the Resources object. In this case, each XForm has its own unique resources object (or uses the global resources), so Im0 works correctly in this context.

So you need to be careful with names in PDF files. You have a similar issue with Forms name, where sometimes you use just the Form name and any inherited values from its parent. But that is for another article…

This post is part of our “Understanding the PDF File Format” series. In each article, we discuss a PDF feature, bug, gotcha or tip. If you wish to learn more about PDF, we have 13 years worth of PDF knowledge and tips, so click here to visit our series index!

Related Posts:

The following two tabs change content below.

Mark Stephens

System Architect and Lead Developer at IDRSolutions
Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.
Markee174

About Mark Stephens

Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX.

He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>