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

public Class Overlay

extends Object
implements Closeable
Class Inheritance
All Implemented Interfaces
java.io.Closeable, java.lang.AutoCloseable
Imports
java.awt.geom.AffineTransform, java.io.Closeable, .File, .IOException, .InputStream, .OutputStream, java.math.BigDecimal, java.nio.charset.StandardCharsets, java.util.ArrayList, .Collections, .HashMap, .HashSet, .List, .Map, .Set, org.apache.pdfbox.Loader, org.apache.pdfbox.cos.COSArray, .COSBase, .COSDictionary, .COSName, .COSObject, .COSStream, org.apache.pdfbox.io.IOUtils, org.apache.pdfbox.pdmodel.PDDocument, .PDPage, .PDPageTree, .PDResources, org.apache.pdfbox.pdmodel.common.PDRectangle, org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject

Adds an overlay to an existing PDF document. Based on code contributed by Balazs Jerk.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
Overlay.LayoutPage

Stores the overlay page information.

public static enum
Overlay.Position

Possible location of the overlaid pages: foreground or background.

Field Summary

Modifier and TypeField and Description
private PDDocument
private String
private PDDocument
private String
private Overlay.LayoutPage
private PDDocument
private String
private Overlay.LayoutPage
private PDDocument
private String
private Overlay.LayoutPage
private String
private PDDocument
private PDDocument
private String
private Overlay.LayoutPage
private int
private PDDocument
private String
private Overlay.LayoutPage
private final Set<PDDocument>
private Overlay.Position
private Map<Integer, Overlay.LayoutPage>
private boolean

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private void
addOriginalContent(COSBase contents, COSArray contentArray)

protected AffineTransform

Returns:

The affine transform to be used.
calculateAffineTransform
(PDPage
The page that will get the overlay.
page
,
PDRectangle
The overlay media box.
overlayMediaBox
)

Calculate the transform to be used when positioning the overlay.

public void
close()

Implements java.io.Closeable.close.

Close all input documents which were used for the overlay and opened by this class.

private COSStream
private List<COSStream>
private Overlay.LayoutPage
createLayoutPage(PDPage page)

Create a LayoutPage object from given PDPage object.

private COSStream
createOverlayStream(PDPage page, Overlay.LayoutPage layoutPage, COSName xObjectId)

private COSName
private COSStream
private String
float2String(float floatValue)

public String

Returns:

the default overlay file
getDefaultOverlayFile
()

Returns the default overlay file.

public String

Returns:

the input file
getInputFile
()

Returns the input file.

private Overlay.LayoutPage
getLayoutPage(PDDocument doc)

Create a LayoutPage object from the first page of the given document.

private Overlay.LayoutPage
getLayoutPage(int pageNumber, int numberOfPages)

private Map<Integer, Overlay.LayoutPage>
private PDDocument
loadPDF(String pdfName)

private void
public PDDocument

Returns:

The modified input PDF document, which has to be saved and closed by the caller. If the input document was passed by setInputPDF(PDDocument) then it is that object that is returned.
overlay
(Map<Integer, String>
Optional map of overlay files of which the first page will be used for specific pages of the input document. The page numbers are 1-based. The map must be empty (but not null) if no specific mappings are used.
specificPageOverlayMap
)

This will add overlays to a document.

public PDDocument

Returns:

The modified input PDF document, which has to be saved and closed by the caller. If the input document was passed by setInputPDF(PDDocument) then it is that object that is returned.
overlayDocuments
(Map<Integer, PDDocument>
Optional map of overlay documents for specific pages. The page numbers are 1-based. The map must be empty (but not null) if no specific mappings are used.
specificPageOverlayDocumentMap
)

This will add overlays documents to a document.

private void
overlayPage(PDPage page, Overlay.LayoutPage layoutPage, COSArray array, PDFCloneUtility cloner)

private void
public void
setAllPagesOverlayFile(String
the all pages overlay file
allPagesOverlayFile
)

Sets the all pages overlay file.

public void
setAllPagesOverlayPDF(PDDocument
the all pages overlay PDF. This should not be a PDDocument that you created on the fly, it should be saved first, if it contains any fonts that are subset.
allPagesOverlayPDF
)

Sets the all pages overlay PDF.

public void
setDefaultOverlayFile(String
the default overlay file
defaultOverlayFile
)

Sets the default overlay file.

