Package org.apache.pdfbox.multipdf
Class Overlay
java.lang.Object
org.apache.pdfbox.multipdf.Overlay
- All Implemented Interfaces:
Closeable
,AutoCloseable
Adds an overlay to an existing PDF document.
Based on code contributed by Balazs Jerk.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
Stores the overlay page information.static enum
Possible location of the overlaid pages: foreground or background. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate 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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate void
addOriginalContent
(COSBase contents, COSArray contentArray) protected AffineTransform
calculateAffineTransform
(PDPage page, PDRectangle overlayMediaBox) Calculate the transform to be used when positioning the overlay.void
close()
Close all input documents which were used for the overlay and opened by this class.private COSStream
createCombinedContentStream
(COSBase contents) createContentStreamList
(COSBase contents) 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
createOverlayXObject
(PDPage page, Overlay.LayoutPage layoutPage) private COSStream
createStream
(String content) private String
float2String
(float floatValue) Returns the default overlay file.Returns the input file.private Overlay.LayoutPage
getLayoutPage
(int pageNumber, int numberOfPages) private Overlay.LayoutPage
getLayoutPage
(PDDocument doc) Create a LayoutPage object from the first page of the given document.private Map<Integer,
Overlay.LayoutPage> getLayoutPages
(PDDocument doc) private PDDocument
private void
loadPDFs()
This will add overlays to a document.overlayDocuments
(Map<Integer, PDDocument> specificPageOverlayDocumentMap) This will add overlays documents to a document.private void
overlayPage
(PDPage page, Overlay.LayoutPage layoutPage, COSArray array) private void
processPages
(PDDocument document) void
setAllPagesOverlayFile
(String allPagesOverlayFile) Sets the all pages overlay file.void
setAllPagesOverlayPDF
(PDDocument allPagesOverlayPDF) Sets the all pages overlay PDF.void
setDefaultOverlayFile
(String defaultOverlayFile) Sets the default overlay file.void
setDefaultOverlayPDF
(PDDocument defaultOverlayPDF) Sets the default overlay PDF.void
setEvenPageOverlayFile
(String evenPageOverlayFile) Sets the even page overlay file.void
setEvenPageOverlayPDF
(PDDocument evenPageOverlayPDF) Sets the even page overlay PDF.void
setFirstPageOverlayFile
(String firstPageOverlayFile) Sets the first page overlay file.void
setFirstPageOverlayPDF
(PDDocument firstPageOverlayPDF) Sets the first page overlay PDF.void
setInputFile
(String inputFile) Sets the file to be overlaid.void
setInputPDF
(PDDocument inputPDF) Sets the PDF to be overlaid.void
setLastPageOverlayFile
(String lastPageOverlayFile) Sets the last page overlay file.void
setLastPageOverlayPDF
(PDDocument lastPageOverlayPDF) Sets the last page overlay PDF.void
setOddPageOverlayFile
(String oddPageOverlayFile) Sets the odd page overlay file.void
setOddPageOverlayPDF
(PDDocument oddPageOverlayPDF) Sets the odd page overlay PDF.void
setOverlayPosition
(Overlay.Position overlayPosition) Sets the overlay position.
-
Field Details
-
defaultOverlayPage
-
firstPageOverlayPage
-
lastPageOverlayPage
-
oddPageOverlayPage
-
evenPageOverlayPage
-
openDocumentsSet
-
specificPageOverlayLayoutPageMap
-
position
-
inputFileName
-
inputPDFDocument
-
defaultOverlayFilename
-
defaultOverlay
-
firstPageOverlayFilename
-
firstPageOverlay
-
lastPageOverlayFilename
-
lastPageOverlay
-
allPagesOverlayFilename
-
allPagesOverlay
-
oddPageOverlayFilename
-
oddPageOverlay
-
evenPageOverlayFilename
-
evenPageOverlay
-
numberOfOverlayPages
private int numberOfOverlayPages -
useAllOverlayPages
private boolean useAllOverlayPages
-
-
Constructor Details
-
Overlay
public Overlay()
-
-
Method Details
-
overlay
This will add overlays to a document.- Parameters:
specificPageOverlayMap
- 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:
- 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. - Throws:
IOException
- if something went wrong.IllegalArgumentException
- if the input document is missing.
-
overlayDocuments
public PDDocument overlayDocuments(Map<Integer, PDDocument> specificPageOverlayDocumentMap) throws IOExceptionThis 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
- 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:
- 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. - Throws:
IOException
- if something went wrong
-
close
Close all input documents which were used for the overlay and opened by this class.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
- if something went wrong
-
loadPDFs
- Throws:
IOException
-
loadPDF
- Throws:
IOException
-
getLayoutPage
Create a LayoutPage object from the first page of the given document.- Parameters:
doc
-- Returns:
- Throws:
IOException
-
createLayoutPage
Create a LayoutPage object from given PDPage object.- Returns:
- Throws:
IOException
-
getLayoutPages
- Throws:
IOException
-
createCombinedContentStream
- Throws:
IOException
-
createContentStreamList
- Throws:
IOException
-
processPages
- Throws:
IOException
-
addOriginalContent
- Throws:
IOException
-
overlayPage
private void overlayPage(PDPage page, Overlay.LayoutPage layoutPage, COSArray array) throws IOException - Throws:
IOException
-
getLayoutPage
-
createOverlayXObject
-
createOverlayStream
private COSStream createOverlayStream(PDPage page, Overlay.LayoutPage layoutPage, COSName xObjectId) throws IOException - Throws:
IOException
-
calculateAffineTransform
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
- The page that will get the overlay.overlayMediaBox
- The overlay media box.- Returns:
- The affine transform to be used.
-
float2String
-
createStream
- Throws:
IOException
-
setOverlayPosition
Sets the overlay position.- Parameters:
overlayPosition
- the overlay position
-
setInputFile
Sets the file to be overlaid.- Parameters:
inputFile
- the file to be overlaid. ThePDDocument
object gathered from opening this file will be returned byoverlay(Map<Integer, String>)
.
-
setInputPDF
Sets the PDF to be overlaid.- Parameters:
inputPDF
- the PDF to be overlaid. This will be the object that is returned byoverlay(Map<Integer, String>)
.
-
getInputFile
Returns the input file.- Returns:
- the input file
-
setDefaultOverlayFile
Sets the default overlay file.- Parameters:
defaultOverlayFile
- the default overlay file
-
setDefaultOverlayPDF
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
- the default overlay PDF
-
getDefaultOverlayFile
Returns the default overlay file.- Returns:
- the default overlay file
-
setFirstPageOverlayFile
Sets the first page overlay file.- Parameters:
firstPageOverlayFile
- the first page overlay file
-
setFirstPageOverlayPDF
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
- the first page overlay PDF
-
setLastPageOverlayFile
Sets the last page overlay file.- Parameters:
lastPageOverlayFile
- the last page overlay file
-
setLastPageOverlayPDF
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
- the last page overlay PDF
-
setAllPagesOverlayFile
Sets the all pages overlay file.- Parameters:
allPagesOverlayFile
- the all pages overlay file
-
setAllPagesOverlayPDF
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
- 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.
-
setOddPageOverlayFile
Sets the odd page overlay file.- Parameters:
oddPageOverlayFile
- the odd page overlay file
-
setOddPageOverlayPDF
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
- the odd page overlay PDF
-
setEvenPageOverlayFile
Sets the even page overlay file.- Parameters:
evenPageOverlayFile
- the even page overlay file
-
setEvenPageOverlayPDF
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
- the even page overlay PDF
-