The PageObject Class

class PyPDF2.pdf.PageObject(pdf=None, indirectRef=None)

This class represents a single page within a PDF file. Typically this object will be created by accessing the getPage() method of the PdfFileReader class, but it is also possible to create an empty page with the createBlankPage() 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)

Applies a transformation matrix to the page.

Parameters:ctm (tuple) – A 6-element tuple containing the operands of the transformation matrix.
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.

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()

Compresses 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)

Returns a new blank page. If width or height is None, 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:

PageObject

Raises PageSizeNotDefinedError:
 

if pdf is None or contains no page

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 as mediaBox.

extractText()

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()

Accesses the page contents.

Returns:the /Contents object, or None if it doesn’t exist. /Contents is optional, as described in PDF Reference 7.7.3.3
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)

Merges 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.
mergeRotatedPage(page2, rotation, expand=False)

This 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.
mergeRotatedScaledPage(page2, rotation, scale, expand=False)

This 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.
mergeRotatedScaledTranslatedPage(page2, rotation, scale, tx, ty, expand=False)

This 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.
mergeRotatedTranslatedPage(page2, rotation, tx, ty, expand=False)

This 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.
mergeScaledPage(page2, scale, expand=False)

This 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.
mergeScaledTranslatedPage(page2, scale, tx, ty, expand=False)

This 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.
mergeTransformedPage(page2, ctm, expand=False)

This 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.
mergeTranslatedPage(page2, tx, ty, expand=False)

This 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.
rotateClockwise(angle)

Rotates a page clockwise by increments of 90 degrees.

Parameters:angle (int) – Angle to rotate the page. Must be an increment of 90 deg.
rotateCounterClockwise(angle)

Rotates a page counter-clockwise by increments of 90 degrees.

Parameters:angle (int) – Angle to rotate the page. Must be an increment of 90 deg.
scale(sx, sy)

Scales 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)

Scales 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).
scaleTo(width, height)

Scales 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.
trimBox

A RectangleObject, expressed in default user space units, defining the intended dimensions of the finished page after trimming.