Package org.apache.pdfbox.multipdf
Class LayerUtility
java.lang.Object
org.apache.pdfbox.multipdf.LayerUtility
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
FieldsModifier and TypeFieldDescriptionprivate final PDFCloneUtility
private static final boolean
private static final org.apache.commons.logging.Log
private final PDDocument
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionappendFormAsLayer
(PDPage targetPage, PDFormXObject form, AffineTransform transform, String layerName) Places the given form over the existing content of the indicated page (like an overlay).Returns the PDF document we work on.private void
importOcProperties
(PDDocument srcDoc) Imports OCProperties from source document to target document so hidden layers can still be hidden after import.importPageAsForm
(PDDocument sourceDoc, int pageNumber) Imports a page from some PDF file as a Form XObject so it can be placed on another page in the target document.importPageAsForm
(PDDocument sourceDoc, PDPage 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) void
wrapInSaveRestore
(PDPage 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.
-
Field Details
-
LOG
private static final org.apache.commons.logging.Log LOG -
DEBUG
private static final boolean DEBUG- See Also:
-
targetDoc
-
cloner
-
PAGE_TO_FORM_FILTER
-
-
Constructor Details
-
LayerUtility
Creates a new instance.- Parameters:
document
- the PDF document to modify
-
-
Method Details
-
getDocument
Returns the PDF document we work on.- Returns:
- the PDF document
-
wrapInSaveRestore
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
- the page- Throws:
IOException
- if an I/O error occurs
-
importPageAsForm
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
- the source PDF document that contains the page to be copiedpageNumber
- the page number of the page to be copied- Returns:
- a Form XObject containing the original page's content
- Throws:
IOException
- if an I/O error occurs
-
importPageAsForm
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
- the source PDF document that contains the page to be copiedpage
- the page in the source PDF document to be copied- Returns:
- a Form XObject containing the original page's content
- Throws:
IOException
- if an I/O error occurs
-
appendFormAsLayer
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 onPDOptionalContentProperties
.You may want to call
wrapInSaveRestore(PDPage)
before calling this method to make sure that the graphics state is reset.- Parameters:
targetPage
- the target pageform
- the form to placetransform
- 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
- the name for the layer/OCG to produce- Returns:
- the optional content group that was generated for the form usage
- Throws:
IOException
- if an I/O error occurs
-
transferDict
private void transferDict(COSDictionary orgDict, COSDictionary targetDict, Set<String> filter) throws IOException - Throws:
IOException
-
importOcProperties
Imports OCProperties from source document to target document so hidden layers can still be hidden after import.- Parameters:
sourceDoc
- The source PDF document that contains the /OCProperties to be copied.- Throws:
IOException
- If an I/O error occurs.
-