Top Description Fields Constructors Methods
org.apache.pdfbox.multipdf

public Class LayerUtility

extends Object
Class Inheritance
Imports
java.awt.geom.AffineTransform, java.io.IOException, .OutputStream, java.nio.charset.StandardCharsets, java.util.Arrays, .HashSet, .Map, .Set, org.apache.commons.logging.Log, .LogFactory, org.apache.fontbox.util.BoundingBox, org.apache.pdfbox.cos.COSArray, .COSBase, .COSDictionary, .COSName, .COSStream, org.apache.pdfbox.pdmodel.PDDocument, .PDDocumentCatalog, .PDPage, .PDResources, .PDPageContentStream, .PDPageContentStream.AppendMode, org.apache.pdfbox.pdmodel.common.PDRectangle, .PDStream, org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject, org.apache.pdfbox.pdmodel.graphics.optionalcontent.PDOptionalContentGroup, .PDOptionalContentProperties, org.apache.pdfbox.util.Matrix

This class allows to import pages as Form XObjects into a document and use them to create layers (optional content groups). It should used only on loaded documents, not on generated documents because these can contain unfinished parts, e.g. font subsetting information.

Field Summary

Modifier and TypeField and Description
private final PDFCloneUtility
private static final boolean
private static final Log
private static final Set<String>
private final PDDocument

Constructor Summary

AccessConstructor and Description
public
LayerUtility(PDDocument
the PDF document to modify
targetDoc
)

Creates a new instance.

Method Summary

Modifier and TypeMethod and Description
public PDOptionalContentGroup

Returns:

the optional content group that was generated for the form usage
appendFormAsLayer
(PDPage
the target page
targetPage
,
PDFormXObject
the form to place
form
,
AffineTransform
the transformation matrix that controls the placement of your form. You'll need this if your page has a crop box different than the media box, or if these have negative coordinates, or if you want to scale or adjust your form.
transform
,
String
the name for the layer/OCG to produce
layerName
)

Places the given form over the existing content of the indicated page (like an overlay).

public PDDocument

Returns:

the PDF document
getDocument
()

Returns the PDF document we work on.

private void
importOcProperties(PDDocument
The source PDF document that contains the /OCProperties to be copied.
srcDoc
)

Imports OCProperties from source document to target document so hidden layers can still be hidden after import.

public PDFormXObject

Returns:

a Form XObject containing the original page's content
importPageAsForm
(PDDocument
the source PDF document that contains the page to be copied
sourceDoc
,
int
the 0-based page number of the page to be copied
pageNumber
)

Imports a page from some PDF file as a Form XObject so it can be placed on another page in the target document.

public PDFormXObject

Returns:

a Form XObject containing the original page's content
importPageAsForm
(PDDocument
the source PDF document that contains the page to be copied
sourceDoc
,
PDPage
the page in the source PDF document to be copied
page
)

Imports a page from some PDF file as a Form XObject so it can be placed on another page in the target document.

private void
transferDict(COSDictionary orgDict, COSDictionary targetDict, Set<String> filter)

public void
wrapInSaveRestore(PDPage
the page
page
)

Some applications may not wrap their page content in a save/restore (q/Q) pair which can lead to problems with coordinate system transformations when content is appended.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

clonerback to summary
private final PDFCloneUtility cloner
DEBUGback to summary
private static final boolean DEBUG
LOGback to summary
private static final Log LOG
PAGE_TO_FORM_FILTERback to summary
private static final Set<String> PAGE_TO_FORM_FILTER
targetDocback to summary
private final PDDocument targetDoc

Constructor Detail

LayerUtilityback to summary
public LayerUtility(PDDocument targetDoc)

Creates a new instance.

Parameters
targetDoc:PDDocument

the PDF document to modify

Method Detail

appendFormAsLayerback to summary
public PDOptionalContentGroup appendFormAsLayer(PDPage targetPage, PDFormXObject form, AffineTransform transform, String layerName) throws IOException

Places the given form over the existing content of the indicated page (like an overlay). The form is enveloped in a marked content section to indicate that it's part of an optional content group (OCG), here used as a layer. This optional group is returned and can be enabled and disabled through methods on PDOptionalContentProperties.

You may want to call wrapInSaveRestore(PDPage) before calling this method to make sure that the graphics state is reset.

Parameters
targetPage:PDPage

the target page

form:PDFormXObject

the form to place

transform:AffineTransform

the transformation matrix that controls the placement of your form. You'll need this if your page has a crop box different than the media box, or if these have negative coordinates, or if you want to scale or adjust your form.

layerName:String

the name for the layer/OCG to produce

Returns:PDOptionalContentGroup

the optional content group that was generated for the form usage

Exceptions
IOException:
if an I/O error occurs
getDocumentback to summary
public PDDocument getDocument()

Returns the PDF document we work on.

Returns:PDDocument

the PDF document

importOcPropertiesback to summary
private void importOcProperties(PDDocument srcDoc) throws IOException

Imports OCProperties from source document to target document so hidden layers can still be hidden after import.

Parameters
srcDoc:PDDocument

The source PDF document that contains the /OCProperties to be copied.

Exceptions
IOException:
If an I/O error occurs.
importPageAsFormback to summary
public PDFormXObject importPageAsForm(PDDocument sourceDoc, int pageNumber) throws IOException

Imports a page from some PDF file as a Form XObject so it can be placed on another page in the target document.

You may want to call wrapInSaveRestore(PDPage) before invoking the Form XObject to make sure that the graphics state is reset.

Parameters
sourceDoc:PDDocument

the source PDF document that contains the page to be copied

pageNumber:int

the 0-based page number of the page to be copied

Returns:PDFormXObject

a Form XObject containing the original page's content

Exceptions
IOException:
if an I/O error occurs
importPageAsFormback to summary
public PDFormXObject importPageAsForm(PDDocument sourceDoc, PDPage page) throws IOException

Imports a page from some PDF file as a Form XObject so it can be placed on another page in the target document.

You may want to call wrapInSaveRestore(PDPage) before invoking the Form XObject to make sure that the graphics state is reset.

Parameters
sourceDoc:PDDocument

the source PDF document that contains the page to be copied

page:PDPage

the page in the source PDF document to be copied

Returns:PDFormXObject

a Form XObject containing the original page's content

Exceptions
IOException:
if an I/O error occurs
transferDictback to summary
private void transferDict(COSDictionary orgDict, COSDictionary targetDict, Set<String> filter) throws IOException
wrapInSaveRestoreback to summary
public void wrapInSaveRestore(PDPage page) throws IOException

Some applications may not wrap their page content in a save/restore (q/Q) pair which can lead to problems with coordinate system transformations when content is appended. This method lets you add a q/Q pair around the existing page's content.

Parameters
page:PDPage

the page

Exceptions
IOException:
if an I/O error occurs