The PageObject Class
- class PyPDF2._page.PageObject(pdf=None, indirectRef=None)[source]
Bases:
PyPDF2.generic.DictionaryObject
PageObject represents a single page within a PDF file.
Typically this object will be created by accessing the
get_page()
method of thePdfReader
class, but it is also possible to create an empty page with thecreateBlankPage()
static method.- Parameters
pdf – PDF file the page belongs to.
indirectRef – Stores the original indirect reference to this object in its source PDF
- addTransformation(ctm)[source]
Deprecated since version 1.28.0: Use
add_transformation()
instead.
- add_transformation(ctm)[source]
Apply a transformation matrix to the page.
- Parameters
ctm (tuple) – A 6-element tuple containing the operands of the transformation matrix. Alternatively, a
Transformation
object can be passed.
- property artbox
A
RectangleObject
, expressed in default user space units, defining the extent of the page’s meaningful content as intended by the page’s creator.
- property bleedbox
A
RectangleObject
, expressed in default user space units, defining the region to which the contents of the page should be clipped when output in a production enviroment.
- compressContentStreams()[source]
Deprecated since version 1.28.0: Use
compress_content_streams()
instead.
- compress_content_streams()[source]
Compress the size of this page by joining all content streams and applying a FlateDecode filter.
However, it is possible that this function will perform no action if content stream compression becomes “automatic” for some reason.
- static createBlankPage(pdf=None, width=None, height=None)[source]
Deprecated since version 1.28.0: Use
create_blank_page()
instead.
- static create_blank_page(pdf=None, width=None, height=None)[source]
Return a new blank page.
If
width
orheight
isNone
, try to get the page size from the last page of pdf.- Parameters
pdf – PDF file the page belongs to
width (float) – The width of the new page expressed in default user space units.
height (float) – The height of the new page expressed in default user space units.
- Returns
the new blank page:
- Return type
- Raises
PageSizeNotDefinedError – if
pdf
isNone
or contains no page
- property cropbox
A
RectangleObject
, expressed in default user space units, defining the visible region of default user space. When the page is displayed or printed, its contents are to be clipped (cropped) to this rectangle and then imposed on the output medium in some implementation-defined manner. Default value: same asmediabox
.
- extractText(Tj_sep='', TJ_sep='')[source]
Deprecated since version 1.28.0: Use
extract_text()
instead.
- extract_text(Tj_sep='', TJ_sep='')[source]
Locate all text drawing commands, in the order they are provided in the content stream, and extract the text. This works well for some PDF files, but poorly for others, depending on the generator used. This will be refined in the future. Do not rely on the order of text coming out of this function, as it will change if this function is made more sophisticated.
- Returns
a unicode string object.
- getContents()[source]
Deprecated since version 1.28.0: Use
get_contents()
instead.
- get_contents()[source]
Access the page contents.
- Returns
the
/Contents
object, orNone
if it doesn’t exist./Contents
is optional, as described in PDF Reference 7.7.3.3
- property mediabox
A
RectangleObject
, expressed in default user space units, defining the boundaries of the physical medium on which the page is intended to be displayed or printed.
- mergePage(page2)[source]
Deprecated since version 1.28.0: Use
merge_page()
instead.
- mergeRotatedPage(page2, rotation, expand=False)[source]
mergeRotatedPage is similar to mergePage, but the stream to be merged is rotated by appling a transformation matrix.
- Parameters
page2 (PageObject) – the page to be merged into this one. Should be an instance of
PageObject
.rotation (float) – The angle of the rotation, in degrees
expand (bool) – Whether the page should be expanded to fit the dimensions of the page to be merged.
Deprecated since version 1.28.0: Use
add_transformation()
andmerge_page()
instead.
- mergeRotatedScaledPage(page2, rotation, scale, expand=False)[source]
mergeRotatedScaledPage is similar to mergePage, but the stream to be merged is rotated and scaled by appling a transformation matrix.
- Parameters
page2 (PageObject) – the page to be merged into this one. Should be an instance of
PageObject
.rotation (float) – The angle of the rotation, in degrees
scale (float) – The scaling factor
expand (bool) – Whether the page should be expanded to fit the dimensions of the page to be merged.
Deprecated since version 1.28.0: Use
add_transformation()
andmerge_page()
instead.
- mergeRotatedScaledTranslatedPage(page2, rotation, scale, tx, ty, expand=False)[source]
mergeRotatedScaledTranslatedPage is similar to mergePage, but the stream to be merged is translated, rotated and scaled by appling a transformation matrix.
- Parameters
page2 (PageObject) – the page to be merged into this one. Should be an instance of
PageObject
.tx (float) – The translation on X axis
ty (float) – The translation on Y axis
rotation (float) – The angle of the rotation, in degrees
scale (float) – The scaling factor
expand (bool) – Whether the page should be expanded to fit the dimensions of the page to be merged.
Deprecated since version 1.28.0: Use
add_transformation()
andmerge_page()
instead.
- mergeRotatedTranslatedPage(page2, rotation, tx, ty, expand=False)[source]
mergeRotatedTranslatedPage is similar to mergePage, but the stream to be merged is rotated and translated by appling a transformation matrix.
- Parameters
page2 (PageObject) – the page to be merged into this one. Should be an instance of
PageObject
.tx (float) – The translation on X axis
ty (float) – The translation on Y axis
rotation (float) – The angle of the rotation, in degrees
expand (bool) – Whether the page should be expanded to fit the dimensions of the page to be merged.
Deprecated since version 1.28.0: Use
add_transformation()
andmerge_page()
instead.
- mergeScaledPage(page2, scale, expand=False)[source]
mergeScaledPage is similar to mergePage, but the stream to be merged is scaled by appling a transformation matrix.
- Parameters
page2 (PageObject) – The page to be merged into this one. Should be an instance of
PageObject
.scale (float) – The scaling factor
expand (bool) – Whether the page should be expanded to fit the dimensions of the page to be merged.
Deprecated since version 1.28.0: Use
add_transformation()
andmerge_page()
instead.
- mergeScaledTranslatedPage(page2, scale, tx, ty, expand=False)[source]
mergeScaledTranslatedPage is similar to mergePage, but the stream to be merged is translated and scaled by appling a transformation matrix.
- Parameters
page2 (PageObject) – the page to be merged into this one. Should be an instance of
PageObject
.scale (float) – The scaling factor
tx (float) – The translation on X axis
ty (float) – The translation on Y axis
expand (bool) – Whether the page should be expanded to fit the dimensions of the page to be merged.
Deprecated since version 1.28.0: Use
add_transformation()
andmerge_page()
instead.
- mergeTransformedPage(page2, ctm, expand=False)[source]
mergeTransformedPage is similar to mergePage, but a transformation matrix is applied to the merged stream.
- Parameters
page2 (PageObject) – The page to be merged into this one. Should be an instance of
PageObject
.ctm (tuple) – a 6-element tuple containing the operands of the transformation matrix
expand (bool) – Whether the page should be expanded to fit the dimensions of the page to be merged.
Deprecated since version 1.28.0: Use
add_transformation()
andmerge_page()
instead.
- mergeTranslatedPage(page2, tx, ty, expand=False)[source]
mergeTranslatedPage is similar to mergePage, but the stream to be merged is translated by appling a transformation matrix.
- Parameters
page2 (PageObject) – the page to be merged into this one. Should be an instance of
PageObject
.tx (float) – The translation on X axis
ty (float) – The translation on Y axis
expand (bool) – Whether the page should be expanded to fit the dimensions of the page to be merged.
Deprecated since version 1.28.0: Use
add_transformation()
andmerge_page()
instead.
- merge_page(page2, expand=False)[source]
Merge the content streams of two pages into one.
Resource references (i.e. fonts) are maintained from both pages. The mediabox/cropbox/etc of this page are not altered. The parameter page’s content stream will be added to the end of this page’s content stream, meaning that it will be drawn after, or “on top” of this page.
- Parameters
page2 (PageObject) – The page to be merged into this one. Should be an instance of
PageObject
.expand (bool) – If true, the current page dimensions will be expanded to accommodate the dimensions of the page to be merged.
- rotateClockwise(angle)[source]
Deprecated since version 1.28.0: Use
rotate_clockwise()
instead.
- rotateCounterClockwise(angle)[source]
Deprecated since version 1.28.0: Use
rotate_clockwise()
with a negative argument instead.
- rotate_clockwise(angle)[source]
Rotate a page clockwise by increments of 90 degrees.
- Parameters
angle (int) – Angle to rotate the page. Must be an increment of 90 deg.
- scale(sx, sy)[source]
Scale a page by the given factors by appling a transformation matrix to its content and updating the page size.
- Parameters
sx (float) – The scaling factor on horizontal axis.
sy (float) – The scaling factor on vertical axis.
- scaleBy(factor)[source]
Deprecated since version 1.28.0: Use
scale_by()
instead.
- scaleTo(width, height)[source]
Deprecated since version 1.28.0: Use
scale_to()
instead.
- scale_by(factor)[source]
Scale a page by the given factor by appling a transformation matrix to its content and updating the page size.
- Parameters
factor (float) – The scaling factor (for both X and Y axis).
- scale_to(width, height)[source]
Scale a page to the specified dimentions by appling a transformation matrix to its content and updating the page size.
- Parameters
width (float) – The new width.
height (float) – The new heigth.
- property trimbox
A
RectangleObject
, expressed in default user space units, defining the intended dimensions of the finished page after trimming.