Skip to main content

Page Class

A single PDF page.

Declaration

public class Page extends JavaObject implements IPropertyChangeNotifier, ICloneable

Remarks

The PdfDocument class stores document pages in a IPageCollection. Call the PdfDocument.getPages() method to access this collection.

// Get document pages.
var pages = pdfDocument.getPages();

// Iterate through all pages in the document.
for(Page page : pages) {

    // Process the current page.
}

Supported page operations:

A Page object stores its content as a collection of fragments (text blocks, images, shapes, or forms).

Create a fragment and add it to the page’s fragments collection. Use the Page.getFragments() method to access the fragments collection:

page.getFragments().add(fragment);

Use the following methods to add specific fragment types:

Method Description
addTextFragment() Adds a text fragment.
addImageFragment() Adds an image fragment.
addFragment() Adds any page fragment, including PathFragment and FormFragment.

Implements

com.devexpress.system.ICloneable

Inherited Members

com.devexpress.system.JavaObject.clone()
com.devexpress.system.JavaObject.equals(java.lang.Object)
com.devexpress.system.JavaObject.hashCode()
com.devexpress.system.JavaObject.initFields()
com.devexpress.system.JavaObject.memberwiseClone()
com.devexpress.system.JavaObject.toString()
java.lang.Object.finalize()
java.lang.Object.getClass()
java.lang.Object.notify()
java.lang.Object.notifyAll()
java.lang.Object.wait()
java.lang.Object.wait(long)
java.lang.Object.wait(long,int)

Inheritance

Object
com.devexpress.system.JavaObject
Page

Constructors

Page() Constructor

Initializes a new instance of the Page class with the default A4 page size.

Declaration

public Page()

Page(double width, double height) Constructor

Initializes a new instance of the Page class with the specified width and height.

Declaration

public Page(double width, double height)

Parameters

Name Type Description
width double

The page width.

height double

The page height.

Page(DXPaperKind kind) Constructor

Initializes a new instance of the Page class with the specified page size.

Declaration

public Page(DXPaperKind kind)

Parameters

Name Type Description
kind com.devexpress.drawing.printing.DXPaperKind

The paper kind used to initialize the page size.

Methods

addFragment(PageFragment fragment) Method

Adds a fragment to the page.

Declaration

public void addFragment(PageFragment fragment)

Parameters

Name Type Description
fragment PageFragment

The fragment to add to the page.

Remarks

The addFragment method adds any page fragment, including PathFragment and FormFragment.

The following code snippet adds a text fragment with custom formatting:

import com.devexpress.docs.pdf.*;
import com.devexpress.drawing.*;
import com.devexpress.system.drawing.*;
import com.devexpress.drawing.printing.*;

import java.nio.channels.*;
import java.nio.file.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Create a new PDF document.
        try (PdfDocument pdfDocument = new PdfDocument()) {

            // Add a Letter-size page to the document.
            Page page = pdfDocument.getPages().add(DXPaperKind.LETTER);

            TextFragment text = new TextFragment();
            text.setFont(new TextFont("Courier New"));
            text.setLocation(new PointF(10, 740));
            text.setText("The PDF Document API");
            text.setFontSize(18);

            page.addFragment(text);

            // Save the document to a PDF file.
            try (WritableByteChannel writableByteChannel =
                    FileChannel.open(Path.of("result.pdf"),
                        StandardOpenOption.CREATE,
                        StandardOpenOption.WRITE,
                        StandardOpenOption.TRUNCATE_EXISTING)) {

                pdfDocument.save(writableByteChannel);
            }
        }
    }
}

The following code snippet adds a rectangle shape to a PDF page:

PathFragment rectangle = PathFragment.rectangle(new RectangleF(100, 100, 200, 200));
rectangle.setOutline(Outline.create(Fill.createSolid(PdfColor.getDarkOrange()), 5));
rectangle.setFill(Fill.createSolid(PdfColor.getOrange()));

page.addFragment(rectangle);

