Class PdfPage
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedPdfPage(PdfDictionary pdfObject) protectedPdfPage(PdfDocument pdfDocument) protectedPdfPage(PdfDocument pdfDocument, PageSize pageSize) -
Method Summary
Modifier and TypeMethodDescriptionaddAnnotation(int index, PdfAnnotation annotation, boolean tagAnnotation) Adds specifiedPdfAnnotationto specified index in annotations array with or without autotagging.addAnnotation(PdfAnnotation annotation) Adds specified annotation to the end of annotations array and tagged it.voidAdds file associated with PDF page and identifies the relationship between them.voidaddAssociatedFile(String description, PdfFileSpec fs) Adds file associated with PDF page and identifies the relationship between them.addOutputIntent(PdfOutputIntent outputIntent) AddsPdfOutputIntentthat shall specify the colour characteristics of output devices on which the page might be rendered.booleancontainsAnnotation(PdfAnnotation annotation) Checks if page contains the specified annotation.copyAsFormXObject(PdfDocument toDocument) Copies page as FormXObject to the specified document.copyTo(PdfDocument toDocument) Copies page to the specified document.copyTo(PdfDocument toDocument, IPdfPageExtraCopier copier) Copies page to the specified document.copyTo(PdfDocument toDocument, IPdfPageExtraCopier copier, boolean addPageToDocument, int pageInsertIndex) Copies page and adds it to the specified document to the end or by index if the corresponding parameter is true.voidflush()Flushes page dictionary, its content streams, annotations and thumb image.voidflush(boolean flushResourcesContentStreams) Flushes page dictionary, its content streams, annotations and thumb image.Gets array of annotation dictionaries that shall contain indirect references to all annotations associated with the page.intGets the number ofPdfAnnotationassociated with this page.Gets theRectangleobject specified by page's ArtBox, that define the extent of the page’s meaningful content (including potential white space) as intended by the page’s creator.getAssociatedFiles(boolean create) Returns files associated with PDF page.Gets theRectangleobject specified by page's BleedBox, that define the region to which the contents of the page shall be clipped when output in a production environment.byte[]Get decoded bytes for the whole page content.getContentStream(int index) Gets the content stream at specified 0-based index in the Contents objectPdfArray.intGets the size of Contents objectPdfArray.Gets theRectanglespecified by page's CropBox, that defines the visible region of default user space.Gets thePdfDocumentthat owns that page, ornullif such document isn't exist.GetsRectangleobject specified by page's Media Box, that defines the boundaries of the physical medium on which the page shall be displayed or printedintCalculates and returns the next available for this page's content stream MCID reference.getOutlines(boolean updateOutlines) This method gets outlines of a current pageGets page size, defined by media box object.Gets page size, considering page rotation.Get all pdf layers stored under this page's annotations/xobjects/resources.Gets thePdfResourceswrapper object for this page resources.intGets the number of degrees by which the page shall be rotated clockwise when displayed or printed.byte[]getStreamBytes(int index) Gets decoded bytes of a certain stream of a page content.intGets the key of the page’s entry in the structural parent tree.Gets a name specifying the tab order that shall be used for annotations on the page.Sets a stream object that shall define the page’s thumbnail image.Gets theRectangleobject specified by page's TrimBox object, that define the intended dimensions of the finished page after trimming.Gets the XMP Metadata object.booleanprotected booleanDefines if the object behind this wrapper must be an indirect object in the resultant document.Helper method that associates specified value with the specified key in the underlyingPdfDictionary.Helper method that removes the value associated with the specified key from the underlyingPdfDictionary.removeAnnotation(PdfAnnotation annotation) Removes an annotation from the page.removeAnnotation(PdfAnnotation annotation, boolean rememberTagPointer) Removes an annotation from the page.setAdditionalAction(PdfName key, PdfAction action) Helper method to add an additional action to this page.Sets the ArtBox object, that define the extent of the page’s meaningful content (including potential white space) as intended by the page’s creator.setBleedBox(Rectangle rectangle) Sets the BleedBox object, that defines the region to which the contents of the page shall be clipped when output in a production environment.setCropBox(Rectangle rectangle) Sets the CropBox object, that defines the visible region of default user space.setIgnorePageRotationForContent(boolean ignorePageRotationForContent) If true - defines that in case the page has a rotation, then new content will be automatically rotated in the opposite direction.setMediaBox(Rectangle rectangle) Sets the Media Box object, that defines the boundaries of the physical medium on which the page shall be displayed or printed.setPageLabel(PageLabelNumberingStyle numberingStyle, String labelPrefix) This method adds or replaces a page label.setPageLabel(PageLabelNumberingStyle numberingStyle, String labelPrefix, int firstPage) This method adds or replaces a page label.setResources(PdfResources pdfResources) SetsPdfResourcesobject.setRotation(int degAngle) Sets the page rotation.setTabOrder(PdfName tabOrder) Sets a name specifying the tab order that shall be used for annotations on the page.setThumbnailImage(PdfImageXObject thumb) Sets a stream object that shall define the page’s thumbnail image.setTrimBox(Rectangle rectangle) Sets the TrimBox object, that define the intended dimensions of the finished page after trimming.setXmpMetadata(byte[] xmpMetadata) Sets the XMP Metadata.setXmpMetadata(XMPMeta xmpMeta) Serializes XMP Metadata to byte array and sets it.setXmpMetadata(XMPMeta xmpMeta, SerializeOptions serializeOptions) Serializes XMP Metadata to byte array and sets it.Methods inherited from class com.itextpdf.kernel.pdf.PdfObjectWrapper
ensureObjectIsAddedToDocument, ensureUnderlyingObjectHasIndirectReference, getPdfObject, isFlushed, makeIndirect, makeIndirect, markObjectAsIndirect, setForbidRelease, setModified, setPdfObject, unsetForbidRelease
-
Constructor Details
-
PdfPage
-
PdfPage
-
PdfPage
-
-
Method Details
-
getPageSize
Gets page size, defined by media box object. This method doesn't take page rotation into account.- Returns:
-
Rectanglethat specify page size.
-
getPageSizeWithRotation
Gets page size, considering page rotation.- Returns:
-
Rectanglethat specify size of rotated page.
-
getRotation
public int getRotation()Gets the number of degrees by which the page shall be rotated clockwise when displayed or printed. Shall be a multiple of 90.- Returns:
-
intnumber of degrees. Default value: 0
-
setRotation
Sets the page rotation.- Parameters:
-
degAngle- theintnumber of degrees by which the page shall be rotated clockwise when displayed or printed. Shall be a multiple of 90. - Returns:
-
this
PdfPageinstance.
-
getContentStream
Gets the content stream at specified 0-based index in the Contents objectPdfArray. The situation when Contents object is aPdfStreamis treated like a one element array.- Parameters:
-
index- theintindex of returnedPdfStream. - Returns:
-
PdfStreamobject at specified index; will return null in case page dictionary doesn't adhere to the specification, meaning that the document is an invalid PDF. - Throws:
-
IndexOutOfBoundsException- if the index is out of range
-
getContentStreamCount
public int getContentStreamCount()Gets the size of Contents objectPdfArray. The situation when Contents object is aPdfStreamis treated like a one element array.- Returns:
-
the
intsize of Contents object, or 1 if Contents object is aPdfStream.
-
getFirstContentStream
- Returns:
-
first
PdfStreamin Contents object, ornullif Contents is empty.
-
getLastContentStream
- Returns:
-
first
PdfStreamin Contents object, ornullif Contents is empty.
-
newContentStreamBefore
Creates newPdfStreamobject and puts it at the beginning of Contents array (if Contents object isPdfStreamit will be replaced with one-element array).- Returns:
-
Created
PdfStreamobject.
-
newContentStreamAfter
Creates newPdfStreamobject and puts it at the end ofContentsarray (if Contents object isPdfStreamit will be replaced with one-element array).- Returns:
-
Created
PdfStreamobject.
-
getResources
Gets thePdfResourceswrapper object for this page resources. If page doesn't have resource object, then it will be inherited from page's parents. If neither parents nor page has the resource object, then the new one is created and added to page dictionary.
NOTE: If you'll try to modify the inherited resources, then the new resources object will be created, so you won't change the parent's resources. This new object under the wrapper will be added to page dictionary onflush(), or you can add it manually with this line, if needed:
getPdfObject().put(PdfName.Resources, getResources().getPdfObject());- Returns:
-
PdfResourceswrapper of the page.
-
setResources
SetsPdfResourcesobject.- Parameters:
-
pdfResources-PdfResourcesto set. - Returns:
-
this
PdfPageinstance.
-
setXmpMetadata
Sets the XMP Metadata.- Parameters:
-
xmpMetadata- thebyte[]of XMP Metadata to set. - Returns:
-
this
PdfPageinstance. - Throws:
-
IOException- in case of writing error.
-
setXmpMetadata
public PdfPage setXmpMetadata(XMPMeta xmpMeta, SerializeOptions serializeOptions) throws XMPException, IOException Serializes XMP Metadata to byte array and sets it.- Parameters:
-
xmpMeta- theXMPMetaobject to set. -
serializeOptions- theSerializeOptionsused while serialization. - Returns:
-
this
PdfPageinstance. - Throws:
-
XMPException- in case of XMP Metadata serialization error. -
IOException- in case of writing error.
-
setXmpMetadata
Serializes XMP Metadata to byte array and sets it. Uses padding equals to 2000.- Parameters:
-
xmpMeta- theXMPMetaobject to set. - Returns:
-
this
PdfPageinstance. - Throws:
-
XMPException- in case of XMP Metadata serialization error. -
IOException- in case of writing error.
-
getXmpMetadata
Gets the XMP Metadata object.- Returns:
-
PdfStreamobject, that represent XMP Metadata.
-
copyTo
Copies page to the specified document.
NOTE: Works only for pages from the document opened in reading mode, otherwise an exception is thrown.- Parameters:
-
toDocument- a document to copy page to. - Returns:
-
copied
PdfPage.
-
copyTo
Copies page to the specified document.
NOTE: Works only for pages from the document opened in reading mode, otherwise an exception is thrown.- Parameters:
-
toDocument- a document to copy page to. -
copier- a copier which bears a special copy logic. May be null. It is recommended to use the same instance ofIPdfPageExtraCopierfor the same output document. - Returns:
-
copied
PdfPage.
-
copyTo
public PdfPage copyTo(PdfDocument toDocument, IPdfPageExtraCopier copier, boolean addPageToDocument, int pageInsertIndex) Copies page and adds it to the specified document to the end or by index if the corresponding parameter is true.
NOTE: Works only for pages from the document opened in reading mode, otherwise an exception is thrown. NOTE: If both documents (from which and to which the copy is made) are tagged, you must additionally call theIPdfPageFormCopier.recreateAcroformToProcessCopiedFields(PdfDocument)method after copying the tag structure to process copied fields, like add them to the document and merge fields with the same names.- Parameters:
-
toDocument- a document to copy page to. -
copier- a copier which bears a special copy logic. May be null. It is recommended to use the same instance ofIPdfPageExtraCopierfor the same output document. -
addPageToDocument- true if page should be added to document. -
pageInsertIndex- position to add the page to, if -1 page will be added to the end of the document, will be ignored if addPageToDocument is false. - Returns:
-
copied
PdfPage.
-
getPdfLayers
Get all pdf layers stored under this page's annotations/xobjects/resources. Note that it will include all layers, even those already stored under /OCProperties entry in catalog. To get only unique layers, you can simply exclude ocgs, which already present in catalog.- Returns:
- set of pdf layers, associated with this page.
-
copyAsFormXObject
Copies page as FormXObject to the specified document.- Parameters:
-
toDocument- a document to copy to. - Returns:
-
copied
PdfFormXObjectobject. - Throws:
-
IOException- if an I/O error occurs.
-
getDocument
Gets thePdfDocumentthat owns that page, ornullif such document isn't exist.- Returns:
-
PdfDocumentthat owns that page, ornullif such document isn't exist.
-
flush
public void flush()Flushes page dictionary, its content streams, annotations and thumb image.If the page belongs to the document which is tagged, page flushing also triggers flushing of the tags, which are considered to belong to the page. The logic that defines if the given tag (structure element) belongs to the page is the following: if all the marked content references (dictionary or number references), that are the descendants of the given structure element, belong to the current page - the tag is considered to belong to the page. If tag has descendants from several pages - it is flushed, if all other pages except the current one are flushed.
- Overrides:
-
flushin classPdfObjectWrapper<PdfDictionary>
-
flush
public void flush(boolean flushResourcesContentStreams) Flushes page dictionary, its content streams, annotations and thumb image. IfflushResourcesContentStreamsis true, all content streams that are rendered on this page (like FormXObjects, annotation appearance streams, patterns) and also all images associated with this page will also be flushed.For notes about tag structure flushing see
PdfPage#flush() method.If
PdfADocumentis used, flushing will be applied only ifflushResourcesContentStreamsis true.Be careful with handling document in which some of the pages are flushed. Keep in mind that flushed objects are finalized and are completely written to the output stream. This frees their memory but makes it impossible to modify or read data from them. Whenever there is an attempt to modify or to fetch flushed object inner contents an exception will be thrown. Flushing is only possible for objects in the writing and stamping modes, also its possible to flush modified objects in append mode.
- Parameters:
-
flushResourcesContentStreams- if true all content streams that are rendered on this page (like form xObjects, annotation appearance streams, patterns) and also all images associated with this page will be flushed.
-
getMediaBox
GetsRectangleobject specified by page's Media Box, that defines the boundaries of the physical medium on which the page shall be displayed or printed- Returns:
-
Rectangleobject specified by page Media Box, expressed in default user space units. - Throws:
-
PdfException- in case of any error while reading MediaBox object.
-
setMediaBox
Sets the Media Box object, that defines the boundaries of the physical medium on which the page shall be displayed or printed. -
getCropBox
Gets theRectanglespecified by page's CropBox, that defines the visible region of default user space. When the page is displayed or printed, its contents shall be clipped (cropped) to this rectangle and then shall be imposed on the output medium in some implementation-defined manner.- Returns:
-
the
Rectangleobject specified by pages's CropBox, expressed in default user space units. MediaBox by default.
-
setCropBox
Sets the CropBox object, that defines the visible region of default user space. When the page is displayed or printed, its contents shall be clipped (cropped) to this rectangle and then shall be imposed on the output medium in some implementation-defined manner. -
setBleedBox
Sets the BleedBox object, that defines the region to which the contents of the page shall be clipped when output in a production environment. -
getBleedBox
Gets theRectangleobject specified by page's BleedBox, that define the region to which the contents of the page shall be clipped when output in a production environment.- Returns:
-
the
Rectangleobject specified by page's BleedBox, expressed in default user space units. CropBox by default.
-
setArtBox
Sets the ArtBox object, that define the extent of the page’s meaningful content (including potential white space) as intended by the page’s creator. -
getArtBox
Gets theRectangleobject specified by page's ArtBox, that define the extent of the page’s meaningful content (including potential white space) as intended by the page’s creator.- Returns:
-
the
Rectangleobject specified by page's ArtBox, expressed in default user space units. CropBox by default.
-
setTrimBox
Sets the TrimBox object, that define the intended dimensions of the finished page after trimming. -
getTrimBox
Gets theRectangleobject specified by page's TrimBox object, that define the intended dimensions of the finished page after trimming.- Returns:
-
the
Rectangleobject specified by page's TrimBox, expressed in default user space units. CropBox by default.
-
getContentBytes
public byte[] getContentBytes()Get decoded bytes for the whole page content.- Returns:
- byte array.
- Throws:
-
PdfException- in case of anyIOException.
-
getStreamBytes
public byte[] getStreamBytes(int index) Gets decoded bytes of a certain stream of a page content.- Parameters:
-
index- index of stream inside Content. - Returns:
- byte array.
- Throws:
-
PdfException- in case of anyIOException.
-
getNextMcid
public int getNextMcid()Calculates and returns the next available for this page's content stream MCID reference.- Returns:
- calculated MCID reference.
- Throws:
-
PdfException- in case of not tagged document.
-
getStructParentIndex
public int getStructParentIndex()Gets the key of the page’s entry in the structural parent tree.- Returns:
- the key of the page’s entry in the structural parent tree. If page has no entry in the structural parent tree, returned value is -1.
-
setAdditionalAction
Helper method to add an additional action to this page. May be used in chain. -
getAnnotations
Gets array of annotation dictionaries that shall contain indirect references to all annotations associated with the page.- Returns:
-
the
List<PdfAnnotation> containing all page's annotations.
-
containsAnnotation
Checks if page contains the specified annotation.- Parameters:
-
annotation- thePdfAnnotationto check. - Returns:
-
trueif page contains specified annotation andfalseotherwise.
-
addAnnotation
Adds specified annotation to the end of annotations array and tagged it. May be used in chain.- Parameters:
-
annotation- thePdfAnnotationto add. - Returns:
-
this
PdfPageinstance.
-
addAnnotation
Adds specifiedPdfAnnotationto specified index in annotations array with or without autotagging. May be used in chain.- Parameters:
-
index- the index at which specified annotation will be added. If-1then annotation will be added to the end of array. -
annotation- thePdfAnnotationto add. -
tagAnnotation- iftruethe added annotation will be autotagged. - Returns:
-
this
PdfPageinstance.
-
removeAnnotation
Removes an annotation from the page.When document is tagged a corresponding logical structure content item for this annotation will be removed; its immediate structure element parent will be removed as well if the following conditions are met: annotation content item was its single child and structure element role is either Annot or Form.
- Parameters:
-
annotation- an annotation to be removed - Returns:
-
this
PdfPageinstance.
-
removeAnnotation
Removes an annotation from the page.When document is tagged a corresponding logical structure content item for this annotation will be removed; its immediate structure element parent will be removed as well if the following conditions are met: annotation content item was its single child and structure element role is either Annot or Form.
- Parameters:
-
annotation- an annotation to be removed -
rememberTagPointer- if set to true, theTagStructureContext.getAutoTaggingPointer()instance ofTagTreePointerwill be moved to the parent of the removed annotation tag. Can be used to add a new annotation to the same place in the tag structure. (E.g. when merged Acroform field is split into a field and a pure widget, the page annotation needs to be replaced by the new one) - Returns:
-
this
PdfPageinstance.
-
getAnnotsSize
public int getAnnotsSize()Gets the number ofPdfAnnotationassociated with this page.- Returns:
-
the
intnumber ofPdfAnnotationassociated with this page.
-
getOutlines
This method gets outlines of a current page- Parameters:
-
updateOutlines- if the flag istrue, the method reads the whole document and creates outline tree. If the flag isfalse, the method gets cached outline tree (if it was cached via calling getOutlines method before). - Returns:
- return all outlines of a current page
-
isIgnorePageRotationForContent
public boolean isIgnorePageRotationForContent()- Returns:
- true - if in case the page has a rotation, then new content will be automatically rotated in the opposite direction. On the rotated page this would look like if new content ignores page rotation.
-
setIgnorePageRotationForContent
If true - defines that in case the page has a rotation, then new content will be automatically rotated in the opposite direction. On the rotated page this would look like if new content ignores page rotation. Default value -false.- Parameters:
-
ignorePageRotationForContent- - true to ignore rotation of the new content on the rotated page. - Returns:
-
this
PdfPageinstance.
-
setPageLabel
This method adds or replaces a page label.- Parameters:
-
numberingStyle- The numbering style that shall be used for the numeric portion of each page label. May be NULL -
labelPrefix- The label prefix for page labels in this range. May be NULL - Returns:
-
this
PdfPageinstance.
-
setPageLabel
public PdfPage setPageLabel(PageLabelNumberingStyle numberingStyle, String labelPrefix, int firstPage) This method adds or replaces a page label.- Parameters:
-
numberingStyle- The numbering style that shall be used for the numeric portion of each page label. May be NULL -
labelPrefix- The label prefix for page labels in this range. May be NULL -
firstPage- The value of the numeric portion for the first page label in the range. Must be greater or equal 1. - Returns:
-
this
PdfPageinstance.
-
setTabOrder
Sets a name specifying the tab order that shall be used for annotations on the page. The possible values arePdfName.R(row order),PdfName.C(column order), andPdfName.S(structure order). Beginning with PDF 2.0, the possible values also includePdfName.A(annotations array order) andPdfName.W(widget order). See ISO 32000 12.5, "Annotations" for details. -
getTabOrder
Gets a name specifying the tab order that shall be used for annotations on the page. The possible values arePdfName.R(row order),PdfName.C(column order), andPdfName.S(structure order). Beginning with PDF 2.0, the possible values also includePdfName.A(annotations array order) andPdfName.W(widget order). See ISO 32000 12.5, "Annotations" for details.- Returns:
-
a
PdfNamespecifying the annotations tab order or null if tab order is not defined.
-
setThumbnailImage
Sets a stream object that shall define the page’s thumbnail image. Thumbnail images represent the contents of its pages in miniature form- Parameters:
-
thumb- the thumbnail image - Returns:
-
this
PdfPageobject
-
getThumbnailImage
Sets a stream object that shall define the page’s thumbnail image. Thumbnail images represent the contents of its pages in miniature form- Returns:
-
the thumbnail image, or
nullif it is not present
-
addOutputIntent
AddsPdfOutputIntentthat shall specify the colour characteristics of output devices on which the page might be rendered.- Parameters:
-
outputIntent-PdfOutputIntentto add. - Returns:
-
this
PdfPageobject - See Also:
-
put
Helper method that associates specified value with the specified key in the underlyingPdfDictionary. Can be used in method chaining. -
remove
Helper method that removes the value associated with the specified key from the underlyingPdfDictionary. Can be used in method chaining. -
addAssociatedFile
Adds file associated with PDF page and identifies the relationship between them.Associated files may be used in Pdf/A-3 and Pdf 2.0 documents. The method adds file to array value of the AF key in the page dictionary. If description is provided, it also will add file description to catalog Names tree.
For associated files their associated file specification dictionaries shall include the AFRelationship key
- Parameters:
-
description- the file description -
fs- file specification dictionary of associated file
-
addAssociatedFile
Adds file associated with PDF page and identifies the relationship between them.
Associated files may be used in Pdf/A-3 and Pdf 2.0 documents. The method adds file to array value of the AF key in the page dictionary.
For associated files their associated file specification dictionaries shall include the AFRelationship key
- Parameters:
-
fs- file specification dictionary of associated file
-
getAssociatedFiles
Returns files associated with PDF page.- Parameters:
-
create- defines whether AF arrays will be created if it doesn't exist - Returns:
- associated files array
-
isWrappedObjectMustBeIndirect
protected boolean isWrappedObjectMustBeIndirect()Description copied from class:PdfObjectWrapperDefines if the object behind this wrapper must be an indirect object in the resultant document.
If this method returns true it doesn't necessarily mean that object must be in the indirect state at any moment, but rather defines that when the object will be written to the document it will be transformed into indirect object if it's not indirect yet.
Return value of this method shouldn't depend on any logic, it should return always true or false.- Specified by:
-
isWrappedObjectMustBeIndirectin classPdfObjectWrapper<PdfDictionary> - Returns:
- true if in the resultant document the object behind the wrapper must be indirect, otherwise false.
-