Display alternate images for view and print in a PDF document

A question on StackOverflow.com asked if it is possible to have 2 alternate versions of an image in a PDF file, one for display and other for print.
Several solutions were given: optional contentform fields or alternate images. In the article below we’ll show how these solutions can be implemented using XFINIUM.PDF library and how they compare to each other.

1. Optional content

First proposed solution was to use optional content groups. This solution requires to create 2 optional content groups. One group is marked for view only while the other group is marked for print only. Each image is drawn inside the corresponding group.

 2. Form fields

The second solution suggested to use form fields with custom appearances. This solution requires to create a form field, we use textboxes, for each image, put the form fields at the position on the page where the image should appear and then draw the images on fields’ appearances. One field is marked as visible but not printable while the other field is marked as hidden but printable.

3. Alternate images

The third solution recommends a PDF feature that solves exactly the requirement described in the question: pdf alternate images. PDF alternate images allow to attach an image to another image and mark the attached image to be used as default for printing. The code below creates an alternate image from the print only image and attaches it to the view only image. In this scenario the only the view image is drawn on the page and when the document is printed the viewer application replaces the image with the print only image.

 Conclusions

At the end you might ask what is the recommended solution? The answer is: it depends. PDF files created with solutions 1 and 3 are printed correctly only by Adobe Reader and a few other applications while the PDF files created with solution 2 are printed correctly by a wider range of applications (this is merely an observation, not a documented scientific study). However, the problem with solution 2 is that the form fields are always on top of page content so you cannot have the images drawn behind text or some other vector graphics and if page content is extracted for creating n-up PDF pages the images in the form fields will not be included.
So choose the solution based on your actual requirements and the possible consumers of the PDF files.

Leave a Reply