Refer to the following help topic for additional information: Add Content to Pages.

addImageFragment(DXImage image, float x, float y) Method

Adds an image fragment to the page at the specified coordinates.

Declaration

public ImageFragment addImageFragment(DXImage image, float x, float y)

Parameters

Name Type Description
image DXImage

The image to add.

x float

The X-coordinate of the fragment origin.

y float

The Y-coordinate of the fragment origin.

Returns

Type Description
ImageFragment

The image fragment added to the page.

Remarks

The following code snippet creates a PDF document, loads an image from a file, places it on a Letter-size page at a specified position, and saves the result to a PDF file:

import com.devexpress.docs.pdf.*;
import com.devexpress.drawing.*;
import com.devexpress.drawing.printing.*;

import java.io.ByteArrayInputStream;
import java.nio.channels.*;
import java.nio.file.*;

public class Main {
    public static void main(String[] args) throws Exception {
        byte[] imageData = Files.readAllBytes(Path.of("devexpress-logo.png"));

        try (PdfDocument pdfDocument = new PdfDocument();
            DXImage image = DXImage.fromStream(new ByteArrayInputStream(imageData))) {

            // Add a Letter-size page to the document.
            Page page = pdfDocument.getPages().add(DXPaperKind.LETTER);

            page.addImageFragment(image, 100, 500);

            // Save the document to a PDF file.
            try (WritableByteChannel writableByteChannel =
                     FileChannel.open(Path.of("result.pdf"),
                         StandardOpenOption.CREATE,
                         StandardOpenOption.WRITE,
                         StandardOpenOption.TRUNCATE_EXISTING)) {

                pdfDocument.save(writableByteChannel);
            }
        }
    }
}

Refer to the following help topic for additional information: Add Images to PDF Documents.

addTextFragment(String text, float x, float y) Method

Adds a text fragment to the page at the specified coordinates.

Declaration

public TextFragment addTextFragment(String text, float x, float y)

Parameters

Name Type Description
text String

The text to add.

x float

The X-coordinate of the fragment origin.

y float

The Y-coordinate of the fragment origin.

Returns

Type Description
TextFragment

The text fragment added to the page.

Remarks

Use the addTextFragment method to add a single line of text with default formatting to a PDF page:

page.addTextFragment("The PDF Document API", 10, 740);

Refer to the following help topic for additional information: Add Text to PDF Documents.

clone(DocumentCloneContext context) Method

Clones the page in the specified clone context.

Declaration

public Page clone(DocumentCloneContext context)

Parameters

Name Type Description
context DocumentCloneContext

The context that controls the clone operation.

Returns

Type Description
Page

The cloned page.

deepClone() Method

Creates a deep copy of the page.

Declaration

public Page deepClone()

Returns

Type Description
Page

A deep copy of the page.

getAnnotations() Method

Returns the page annotation collection.

Declaration

public IAnnotationCollection getAnnotations()

Returns

Type Description
IAnnotationCollection

The annotation collection for the page.

Remarks

The following code snippet creates a text field:

import com.devexpress.docs.pdf.*;
import com.devexpress.system.drawing.*;
import com.devexpress.drawing.printing.DXPaperKind;

import java.nio.channels.*;
import java.nio.file.*;

public class Main {
    public static void main(String[] args) throws Exception {
        try (PdfDocument pdfDocument = new PdfDocument()) {
            // Add an A4 page to the document.
            Page page = pdfDocument.getPages().add(DXPaperKind.A4);

            // Create a text field.
            TextBoxField loginField = new TextBoxField("Login");

            // Assign an initial value to the field before it is displayed.
            loginField.setValue("John Doe");

            pdfDocument.getFields().add(loginField);

            // Bind the field to a widget and place it on the page.
            RectangleF bounds = new RectangleF(120, 720, 220, 20);
            TextBoxWidgetAnnotation widget = new TextBoxWidgetAnnotation(loginField, bounds);

            widget.setFontSize(12);

            page.getAnnotations().add(widget);

            // Save the document.
            try (WritableByteChannel channel =
                     FileChannel.open(Path.of("result.pdf"),
                         StandardOpenOption.CREATE,
                         StandardOpenOption.WRITE,
                         StandardOpenOption.TRUNCATE_EXISTING)) {

                pdfDocument.save(channel);
            }
        }
    }
}

