Adobe PDF Library: Rendering and Transparency

Today we have a quick discussion on rendering PDF pages with the Adobe PDF Library C/C++ interface and working with transparency. Read on if you’re familiar with the Adobe PDF Library and are interested in how to draw pages over existing graphics or want to maintain alpha channel information when rendering PDF pages.
The Adobe PDF Library from Datalogics provides support both for rendering PDF pages over top of existing graphic overlays, and supplies alpha channel information for rendered PDF files. Rendering PDF pages is typically done into a byte array supplied to the page rendering APIs (PDPageDrawContentsToMemory or PDPageDrawContentsToMemoryWithParams). Most often, callers are not concerned with preserving transparency information and wish for an opaque white page background. This is easily accomplished by adding the kPDPageDoLazyErase option to the drawing flags (drawFlags) supplied to the rendering call. This option causes the PDF Library to draw a white rectangle in the size of the PDF page’s media box (or the size of the area to draw, whichever is smaller) into the raster byte array as its first element, before drawing any PDF markings. This results in a PDF page rendered over an opaque white background and erases any data that was existing in the raster byte array. The resulting display is what Adobe Reader or Acrobat displays by default when viewing PDF files when the transparency grid is off.
However, in some cases you may want to render a PDF page on top of a pre-existing graphic – for example, to simulate the transparency grid view in Adobe Reader/Acrobat. Because the PDF Library does not disturb or change pixels in the imaging byte array, you can pre-initialize the byte array with existing graphical content in the colorspace and format that you are rendering the PDF page to (24-bit packed RGB for a PDF rendered to DeviceRGB, as an example) and use the page rendering APIs to render into the raster byte array. Here, you would make sure not to specify the kPDPageDoLazyErase flag; because you want to maintain the pixels in the raster byte buffer that are not marked on the PDF page, the page should not be overlaid with an opaque background before the PDF is rendered. As long as the kPDPageDoLazyErase flag is not specified, only the pixels that represent areas marked in the PDF page will be changed.
The above maintains unmarked areas on the PDF page but does not show the full support for alpha channel (opacity) information in the Datalogics release of the Adobe PDF Library [note that the following applies only to the Datalogics release of the Adobe PDF Library]. Datalogics enhances the PDF Library to support rendering to RGBA (RGB + alpha) pixel format with full 8-bit alpha channel support by adding a virtual colorspace, “DeviceRGBA”, to the set of colorspaces allowed as rendering targets. The above discussion of the kPDPageDoLazyErase draw flag remains valid: specifying this drawing option tells the PDF Library to overlay the contents of the raster byte buffer with an opaque white rectangle, in the size of the PDF page’s media box or the size of the area to draw, before drawing any of the PDF page. This results in a PDF page with an alpha channel that is entirely opaque – because the background white fill is opaque.
When the kPDPageDoLazyErase flag is not set, and the DeviceRGBA colorspace is specified, the PDF Library will additively composite opacity channel information with the information existing in the alpha channel of the supplied raster byte array. In the case where you simply are interested in the PDF page’s content and do not have a background image to use as an underlay, you therefore initialize the alpha channel bytes to 0x00 (fully transparent). The PDF Library will not change the value of the alpha channel bytes for pixels that are unmarked by the PDF page, and these remain fully transparent in the resulting raster. The RGB bytes can be initialized to any value since they are fully transparent; we recommend white (0xFF 0xFF 0xFF) but any value can be used. For cases where you’d like to draw over a background, the PDF Library will composite marked pixels in an additive manner towards opaque white for areas in the PDF page that are not fully opaque. Of course, areas in the PDF page that are fully opaque – and those marked in a colorspace not containing transparency information – will be marked in the raster byte array as fully opaque and will overwrite any RGBA value previously existing in the raster byte array.
We hope you’ve found this brief discussion helpful. Happy imaging!

Share this post with your friends

Share on facebook
Share on twitter
Share on linkedin

Leave a Comment

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