Adobe PDF Library

Adding Text and Graphic Elements


View Sample Code

The AddElements sample program creates a new PDF file with two pages and several graphics and text elements.  The first page features a pentagram drawing; the second shows how to create colored text, text that is vertical or at an angle, and a shape with color fill. The third page features a rectangle and a curved design.  Use this sample for ideas on how to draw an image on a PDF page.


View Sample Code

Use this program to create a new PDF file and add glyphs to the page, managing them by individual Glyph ID codes.

PDF files are not based on a markup language like HTML.  Rather, PDF files are built on a series of program commands that finish tasks, like drawing lines and objects.  These commands also define the color, font, and size of text.

Any PDF file can display text in many different kinds of fonts.  In a PDF file, each font is a collection of symbols called “glyphs.”  Times Roman has its own a glyph for the letter “a,” as does Helvetia (“a”) and Courier New (“a”), and Tempus sans (“a”).  So the single letter, lower case a, could be expressed in a PDF file as any one of hundreds of different kinds of glyphs.  Each glyph has its own width, spacing, and characteristics, like underline or italic.  This means that any block of text in a PDF file is expressed as a string of glyphs.

In this sample program, we print the word “Hello” in a PDF file by defining the individual glyphs used to create each character.  For example, in the Arial font, the glyph ID for the lower case letter “l” is u002f.

You might find this sample program a useful reference if you are using a custom font for a software product and you want to know how to identify characters in a PDF file by using glyph IDs, rather than Unicode IDs.  Both Unicode and glyphs appear in the program.


View Sample Code

This sample program adds six lines of Unicode text to a PDF file, in six different languages.

PDF files make use of the Unicode standard in rendering characters.  The Unicode standard  was created in 1991 to apply a consistent method to code and represent electronic text. Unicode provides a unique number for every character, regardless of the language or computer system involved.  This allows different platforms to trade information easily, and without risk of corrupting data.

Unicode replaced hundreds of earlier competing and conflicting coding methods used for representing text in computer systems.  The current version of the standard, released by the Unicode Consortium in September of 2012, includes over 110,000 characters in 100 different scripts.  It also covers almost all of the writing systems now in use around the world.  Unicode has been adopted across the IT industry and is broadly used in operating systems, XML, and many programming languages and platforms, including Java.


View Sample Code

This program describes how to render text from top to bottom on a page. The program includes an enumerator called WritingMode that is set to “Vertical.” It also provides several rows of Unicode characters to serve as sample text.  The PDF output file presents multiple columns of vertical text.  The characters appear in English as well as Mandarin, Japanese, and Korean.


View Sample Code

You can add a bookmark to a PDF file that connects one part of the file to another.  For example, you could put bookmarks for each section heading, allowing a reader to click on a hyperlink and move directly to that section from cross reference elsewhere in the document.

This sample program shows how to automatically add bookmarks to a PDF file.  The program opens a source file called sample.PDF, adds bookmarks to it, and saves an output file called Bookmark-out.PDF that shows the new bookmark structure.

Open the Bookmark-out.PDF file and click Acrobat Bookmark iconB to display the bookmarks that the CreateBookmarks program added to sample. PDF. Several of these bookmarks will zoom to parts of the page. The last three, Child1, 2, and 3, are dummy bookmarks that do not respond when you click on them.  They demonstrate how to rearrange existing bookmarks.


View Sample Code

The Graphics State is an internal data structure in a PDF file that holds the parameters that describe graphics within that file.  These parameters define how individual graphics are presented on the page. Adobe Systems introduced the Extended Graphic State to expand the original Graphics State data structure, providing space to define and store more data objects within a PDF.

This sample program shows how to use the Extended Graphic State object to add graphics parameters to an image.

PDF files can have objects that are transparent, and thus can blend in various ways with objects behind them.  Transparent graphics or images can be stacked in a PDF file, with each one contributing to the final result that appears on the page.  Opaque objects are different.  If a PDF file has a stack of opaque graphics or images, only the graphic or image on top of the stack appears.

One or more graphics images presented together in a stack is referred to as a transparency group. With a stack of transparent images, the final colors shown are the result of blending the colors of all of the overlapping objects.  Each object will have a backdrop, which is usually the color and image that is produced by blending all of the previous objects in the stack.  But the way the colors blend depends on the blend mode assigned to each object.  The blend mode defines how a graphics image will interact with the other transparent images in the stack on the page.

The Extended Graphics States program breaks out a series of graphics images in a transparency group into its component parts.  Then, the program displays these graphics images in a PDF file called ExtendedGraphicStates_out.PDF.  Each image shown on this output PDF file is an example of one of the transparency modes that can be assigned to an object:

Blend modes that can be separated

Normal Selects the source color, or the color in the graphics object, and ignores the backdrop.
Multiply Multiplies the source color and the backdrop color.  This means that the color will always be at least as dark as the constituent colors.  If either the source object or the backdrop is black, the color will be black.
Screen Multiplies the complements of the source color and the backdrop, and then complements the result.  The result is always as light as either of the constituent colors.
Overlay Multiplies or screens the colors, depending on the backdrop color value.  Source colors overlay the backdrop while preserving highlights and shadows.  The backdrop color is mixed with the source color to reflect the lightness or darkness of the backdrop.
Darken Selects the darker of the backdrop or source colors.
Lighten Selects the lighter of the backdrop or source colors.
Color Dodge Brighten the backdrop color to reflect the source color.
Color Burn Darken the backdrop color to reflect the source color.
Hard Light Multiplies or screens the colors, depending on the source color value.
Soft Light Darkens or lightens the colors, depending on the source color value.
Difference Subtracts the darker of the two constituent colors from the lighter color.
Exclusion Similar to the Difference mode, but lower in contrast.

Blend modes that cannot be separated

Hue Creates a color with the hue of the source color and the saturation and luminosity of the background color.
Saturation Creates a color with the saturation of the source color and the hue and luminosity of the backdrop color.
Color Creates a color with the hue and saturation of the source color and the luminosity of the backdrop color.
Luminosity Creates a color with the luminosity of the source color and the hue and saturation of the backdrop color.


View Sample Code

The Watermark sample program shows how to create a watermark and copy it to a new PDF file.  You could use this code to create a message to apply to PDF files you select, like “Confidential” or “Draft Copy.”  Or you might want to place a copyright statement over a set of photographs shown in a PDF file so that they cannot be easily duplicated without the permission of the owner.

The program provides the default name of an input PDF document,and the name of a PDF to serve as the source of the graphic for the watermark. The watermark file should also be a PDF file. The program takes whatever text or image included in this second “watermark” PDF file and applies it to the middle the pages of your original PDF file.  Then, it will save the updated version to a new output file called Watermark-out.PDF. You can define your own PDF documents in the sample code, or enter the file names in a command prompt.

Any text or image you provide in your watermark PDF file will be applied over the top of the existing text or graphics in the original PDF file, but it will be transparent, and set at an angle, like this:

Watermark sample

Note that the program is also written so that the watermark you provide will only appear on alternate (even) pages.  The phrase “Multiline Watermark” will appear on odd pages, and centered.  Thus the program provides two methods for placing watermarks.  You can add a graphic or text to a separate PDF file and apply that to the final output PDF file, or you can hard code text in your Java program to place in the middle of a page.  You can also adjust the color and font size of this text.