Refer to the following help topic for additional information: Work with PDF Forms (AcroForms).

getArtBox() Method

Returns the page art box (the meaningful content area).

Declaration

public RectangleF getArtBox()

Returns

Type Description
RectangleF

The art box rectangle.

getBleedBox() Method

Returns the page bleed box (the area to which content extends before trimming).

Declaration

public RectangleF getBleedBox()

Returns

Type Description
RectangleF

The bleed box rectangle.

getCropBox() Method

Returns the page crop box (the visible area of the page).

Declaration

public RectangleF getCropBox()

Returns

Type Description
RectangleF

The crop box rectangle.

getFragments() Method

Returns the page fragment collection.

Declaration

public IFragmentCollection getFragments()

Returns

Type Description
IFragmentCollection

The fragment collection for the page.

getHeight() Method

Returns the page height.

Declaration

public double getHeight()

Returns

Type Description
double

The page height.

getMediaBox() Method

Returns the page media box (the physical size of the page).

Declaration

public RectangleF getMediaBox()

Returns

Type Description
RectangleF

The media box rectangle.

getRotation() Method

Returns the page rotation angle.

Declaration

public PageRotationAngle getRotation()

Returns

Type Description
PageRotationAngle

The page rotation angle.

getTrimBox() Method

Returns the page trim box.

Declaration

public RectangleF getTrimBox()

Returns

Type Description
RectangleF

The trim box rectangle.

getWidth() Method

Returns the page width.

Declaration

public double getWidth()

Returns

Type Description
double

The page width.

notifyChange(Object sender, String propertyName) Method

Notifies listeners that a page property has changed.

Declaration

public void notifyChange(Object sender, String propertyName)

Parameters

Name Type Description
sender Object

The object that raised the change notification.

propertyName String

The name of the changed property.

objectClone() Method

Clones the page as an object.

Declaration

public Object objectClone()

Returns

Type Description
Object

A cloned page object.

offsetContent(double dx, double dy) Method

Offsets page content by the specified distances.

Declaration

public void offsetContent(double dx, double dy)

Parameters

Name Type Description
dx double

The horizontal offset.

dy double

The vertical offset.

Remarks

The following code snippet moves page content by an X offset of 50 and a Y offset of 100:

Page page = pdfDocument.getPages().getFirst();

// Move page content by X and Y offsets.
page.offsetContent(50, 100);

Refer to the following help topic for additional information: Offset Page Content.

resize(RectangleF mediaBox, PageContentHorizontalAlignment horizontalAlignment, PageContentVerticalAlignment verticalAlignment) Method

Resizes the page and aligns its content within the new media box.

Declaration

public void resize(RectangleF mediaBox, PageContentHorizontalAlignment horizontalAlignment, PageContentVerticalAlignment verticalAlignment)

Parameters

Name Type Description
mediaBox RectangleF

The new media box rectangle. Page size is measured in points (1 inch = 72 points).

horizontalAlignment PageContentHorizontalAlignment

The horizontal alignment for existing page content.

verticalAlignment PageContentVerticalAlignment

The vertical alignment for existing page content.

Remarks

The following code snippet resizes all pages in a PDF document to A4 size, centers the page content, and saves the result to a new PDF file:

import com.devexpress.docs.pdf.*;
import com.devexpress.drawing.printing.*;
import com.devexpress.system.drawing.*;

import java.nio.channels.*;
import java.nio.file.*;