public void
setDefaultOverlayPDF(PDDocument
the default overlay PDF
defaultOverlayPDF
)

Sets the default overlay PDF.

public void
setEvenPageOverlayFile(String
the even page overlay file
evenPageOverlayFile
)

Sets the even page overlay file.

public void
setEvenPageOverlayPDF(PDDocument
the even page overlay PDF
evenPageOverlayPDF
)

Sets the even page overlay PDF.

public void
setFirstPageOverlayFile(String
the first page overlay file
firstPageOverlayFile
)

Sets the first page overlay file.

public void
setFirstPageOverlayPDF(PDDocument
the first page overlay PDF
firstPageOverlayPDF
)

Sets the first page overlay PDF.

public void
setInputFile(String
the file to be overlaid. The PDDocument object gathered from opening this file will be returned by overlay(Map<Integer, String>).
inputFile
)

Sets the file to be overlaid.

public void
setInputPDF(PDDocument
the PDF to be overlaid. This will be the object that is returned by overlay(Map<Integer, String>).
inputPDF
)

Sets the PDF to be overlaid.

public void
setLastPageOverlayFile(String
the last page overlay file
lastPageOverlayFile
)

Sets the last page overlay file.

public void
setLastPageOverlayPDF(PDDocument
the last page overlay PDF
lastPageOverlayPDF
)

Sets the last page overlay PDF.

public void
setOddPageOverlayFile(String
the odd page overlay file
oddPageOverlayFile
)

Sets the odd page overlay file.

public void
setOddPageOverlayPDF(PDDocument
the odd page overlay PDF
oddPageOverlayPDF
)

Sets the odd page overlay PDF.

public void
setOverlayPosition(Overlay.Position
the overlay position
overlayPosition
)

Sets the overlay position.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

allPagesOverlayback to summary
private PDDocument allPagesOverlay
allPagesOverlayFilenameback to summary
private String allPagesOverlayFilename
defaultOverlayback to summary
private PDDocument defaultOverlay
defaultOverlayFilenameback to summary
private String defaultOverlayFilename
defaultOverlayPageback to summary
private Overlay.LayoutPage defaultOverlayPage
evenPageOverlayback to summary
private PDDocument evenPageOverlay
evenPageOverlayFilenameback to summary
private String evenPageOverlayFilename
evenPageOverlayPageback to summary
private Overlay.LayoutPage evenPageOverlayPage
firstPageOverlayback to summary
private PDDocument firstPageOverlay
firstPageOverlayFilenameback to summary
private String firstPageOverlayFilename
firstPageOverlayPageback to summary
private Overlay.LayoutPage firstPageOverlayPage
inputFileNameback to summary
private String inputFileName
inputPDFDocumentback to summary
private PDDocument inputPDFDocument
lastPageOverlayback to summary
private PDDocument lastPageOverlay
lastPageOverlayFilenameback to summary
private String lastPageOverlayFilename
lastPageOverlayPageback to summary
private Overlay.LayoutPage lastPageOverlayPage
numberOfOverlayPagesback to summary
private int numberOfOverlayPages
oddPageOverlayback to summary
private PDDocument oddPageOverlay
oddPageOverlayFilenameback to summary
private String oddPageOverlayFilename
oddPageOverlayPageback to summary
private Overlay.LayoutPage oddPageOverlayPage
openDocumentsSetback to summary
private final Set<PDDocument> openDocumentsSet
positionback to summary
private Overlay.Position position
specificPageOverlayLayoutPageMapback to summary
private Map<Integer, Overlay.LayoutPage> specificPageOverlayLayoutPageMap
useAllOverlayPagesback to summary
private boolean useAllOverlayPages

Constructor Detail

Overlayback to summary
public Overlay()

Method Detail

addOriginalContentback to summary
private void addOriginalContent(COSBase contents, COSArray contentArray) throws IOException
calculateAffineTransformback to summary
protected AffineTransform calculateAffineTransform(PDPage page, PDRectangle overlayMediaBox)

Calculate the transform to be used when positioning the overlay. The default implementation centers on the destination. Override this method to do your own, e.g. move to a corner, or rotate.

Parameters
page:PDPage

The page that will get the overlay.

overlayMediaBox:PDRectangle

The overlay media box.

Returns:AffineTransform

The affine transform to be used.

closeback to summary
public void close() throws IOException

Implements java.io.Closeable.close.

Close all input documents which were used for the overlay and opened by this class.

