public class PdfDocument extends Object implements IEventDispatcher, Closeable, Serializable
Modifier and Type | Field and Description |
---|---|
protected PdfCatalog |
catalog
Document catalog.
|
protected boolean |
closed |
protected boolean |
closeReader |
protected boolean |
closeWriter |
protected PdfPage |
currentPage
Currently active page.
|
protected PageSize |
defaultPageSize
Default page size.
|
protected Set<PdfFont> |
documentFonts |
protected EventDispatcher |
eventDispatcher |
protected boolean |
flushUnusedObjects
flag determines whether to write unused objects to result document
|
protected PdfDocumentInfo |
info
Document info.
|
protected boolean |
isClosing |
protected PdfVersion |
pdfVersion
Document version.
|
protected StampingProperties |
properties |
protected PdfReader |
reader
PdfReader associated with the document.
|
protected int |
structParentIndex |
protected PdfStructTreeRoot |
structTreeRoot |
protected TagStructureContext |
tagStructureContext |
protected PdfDictionary |
trailer
Document trailed.
|
protected boolean |
userProperties |
protected PdfWriter |
writer
PdfWriter associated with the document.
|
protected byte[] |
xmpMetadata
XMP Metadata for the document.
|
Constructor and Description |
---|
PdfDocument(PdfReader reader)
Open PDF document in reading mode.
|
PdfDocument(PdfReader reader, PdfWriter writer)
Opens PDF document in the stamping mode.
|
PdfDocument(PdfReader reader, PdfWriter writer, StampingProperties properties)
Open PDF document in stamping mode.
|
PdfDocument(PdfWriter writer)
Open PDF document in writing mode.
|
Modifier and Type | Method and Description |
---|---|
protected void |
addCustomMetadataExtensions(XMPMeta xmpMeta)
Adds custom XMP metadata extension.
|
void |
addEventHandler(String type, IEventHandler handler)
Adds new event handler.
|
void |
addFileAttachment(String description, byte[] fileStore, String fileDisplay, PdfName mimeType, PdfDictionary fileParameter, PdfName afRelationshipValue)
Adds file attachment at document level.
|
void |
addFileAttachment(String description, PdfFileSpec fs)
Adds file attachment at document level.
|
void |
addFileAttachment(String description, String file, String fileDisplay, PdfName mimeType, PdfName afRelationshipValue)
Adds file attachment at document level.
|
void |
addNamedDestination(String key, PdfObject value)
This methods adds new name in the Dests NameTree.
|
PdfPage |
addNewPage()
Creates and adds new page to the end of document.
|
PdfPage |
addNewPage(int index)
Creates and inserts new page to the document.
|
PdfPage |
addNewPage(int index, PageSize pageSize)
Creates and inserts new page to the document.
|
PdfPage |
addNewPage(PageSize pageSize)
Creates and adds new page with the specified page size.
|
void |
addOutputIntent(PdfOutputIntent outputIntent)
Adds PdfOutputIntent that shall specify the colour characteristics of output devices on which the document might be rendered.
|
PdfPage |
addPage(int index, PdfPage page)
Inserts page to the document.
|
PdfPage |
addPage(PdfPage page)
Adds page to the end of document.
|
protected void |
checkAndAddPage(int index, PdfPage page)
Checks page before adding and add.
|
protected void |
checkAndAddPage(PdfPage page)
Checks page before adding.
|
protected void |
checkClosingStatus()
checks whether a method is invoked at the closed document
|
protected void |
checkIsoConformance()
Checks whether PDF document conforms a specific standard.
|
void |
checkIsoConformance(Object obj, IsoKey key)
Checks whether PDF document conforms a specific standard.
|
void |
checkIsoConformance(Object obj, IsoKey key, PdfResources resources)
Checks whether PDF document conforms a specific standard.
|
void |
checkShowTextIsoConformance(Object gState, PdfResources resources)
Checks whether PDF document conforms a specific standard.
|
void |
close()
Close PDF document.
|
List<PdfPage> |
copyPagesTo(int pageFrom, int pageTo, PdfDocument toDocument)
Copies a range of pages from current document to toDocument appending copied pages to the end.
|
List<PdfPage> |
copyPagesTo(int pageFrom, int pageTo, PdfDocument toDocument, int insertBeforePage)
Copies a range of pages from current document to toDocument .
|
List<PdfPage> |
copyPagesTo(int pageFrom, int pageTo, PdfDocument toDocument, int insertBeforePage, IPdfPageExtraCopier copier)
Copies a range of pages from current document to toDocument .
|
List<PdfPage> |
copyPagesTo(int pageFrom, int pageTo, PdfDocument toDocument, IPdfPageExtraCopier copier)
Copies a range of pages from current document to toDocument appending copied pages to the end.
|
List<PdfPage> |
copyPagesTo(List<Integer> pagesToCopy, PdfDocument toDocument)
Copies a range of pages from current document to toDocument appending copied pages to the end.
|
List<PdfPage> |
copyPagesTo(List<Integer> pagesToCopy, PdfDocument toDocument, int insertBeforePage)
Copies a range of pages from current document to toDocument .
|
List<PdfPage> |
copyPagesTo(List<Integer> pagesToCopy, PdfDocument toDocument, int insertBeforePage, IPdfPageExtraCopier copier)
Copies a range of pages from current document to toDocument .
|
List<PdfPage> |
copyPagesTo(List<Integer> pagesToCopy, PdfDocument toDocument, IPdfPageExtraCopier copier)
Copies a range of pages from current document to toDocument appending copied pages to the end.
|
PdfIndirectReference |
createNextIndirectReference()
Creates next available indirect reference.
|
void |
dispatchEvent(Event event)
Dispatches an event.
|
void |
dispatchEvent(Event event, boolean delayed)
Dispatches a delayed event.
|
protected void |
flushFonts() |
protected void |
flushObject(PdfObject pdfObject, boolean canBeInObjStm)
Flush an object.
|
PdfCatalog |
getCatalog()
Gets PDF catalog.
|
protected Counter |
getCounter()
Gets Counter instance.
|
PageSize |
getDefaultPageSize()
Gets default page size.
|
protected Set<PdfFont> |
getDocumentFonts()
List all newly added or loaded fonts
|
PdfDocumentInfo |
getDocumentInfo()
Gets document information dictionary.
|
PdfPage |
getFirstPage()
Get the first page of the document.
|
PdfPage |
getLastPage()
Gets the last page of the document.
|
int |
getNextStructParentIndex()
Gets next parent index of tagged document.
|
int |
getNumberOfPages()
Gets number of pages of the document.
|
int |
getNumberOfPdfObjects()
Get number of indirect objects in the document.
|
PdfOutline |
getOutlines(boolean updateOutlines)
This method returns a complete outline tree of the whole document.
|
PdfPage |
getPage(int pageNum)
Gets the page by page number.
|
PdfPage |
getPage(PdfDictionary pageDictionary)
Gets the PdfPage instance by PdfDictionary .
|
String[] |
getPageLabels()
This method retrieves the page labels from a document as an array of String objects.
|
int |
getPageNumber(PdfDictionary pageDictionary)
Gets page number by PdfDictionary .
|
int |
getPageNumber(PdfPage page)
Gets page number by page.
|
PdfObject |
getPdfObject(int objNum)
Gets PdfObject by object number.
|
PdfVersion |
getPdfVersion()
Gets PDF version.
|
PdfReader |
getReader()
Gets PdfReader associated with the document.
|
PdfStructTreeRoot |
getStructTreeRoot()
Gets PdfStructTreeRoot of tagged document.
|
TagStructureContext |
getTagStructureContext()
Gets document TagStructureContext .
|
PdfDictionary |
getTrailer()
Gets document trailer.
|
PdfWriter |
getWriter()
Gets PdfWriter associated with the document.
|
byte[] |
getXmpMetadata()
Gets XMPMetadata.
|
byte[] |
getXmpMetadata(boolean createNew)
Gets XMPMetadata or create a new one.
|
boolean |
hasEventHandler(String type)
Checks if event dispatcher as an event handler assigned for a certain event type.
|
boolean |
hasOutlines()
Indicates if the document has any outlines
|
void |
initializeOutlines()
This method initializes an outline tree of the document and sets outline mode to true.
|
protected void |
initTagStructureContext()
Initialize TagStructureContext .
|
boolean |
isAppendMode()
Returns true if the document is opened in append mode, and false otherwise.
|
boolean |
isClosed()
Gets close status of the document.
|
boolean |
isCloseReader()
Checks, whether close() method will close associated PdfReader.
|
boolean |
isCloseWriter()
Checks, whether close() method will close associated PdfWriter.
|
boolean |
isFlushUnusedObjects()
Checks, whether close() will flush unused objects, e.g.
|
boolean |
isTagged()
Gets tagged status of the document.
|
List<PdfIndirectReference> |
listIndirectReferences()
Gets static copy of cross reference table.
|
protected void |
markObjectAsMustBeFlushed(PdfObject pdfObject)
Mark an object with PdfObject.MUST_BE_FLUSHED .
|
protected void |
open(PdfVersion newPdfVersion)
Initializes document.
|
void |
removeAllHandlers()
Remove all event handlers.
|
void |
removeEventHandler(String type, IEventHandler handler)
Removes event handler.
|
PdfPage |
removePage(int pageNum)
Removes page from the document by page number.
|
boolean |
removePage(PdfPage page)
Removes the first occurrence of the specified page from this document, if it is present.
|
void |
setCloseReader(boolean closeReader)
Sets, whether close() method shall close associated PdfReader.
|
void |
setCloseWriter(boolean closeWriter)
Sets, whether close() method shall close associated PdfWriter.
|
void |
setDefaultPageSize(PageSize pageSize)
Sets default page size.
|
void |
setFlushUnusedObjects(boolean flushUnusedObjects)
Sets, whether close() shall flush unused objects, e.g.
|
void |
setTagged() |
void |
setUserProperties(boolean userProperties)
Sets the flag indicating the presence of structure elements that contain user properties attributes.
|
protected void |
setXmpMetadata(byte[] xmpMetadata)
Use this method to set the XMP Metadata.
|
void |
setXmpMetadata(XMPMeta xmpMeta) |
void |
setXmpMetadata(XMPMeta xmpMeta, SerializeOptions serializeOptions) |
protected void |
storeLinkAnnotation(PdfPage page, PdfLinkAnnotation annotation)
Save the link annotation in a temporary storage for further copying.
|
protected XMPMeta |
updateDefaultXmpMetadata()
Update XMP metadata values from PdfDocumentInfo .
|
protected void |
updateXmpMetadata()
Updates XMP metadata.
|
protected PdfPage currentPage
protected PageSize defaultPageSize
protected EventDispatcher eventDispatcher
protected PdfWriter writer
protected PdfReader reader
protected byte[] xmpMetadata
protected PdfCatalog catalog
protected PdfDictionary trailer
protected PdfDocumentInfo info
protected PdfVersion pdfVersion
protected final StampingProperties properties
protected PdfStructTreeRoot structTreeRoot
protected int structParentIndex
protected boolean userProperties
protected boolean closeReader
protected boolean closeWriter
protected boolean isClosing
protected boolean closed
protected boolean flushUnusedObjects
protected TagStructureContext tagStructureContext
public PdfDocument(PdfReader reader)
reader
- PDF reader.
public PdfDocument(PdfWriter writer)
writer
- PDF writer
public PdfDocument(PdfReader reader, PdfWriter writer)
reader
- PDF reader.
writer
- PDF writer.
public PdfDocument(PdfReader reader, PdfWriter writer, StampingProperties properties)
reader
- PDF reader.
writer
- PDF writer.
properties
- properties of the stamping process
protected void setXmpMetadata(byte[] xmpMetadata)
xmpMetadata
- The xmpMetadata to set.
public void setXmpMetadata(XMPMeta xmpMeta, SerializeOptions serializeOptions) throws XMPException
XMPException
public void setXmpMetadata(XMPMeta xmpMeta) throws XMPException
XMPException
public byte[] getXmpMetadata()
public byte[] getXmpMetadata(boolean createNew)
createNew
- if true, create a new empty XMPMetadata if it did not present.
public PdfObject getPdfObject(int objNum)
objNum
- object number.
PdfObject
or null
, if object not found.
public int getNumberOfPdfObjects()
public PdfPage getPage(int pageNum)
pageNum
- page number.
public PdfPage getPage(PdfDictionary pageDictionary)
PdfPage
instance by PdfDictionary
.
pageDictionary
- PdfDictionary
that present page.
PdfDictionary
.
public PdfPage getFirstPage()
public PdfPage getLastPage()
public PdfPage addNewPage()
public PdfPage addNewPage(PageSize pageSize)
pageSize
- page size of the new page
public PdfPage addNewPage(int index)
index
- position to addPage page to
PdfException
- in case page
is flushed
public PdfPage addNewPage(int index, PageSize pageSize)
index
- position to addPage page to
pageSize
- page size of the new page
PdfException
- in case page
is flushed
public PdfPage addPage(PdfPage page)
page
- page to add.
PdfException
- in case page
is flushed
public PdfPage addPage(int index, PdfPage page)
index
- position to addPage page to
page
- page to addPage
PdfException
- in case page
is flushed
public int getNumberOfPages()
public int getPageNumber(PdfPage page)
page
- the page.
public int getPageNumber(PdfDictionary pageDictionary)
PdfDictionary
.
pageDictionary
- PdfDictionary
that present page.
PdfDictionary
.
public boolean removePage(PdfPage page)
page
- page to be removed from this document, if present
public PdfPage removePage(int pageNum)
pageNum
- the one-based index of the PdfPage to be removed
public PdfDocumentInfo getDocumentInfo()
public PageSize getDefaultPageSize()
public void setDefaultPageSize(PageSize pageSize)
pageSize
- page size to be set as default.
public void addEventHandler(String type, IEventHandler handler)
addEventHandler
in interface IEventDispatcher
type
- a type of event to be handled.
handler
- event handler.
public void dispatchEvent(Event event)
dispatchEvent
in interface IEventDispatcher
public void dispatchEvent(Event event, boolean delayed)
dispatchEvent
in interface IEventDispatcher
public boolean hasEventHandler(String type)
hasEventHandler
in interface IEventDispatcher
public void removeEventHandler(String type, IEventHandler handler)
removeEventHandler
in interface IEventDispatcher
public void removeAllHandlers()
removeAllHandlers
in interface IEventDispatcher
public PdfWriter getWriter()
PdfWriter
associated with the document.
public PdfReader getReader()
PdfReader
associated with the document.
public boolean isAppendMode()
true
if the document is opened in append mode, and false
otherwise.
true
if the document is opened in append mode, and false
otherwise.
public PdfIndirectReference createNextIndirectReference()
public PdfVersion getPdfVersion()
public PdfCatalog getCatalog()
public void close()
close
in interface Closeable
close
in interface AutoCloseable
public boolean isClosed()
public boolean isTagged()
public void setTagged()
public PdfStructTreeRoot getStructTreeRoot()
PdfStructTreeRoot
of tagged document.
PdfStructTreeRoot
in case tagged document, otherwise false.
isTagged()
, getNextStructParentIndex()
public int getNextStructParentIndex()
isTagged()
, getNextStructParentIndex()
public TagStructureContext getTagStructureContext()
TagStructureContext
. The document must be tagged, otherwise an exception will be thrown.
TagStructureContext
.
public List<PdfPage> copyPagesTo(int pageFrom, int pageTo, PdfDocument toDocument, int insertBeforePage)
toDocument
. Use this method if you want to copy pages across tagged documents. This will keep resultant PDF structure consistent.
pageFrom
- start of the range of pages to be copied.
pageTo
- end of the range of pages to be copied.
toDocument
- a document to copy pages to.
insertBeforePage
- a position where to insert copied pages.
public List<PdfPage> copyPagesTo(int pageFrom, int pageTo, PdfDocument toDocument, int insertBeforePage, IPdfPageExtraCopier copier)
toDocument
. Use this method if you want to copy pages across tagged documents. This will keep resultant PDF structure consistent.
pageFrom
- 1-based start of the range of pages to be copied.
pageTo
- 1-based end of the range of pages to be copied.
toDocument
- a document to copy pages to.
insertBeforePage
- a position where to insert copied pages.
copier
- a copier which bears a special copy logic. May be NULL
public List<PdfPage> copyPagesTo(int pageFrom, int pageTo, PdfDocument toDocument)
toDocument
appending copied pages to the end. Use this method if you want to copy pages across tagged documents. This will keep resultant PDF structure consistent.
pageFrom
- 1-based start of the range of pages to be copied.
pageTo
- 1-based end of the range of pages to be copied.
toDocument
- a document to copy pages to.
public List<PdfPage> copyPagesTo(int pageFrom, int pageTo, PdfDocument toDocument, IPdfPageExtraCopier copier)
toDocument
appending copied pages to the end. Use this method if you want to copy pages across tagged documents. This will keep resultant PDF structure consistent.
pageFrom
- 1-based start of the range of pages to be copied.
pageTo
- 1-based end of the range of pages to be copied.
toDocument
- a document to copy pages to.
copier
- a copier which bears a special copy logic. May be null.
public List<PdfPage> copyPagesTo(List<Integer> pagesToCopy, PdfDocument toDocument, int insertBeforePage)
toDocument
. Use this method if you want to copy pages across tagged documents. This will keep resultant PDF structure consistent.
pagesToCopy
- list of pages to be copied. TreeSet for the order of the pages to be natural.
toDocument
- a document to copy pages to.
insertBeforePage
- a position where to insert copied pages.
public List<PdfPage> copyPagesTo(List<Integer> pagesToCopy, PdfDocument toDocument, int insertBeforePage, IPdfPageExtraCopier copier)
toDocument
. Use this method if you want to copy pages across tagged documents. This will keep resultant PDF structure consistent.
pagesToCopy
- list of pages to be copied. TreeSet for the order of the pages to be natural.
toDocument
- a document to copy pages to.
insertBeforePage
- a position where to insert copied pages.
copier
- a copier which bears a special copy logic. May be NULL
public List<PdfPage> copyPagesTo(List<Integer> pagesToCopy, PdfDocument toDocument)
toDocument
appending copied pages to the end. Use this method if you want to copy pages across tagged documents. This will keep resultant PDF structure consistent.
pagesToCopy
- list of pages to be copied. TreeSet for the order of the pages to be natural.
toDocument
- a document to copy pages to.
public List<PdfPage> copyPagesTo(List<Integer> pagesToCopy, PdfDocument toDocument, IPdfPageExtraCopier copier)
toDocument
appending copied pages to the end. Use this method if you want to copy pages across tagged documents. This will keep resultant PDF structure consistent.
pagesToCopy
- list of pages to be copied. TreeSet for the order of the pages to be natural.
toDocument
- a document to copy pages to.
copier
- a copier which bears a special copy logic
public boolean isCloseReader()
close()
method will close associated PdfReader.
close()
method is going to close associated PdfReader, otherwise false.
public void setCloseReader(boolean closeReader)
close()
method shall close associated PdfReader.
closeReader
- true, close()
method shall close associated PdfReader, otherwise false.
public boolean isCloseWriter()
close()
method will close associated PdfWriter.
close()
method is going to close associated PdfWriter, otherwise false.
public void setCloseWriter(boolean closeWriter)
close()
method shall close associated PdfWriter.
closeWriter
- true, close()
method shall close associated PdfWriter, otherwise false.
public boolean isFlushUnusedObjects()
close()
will flush unused objects, e.g. unreachable from PDF Catalog. By default - false.
close()
shall not flush unused objects, otherwise true.
public void setFlushUnusedObjects(boolean flushUnusedObjects)
close()
shall flush unused objects, e.g. unreachable from PDF Catalog.
flushUnusedObjects
- false, if close()
shall not flush unused objects, otherwise true.
public PdfOutline getOutlines(boolean updateOutlines)
updateOutlines
- if the flag is true, the method read the whole document and creates outline tree. If false the method gets cached outline tree (if it was cached via calling getOutlines method before).
PdfOutline
object.
public void initializeOutlines()
public void addNamedDestination(String key, PdfObject value)
key
- Name of the destination.
value
- An object destination refers to. Must be an array or a dictionary with key /D and array. See ISO 32000-1 12.3.2.3 for more info.
public List<PdfIndirectReference> listIndirectReferences()
public PdfDictionary getTrailer()
public void addOutputIntent(PdfOutputIntent outputIntent)
PdfOutputIntent
that shall specify the colour characteristics of output devices on which the document might be rendered.
outputIntent
- PdfOutputIntent
to add.
PdfOutputIntent
public void checkIsoConformance(Object obj, IsoKey key)
obj
- An object to conform.
key
- type of object to conform.
public void checkIsoConformance(Object obj, IsoKey key, PdfResources resources)
obj
- an object to conform.
key
- type of object to conform.
resources
- PdfResources
associated with an object to check.
public void checkShowTextIsoConformance(Object gState, PdfResources resources)
gState
- a CanvasGraphicsState
object to conform.
resources
- PdfResources
associated with an object to check.
public void addFileAttachment(String description, byte[] fileStore, String fileDisplay, PdfName mimeType, PdfDictionary fileParameter, PdfName afRelationshipValue)
description
- the file description
fileStore
- an array with the file.
fileDisplay
- the actual file name stored in the pdf
mimeType
- mime type of the file
fileParameter
- the optional extra file parameters such as the creation or modification date
afRelationshipValue
- if null
, PdfName.Unspecified
will be added. Shall be one of: PdfName.Source
, PdfName.Data
, PdfName.Alternative
, PdfName.Supplement
or PdfName.Unspecified
.
public void addFileAttachment(String description, String file, String fileDisplay, PdfName mimeType, PdfName afRelationshipValue) throws IOException
description
- the file description
file
- the path to the file.
fileDisplay
- the actual file name stored in the pdf
mimeType
- mime type of the file
afRelationshipValue
- if null
, PdfName.Unspecified
will be added. Shall be one of: PdfName.Source
, PdfName.Data
, PdfName.Alternative
, PdfName.Supplement
or PdfName.Unspecified
.
IOException
public void addFileAttachment(String description, PdfFileSpec fs)
description
- the file description
fs
- PdfFileSpec
object.
public String[] getPageLabels()
String
list of page labels if they were found, or null
otherwise
public boolean hasOutlines()
true
, if there are outlines and false
otherwise.
public void setUserProperties(boolean userProperties)
userProperties
- the user properties flag
protected void initTagStructureContext()
TagStructureContext
.
protected void storeLinkAnnotation(PdfPage page, PdfLinkAnnotation annotation)
page
- just copied PdfPage
link annotation belongs to.
annotation
- PdfLinkAnnotation
itself.
protected void checkIsoConformance()
protected void markObjectAsMustBeFlushed(PdfObject pdfObject)
PdfObject.MUST_BE_FLUSHED
.
pdfObject
- an object to mark.
protected void flushObject(PdfObject pdfObject, boolean canBeInObjStm) throws IOException
pdfObject
- object to flush.
canBeInObjStm
- indicates whether object can be placed into object stream.
IOException
- on error.
protected void open(PdfVersion newPdfVersion)
newPdfVersion
- new pdf version of the resultant file if stamper is used and the version needs to be changed, or null
otherwise
protected void addCustomMetadataExtensions(XMPMeta xmpMeta)
xmpMeta
- XMPMeta
to add custom metadata to.
protected void updateXmpMetadata()
protected XMPMeta updateDefaultXmpMetadata() throws XMPException
PdfDocumentInfo
.
XMPException
protected Set<PdfFont> getDocumentFonts()
PdfFonts
.
protected void flushFonts()
protected void checkAndAddPage(int index, PdfPage page)
index
- one-base index of the page.
page
- PdfPage
to add.
protected void checkAndAddPage(PdfPage page)
page
- PdfPage
to add.
protected void checkClosingStatus()
Copyright © 1998–2016 iText Group NV. All rights reserved.