public class Main {
    public static void main(String[] args) throws Exception {
        try (PdfDocument pdfDocument = new PdfDocument(FileChannel.open(
                Path.of("document.pdf")))) {

            try {
                // Resize all pages to A4 and center their content.
                for(Page page : pdfDocument.getPages()) {
                    page.resize(
                        new RectangleF(0, 0, 595.28f, 841.89f),
                        PageContentHorizontalAlignment.CENTER,
                        PageContentVerticalAlignment.CENTER
                    );
                }
            } catch (Exception e) {
                System.out.println("The page resize operation failed.\n" +
                    "The target page size must be compatible with the existing page layout.");
            }
            // Save the document to a PDF file.
            try (WritableByteChannel writableByteChannel =
                    FileChannel.open(Path.of("result.pdf"),
                        StandardOpenOption.CREATE,
                        StandardOpenOption.WRITE,
                        StandardOpenOption.TRUNCATE_EXISTING)) {

                pdfDocument.save(writableByteChannel);
            }
        }
    }
}

Refer to the following help topic for additional information: Resize PDF Pages.

rotateContent(double x, double y, double degree) Method

Rotates page content around the specified point.

Declaration

public void rotateContent(double x, double y, double degree)

Parameters

Name Type Description
x double

The X-coordinate of the rotation center.

y double

The Y-coordinate of the rotation center.

degree double

The rotation angle, in degrees.

Remarks

The following code snippet rotates page content by 270 degrees around the specified point:

import com.devexpress.docs.pdf.*;
import com.devexpress.system.drawing.*;

import java.nio.channels.*;
import java.nio.file.*;

public class Main {
    public static void main(String[] args) throws Exception {
        try (PdfDocument pdfDocument = new PdfDocument(FileChannel.open(
                Path.of("document.pdf")))) {

            Page page = pdfDocument.getPages().getFirst();

            // Rotate page content around the specified point.
            page.rotateContent(300, 300, 270);

            try (WritableByteChannel writableByteChannel =
                 FileChannel.open(Path.of("result.pdf"),
                     StandardOpenOption.CREATE,
                     StandardOpenOption.WRITE,
                     StandardOpenOption.TRUNCATE_EXISTING)) {

                pdfDocument.save(writableByteChannel);
            }
        }
    }
}

Refer to the following help topic for additional information: Rotate Page Content.

scaleContent(double scaleX, double scaleY) Method

Scales page content by the specified factors.

Declaration

public void scaleContent(double scaleX, double scaleY)

Parameters

Name Type Description
scaleX double

The horizontal scale factor.

scaleY double

The vertical scale factor.

Remarks

The following code snippet scales page content to 50% of its original size:

Page page = pdfDocument.getPages().getFirst();

// Scale page content by 50% on both axes.
page.scaleContent(0.5, 0.5);

Refer to the following help topic for additional information: Scale Page Content.

setArtBox(RectangleF value) Method

Sets the page art box (the meaningful content area).

Declaration

public void setArtBox(RectangleF value)

Parameters

Name Type Description
value RectangleF

The art box rectangle.

setBleedBox(RectangleF value) Method

Sets the page bleed box (the area to which content extends before trimming).

Declaration

public void setBleedBox(RectangleF value)

Parameters

Name Type Description
value RectangleF

The bleed box rectangle.

setCropBox(RectangleF value) Method

Sets the page crop box (the visible area of the page).

Declaration

public void setCropBox(RectangleF value)

Parameters

Name Type Description
value RectangleF

The crop box rectangle.

setMediaBox(RectangleF value) Method

Sets the page media box (the physical size of the page).

Declaration

public void setMediaBox(RectangleF value)

Parameters

Name Type Description
value RectangleF

The media box rectangle.

setRotation(PageRotationAngle value) Method

Sets the page rotation angle.

Declaration

public void setRotation(PageRotationAngle value)

Parameters

Name Type Description
value PageRotationAngle

The page rotation angle.

setTrimBox(RectangleF value) Method

Sets the page trim box.

Declaration

public void setTrimBox(RectangleF value)

Parameters

Name Type Description
value RectangleF

The trim box rectangle.