Annotations
@Override
Exceptions
IOException:
if something went wrong
createCombinedContentStreamback to summary
private COSStream createCombinedContentStream(COSBase contents) throws IOException
createContentStreamListback to summary
private List<COSStream> createContentStreamList(COSBase contents) throws IOException
createLayoutPageback to summary
private Overlay.LayoutPage createLayoutPage(PDPage page) throws IOException

Create a LayoutPage object from given PDPage object.

createOverlayStreamback to summary
private COSStream createOverlayStream(PDPage page, Overlay.LayoutPage layoutPage, COSName xObjectId) throws IOException
createOverlayXObjectback to summary
private COSName createOverlayXObject(PDPage page, Overlay.LayoutPage layoutPage, PDFCloneUtility cloner) throws IOException
createStreamback to summary
private COSStream createStream(String content) throws IOException
float2Stringback to summary
private String float2String(float floatValue)
getDefaultOverlayFileback to summary
public String getDefaultOverlayFile()

Returns the default overlay file.

Returns:String

the default overlay file

getInputFileback to summary
public String getInputFile()

Returns the input file.

Returns:String

the input file

getLayoutPageback to summary
private Overlay.LayoutPage getLayoutPage(PDDocument doc) throws IOException

Create a LayoutPage object from the first page of the given document.

getLayoutPageback to summary
private Overlay.LayoutPage getLayoutPage(int pageNumber, int numberOfPages)
getLayoutPagesback to summary
private Map<Integer, Overlay.LayoutPage> getLayoutPages(PDDocument doc) throws IOException
loadPDFback to summary
private PDDocument loadPDF(String pdfName) throws IOException
loadPDFsback to summary
private void loadPDFs() throws IOException
overlayback to summary
public PDDocument overlay(Map<Integer, String> specificPageOverlayMap) throws IOException

This will add overlays to a document.

Parameters
specificPageOverlayMap:Map<Integer, String>

Optional map of overlay files of which the first page will be used for specific pages of the input document. The page numbers are 1-based. The map must be empty (but not null) if no specific mappings are used.

Returns:PDDocument

The modified input PDF document, which has to be saved and closed by the caller. If the input document was passed by setInputPDF(PDDocument) then it is that object that is returned.

Exceptions
IOException:
if something went wrong.
IllegalArgumentException:
if the input document is missing.
overlayDocumentsback to summary
public PDDocument overlayDocuments(Map<Integer, PDDocument> specificPageOverlayDocumentMap) throws IOException

This will add overlays documents to a document. If you created the overlay documents with subsetted fonts, you need to save them first so that the subsetting gets done.

Parameters
specificPageOverlayDocumentMap:Map<Integer, PDDocument>

Optional map of overlay documents for specific pages. The page numbers are 1-based. The map must be empty (but not null) if no specific mappings are used.

Returns:PDDocument

The modified input PDF document, which has to be saved and closed by the caller. If the input document was passed by setInputPDF(PDDocument) then it is that object that is returned.

Exceptions
IOException:
if something went wrong
overlayPageback to summary
private void overlayPage(PDPage page, Overlay.LayoutPage layoutPage, COSArray array, PDFCloneUtility cloner) throws IOException
processPagesback to summary
private void processPages(PDDocument document) throws IOException
setAllPagesOverlayFileback to summary
public void setAllPagesOverlayFile(String allPagesOverlayFile)

Sets the all pages overlay file.

Parameters
allPagesOverlayFile:String

the all pages overlay file

setAllPagesOverlayPDFback to summary
public void setAllPagesOverlayPDF(PDDocument allPagesOverlayPDF)

Sets the all pages overlay PDF. If you created the overlay document with subsetted fonts, you need to save it first so that the subsetting gets done.

Parameters
allPagesOverlayPDF:PDDocument

the all pages overlay PDF. This should not be a PDDocument that you created on the fly, it should be saved first, if it contains any fonts that are subset.

setDefaultOverlayFileback to summary
public void setDefaultOverlayFile(String defaultOverlayFile)

Sets the default overlay file.

Parameters
defaultOverlayFile:String

the default overlay file

setDefaultOverlayPDFback to summary
public void setDefaultOverlayPDF(PDDocument defaultOverlayPDF)

Sets the default overlay PDF. If you created the overlay document with subsetted fonts, you need to save it first so that the subsetting gets done.

Parameters
defaultOverlayPDF:PDDocument

the default overlay PDF

