Interesting PDF bugs – Simulating an SMask with Vector Graphics

Today’s PDF file was not so much of a bug as more a ‘how can we emulate this in Java issue’…

I was sent a PDF file which did not correctly display in JPedal. It had a big rectangular block of color on it. In Java you can have a clip on the page which prevents parts of the page being drawn onto (it is like a stencil covering part of the page so you cannot paint there). The clip is Java is fairly flexible – it can be any shape. However it cannot be an image…

The PDF file format has a concept of an SMask which is essentially a bit-mapped clip. So how can we emulate this feature in Java?

There are several possible solutions. The way we have used is to detect this scenario and instead of filling the rectangle we create an image based on the SMask. Because it is an image we can scan  all the pixels and see which are opaque (and make them transparent) and which should let the colour through (and set them to the correct colour). It needs more memory but it provides the flexibilty needed to emulate this feature in most PDF files. And it is the solution the javafx team have used to solve a very similar problem…

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>