Package org.apache.pdfbox.multipdf
Class Overlay
- java.lang.Object
-
- org.apache.pdfbox.multipdf.Overlay
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
public class Overlay extends java.lang.Object implements java.io.Closeable
Adds an overlay to an existing PDF document. Based on code contributed by Balazs Jerk.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
Overlay.LayoutPage
Stores the overlay page information.static class
Overlay.Position
Possible location of the overlaid pages: foreground or background.
-
Field Summary
-
Constructor Summary
Constructors Constructor Description Overlay()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
addOriginalContent(COSBase contents, COSArray contentArray)
protected java.awt.geom.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)
private java.util.List<COSStream>
createContentStreamList(COSBase contents)
private COSStream
createOverlayStream(PDPage page, Overlay.LayoutPage layoutPage, COSName xObjectId)
private COSName
createOverlayXObject(PDPage page, Overlay.LayoutPage layoutPage)
private COSStream
createStream(java.lang.String content)
private java.lang.String
float2String(float floatValue)
java.lang.String
getDefaultOverlayFile()
Returns the default overlay file.java.lang.String
getInputFile()
Returns the input file.private Overlay.LayoutPage
getLayoutPage(int pageNumber, int numberOfPages)
private Overlay.LayoutPage
getLayoutPage(PDDocument doc)
private java.util.Map<java.lang.Integer,Overlay.LayoutPage>
getLayoutPages(PDDocument doc)
private PDDocument
loadPDF(java.lang.String pdfName)
private void
loadPDFs()
PDDocument
overlay(java.util.Map<java.lang.Integer,java.lang.String> specificPageOverlayFile)
This will add overlays to a document.PDDocument
overlayDocuments(java.util.Map<java.lang.Integer,PDDocument> specificPageOverlayDocuments)
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(java.lang.String allPagesOverlayFile)
Sets the all pages overlay file.void
setAllPagesOverlayPDF(PDDocument allPagesOverlayPDF)
Sets the all pages overlay PDF.void
setDefaultOverlayFile(java.lang.String defaultOverlayFile)
Sets the default overlay file.void
setDefaultOverlayPDF(PDDocument defaultOverlayPDF)
Sets the default overlay PDF.void
setEvenPageOverlayFile(java.lang.String evenPageOverlayFile)
Sets the even page overlay file.void
setEvenPageOverlayPDF(PDDocument evenPageOverlayPDF)
Sets the even page overlay PDF.void
setFirstPageOverlayFile(java.lang.String firstPageOverlayFile)
Sets the first page overlay file.void
setFirstPageOverlayPDF(PDDocument firstPageOverlayPDF)
Sets the first page overlay PDF.void
setInputFile(java.lang.String inputFile)
Sets the file to be overlaid.void
setInputPDF(PDDocument inputPDF)
Sets the PDF to be overlaid.void
setLastPageOverlayFile(java.lang.String lastPageOverlayFile)
Sets the last page overlay file.void
setLastPageOverlayPDF(PDDocument lastPageOverlayPDF)
Sets the last page overlay PDF.void
setOddPageOverlayFile(java.lang.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 Detail
-
defaultOverlayPage
private Overlay.LayoutPage defaultOverlayPage
-
firstPageOverlayPage
private Overlay.LayoutPage firstPageOverlayPage
-
lastPageOverlayPage
private Overlay.LayoutPage lastPageOverlayPage
-
oddPageOverlayPage
private Overlay.LayoutPage oddPageOverlayPage
-
evenPageOverlayPage
private Overlay.LayoutPage evenPageOverlayPage
-
openDocuments
private final java.util.Set<PDDocument> openDocuments
-
specificPageOverlayPage
private java.util.Map<java.lang.Integer,Overlay.LayoutPage> specificPageOverlayPage
-
position
private Overlay.Position position
-
inputFileName
private java.lang.String inputFileName
-
inputPDFDocument
private PDDocument inputPDFDocument
-
defaultOverlayFilename
private java.lang.String defaultOverlayFilename
-
defaultOverlay
private PDDocument defaultOverlay
-
firstPageOverlayFilename
private java.lang.String firstPageOverlayFilename
-
firstPageOverlay
private PDDocument firstPageOverlay
-
lastPageOverlayFilename
private java.lang.String lastPageOverlayFilename
-
lastPageOverlay
private PDDocument lastPageOverlay
-
allPagesOverlayFilename
private java.lang.String allPagesOverlayFilename
-
allPagesOverlay
private PDDocument allPagesOverlay
-
oddPageOverlayFilename
private java.lang.String oddPageOverlayFilename
-
oddPageOverlay
private PDDocument oddPageOverlay
-
evenPageOverlayFilename
private java.lang.String evenPageOverlayFilename
-
evenPageOverlay
private PDDocument evenPageOverlay
-
numberOfOverlayPages
private int numberOfOverlayPages
-
useAllOverlayPages
private boolean useAllOverlayPages
-
-
Method Detail
-
overlay
public PDDocument overlay(java.util.Map<java.lang.Integer,java.lang.String> specificPageOverlayFile) throws java.io.IOException
This will add overlays to a document.- Parameters:
specificPageOverlayFile
- Optional map of overlay files 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:
java.io.IOException
- if something went wrong
-
overlayDocuments
public PDDocument overlayDocuments(java.util.Map<java.lang.Integer,PDDocument> specificPageOverlayDocuments) throws java.io.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:
specificPageOverlayDocuments
- 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:
java.io.IOException
- if something went wrong
-
close
public void close() throws java.io.IOException
Close all input documents which were used for the overlay and opened by this class.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
- if something went wrong
-
loadPDFs
private void loadPDFs() throws java.io.IOException
- Throws:
java.io.IOException
-
loadPDF
private PDDocument loadPDF(java.lang.String pdfName) throws java.io.IOException
- Throws:
java.io.IOException
-
getLayoutPage
private Overlay.LayoutPage getLayoutPage(PDDocument doc) throws java.io.IOException
- Throws:
java.io.IOException
-
getLayoutPages
private java.util.Map<java.lang.Integer,Overlay.LayoutPage> getLayoutPages(PDDocument doc) throws java.io.IOException
- Throws:
java.io.IOException
-
createCombinedContentStream
private COSStream createCombinedContentStream(COSBase contents) throws java.io.IOException
- Throws:
java.io.IOException
-
createContentStreamList
private java.util.List<COSStream> createContentStreamList(COSBase contents) throws java.io.IOException
- Throws:
java.io.IOException
-
processPages
private void processPages(PDDocument document) throws java.io.IOException
- Throws:
java.io.IOException
-
addOriginalContent
private void addOriginalContent(COSBase contents, COSArray contentArray) throws java.io.IOException
- Throws:
java.io.IOException
-
overlayPage
private void overlayPage(PDPage page, Overlay.LayoutPage layoutPage, COSArray array) throws java.io.IOException
- Throws:
java.io.IOException
-
getLayoutPage
private Overlay.LayoutPage getLayoutPage(int pageNumber, int numberOfPages)
-
createOverlayXObject
private COSName createOverlayXObject(PDPage page, Overlay.LayoutPage layoutPage)
-
createOverlayStream
private COSStream createOverlayStream(PDPage page, Overlay.LayoutPage layoutPage, COSName xObjectId) throws java.io.IOException
- Throws:
java.io.IOException
-
calculateAffineTransform
protected java.awt.geom.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
- The page that will get the overlay.overlayMediaBox
- The overlay media box.- Returns:
- The affine transform to be used.
-
float2String
private java.lang.String float2String(float floatValue)
-
createStream
private COSStream createStream(java.lang.String content) throws java.io.IOException
- Throws:
java.io.IOException
-
setOverlayPosition
public void setOverlayPosition(Overlay.Position overlayPosition)
Sets the overlay position.- Parameters:
overlayPosition
- the overlay position
-
setInputFile
public void setInputFile(java.lang.String inputFile)
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
public void setInputPDF(PDDocument inputPDF)
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
public java.lang.String getInputFile()
Returns the input file.- Returns:
- the input file
-
setDefaultOverlayFile
public void setDefaultOverlayFile(java.lang.String defaultOverlayFile)
Sets the default overlay file.- Parameters:
defaultOverlayFile
- the default overlay file
-
setDefaultOverlayPDF
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
- the default overlay PDF
-
getDefaultOverlayFile
public java.lang.String getDefaultOverlayFile()
Returns the default overlay file.- Returns:
- the default overlay file
-
setFirstPageOverlayFile
public void setFirstPageOverlayFile(java.lang.String firstPageOverlayFile)
Sets the first page overlay file.- Parameters:
firstPageOverlayFile
- the first page overlay file
-
setFirstPageOverlayPDF
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
- the first page overlay PDF
-
setLastPageOverlayFile
public void setLastPageOverlayFile(java.lang.String lastPageOverlayFile)
Sets the last page overlay file.- Parameters:
lastPageOverlayFile
- the last page overlay file
-
setLastPageOverlayPDF
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
- the last page overlay PDF
-
setAllPagesOverlayFile
public void setAllPagesOverlayFile(java.lang.String allPagesOverlayFile)
Sets the all pages overlay file.- Parameters:
allPagesOverlayFile
- the all pages overlay file
-
setAllPagesOverlayPDF
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
- 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
public void setOddPageOverlayFile(java.lang.String oddPageOverlayFile)
Sets the odd page overlay file.- Parameters:
oddPageOverlayFile
- the odd page overlay file
-
setOddPageOverlayPDF
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
- the odd page overlay PDF
-
setEvenPageOverlayFile
public void setEvenPageOverlayFile(java.lang.String evenPageOverlayFile)
Sets the even page overlay file.- Parameters:
evenPageOverlayFile
- the even page overlay file
-
setEvenPageOverlayPDF
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
- the even page overlay PDF
-
-