setEvenPageOverlayFileback to summary
public void setEvenPageOverlayFile(String evenPageOverlayFile)

Sets the even page overlay file.

Parameters
evenPageOverlayFile:String

the even page overlay file

setEvenPageOverlayPDFback to summary
public void setEvenPageOverlayPDF(PDDocument evenPageOverlayPDF)

Sets the even page overlay PDF. If you created the overlay document with subsetted fonts, you need to save it first so that the subsetting gets done.

Parameters
evenPageOverlayPDF:PDDocument

the even page overlay PDF

setFirstPageOverlayFileback to summary
public void setFirstPageOverlayFile(String firstPageOverlayFile)

Sets the first page overlay file.

Parameters
firstPageOverlayFile:String

the first page overlay file

setFirstPageOverlayPDFback to summary
public void setFirstPageOverlayPDF(PDDocument firstPageOverlayPDF)

Sets the first page overlay PDF. If you created the overlay document with subsetted fonts, you need to save it first so that the subsetting gets done.

Parameters
firstPageOverlayPDF:PDDocument

the first page overlay PDF

setInputFileback to summary
public void setInputFile(String inputFile)

Sets the file to be overlaid.

Parameters
inputFile:String

the file to be overlaid. The PDDocument object gathered from opening this file will be returned by overlay(Map<Integer, String>).

setInputPDFback to summary
public void setInputPDF(PDDocument inputPDF)

Sets the PDF to be overlaid.

Parameters
inputPDF:PDDocument

the PDF to be overlaid. This will be the object that is returned by overlay(Map<Integer, String>).

setLastPageOverlayFileback to summary
public void setLastPageOverlayFile(String lastPageOverlayFile)

Sets the last page overlay file.

Parameters
lastPageOverlayFile:String

the last page overlay file

setLastPageOverlayPDFback to summary
public void setLastPageOverlayPDF(PDDocument lastPageOverlayPDF)

Sets the last page overlay PDF. If you created the overlay document with subsetted fonts, you need to save it first so that the subsetting gets done.

Parameters
lastPageOverlayPDF:PDDocument

the last page overlay PDF

setOddPageOverlayFileback to summary
public void setOddPageOverlayFile(String oddPageOverlayFile)

Sets the odd page overlay file.

Parameters
oddPageOverlayFile:String

the odd page overlay file

setOddPageOverlayPDFback to summary
public void setOddPageOverlayPDF(PDDocument oddPageOverlayPDF)

Sets the odd page overlay PDF. If you created the overlay document with subsetted fonts, you need to save it first so that the subsetting gets done.

Parameters
oddPageOverlayPDF:PDDocument

the odd page overlay PDF

setOverlayPositionback to summary
public void setOverlayPosition(Overlay.Position overlayPosition)

Sets the overlay position.

Parameters
overlayPosition:Overlay.Position

the overlay position

org.apache.pdfbox.multipdf back to summary

private final Class Overlay.LayoutPage

extends Object
Class Inheritance

Stores the overlay page information.

Field Summary

Modifier and TypeField and Description
private final COSStream
private final PDRectangle
private final COSDictionary
private final short

Constructor Summary

AccessConstructor and Description
private
LayoutPage(PDRectangle mediaBox, COSStream contentStream, COSDictionary resources, short rotation)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

overlayContentStreamback to summary
private final COSStream overlayContentStream
overlayMediaBoxback to summary
private final PDRectangle overlayMediaBox
overlayResourcesback to summary
private final COSDictionary overlayResources
overlayRotationback to summary
private final short overlayRotation

Constructor Detail

LayoutPageback to summary
private LayoutPage(PDRectangle mediaBox, COSStream contentStream, COSDictionary resources, short rotation)
org.apache.pdfbox.multipdf back to summary

public final Enum Overlay.Position

extends Enum<Overlay.Position>
Class Inheritance

Possible location of the overlaid pages: foreground or background.

Field Summary

Modifier and TypeField and Description
public static final Overlay.Position
public static final Overlay.Position

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static Overlay.Position
public static Overlay.Position[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

BACKGROUNDback to summary
public static final Overlay.Position BACKGROUND
FOREGROUNDback to summary
public static final Overlay.Position FOREGROUND

Constructor Detail

Positionback to summary
private Position()

Method Detail

valueOfback to summary
public static Overlay.Position valueOf(String name)
valuesback to summary
public static Overlay.Position[] values()