Some test text!

menu
search

Rotating PDF pages

PDFTron's library has a rotate pages dialog which allows users to rotate pages easily. For more information, see: rotate pages dialog.

There are two options for rotating PDF pages. The first is rotating PDFViewCtrl, it will rotate all PDF pages temporarily. The second is rotating PDF pages using Page class, it will rotate selected PDF pages permanently.

linkRotating PDFViewCtrl

If you want to rotate a PDFViewCtrl by 90 degrees clockwisely, just simply call PDFViewCtrl.rotateClockwise (). If you want to rotate the PDFViewCtrl by 90 degrees counter clockwisely, call PDFViewCtrl.rotateCounterClockwise (). After rotation, update the PDFViewCtrl pages layout by calling PDFViewCtrl.updatePageLayout().

pdfViewCtrl.rotateClockwise();
try {
    pdfViewCtrl.updatePageLayout();
} catch (Exception e) {
    e.printStackTrace();  
}

where pdfViewCtrl is the instance of PDFViewCtrl.

linkRotating a PDF page

To rotate a PDF page by 90 degrees, call Page.setRotation(int). The parameter of this method is the rotation value, which is one of [0, 1, 2, 3]. It represents [0 degree, 90 degree, 180 degree, 270 degree] correspondingly. After rotation, update the PDFViewCtrl pages layout by calling PDFViewCtrl.updatePageLayout().

To rotate a PDF page, you must lock PDFViewCtrl first, see: document locking.
public static void rotatePage(PDFViewCtrl pdfViewCtrl, int pageNum, @IntRange(from = 0, to = 3) int rotateValue) {
    boolean shouldUnlock = false;
    try {
        // lock pdf doc
        pdfViewCtrl.docLock(true);
        shouldUnlock = true;
        // get pdf document
        PDFDoc pdfDoc = pdfViewCtrl.getDoc();
        // get page from page number
        Page page = pdfDoc.getPage(pageNum);
        page.setRotation(rotateValue);
        // update page layout
        pdfViewCtrl.updatePageLayout();
    }catch (PDFNetException e) {
        e.printStackTrace();
    }finally {
        if (shouldUnlock){
            // unlock pdf doc
            pdfViewCtrl.docUnlock();
        }
    }
}

linkRotating PDF pages

To rotate PDF pages, traverse each page and call Page.setRotation(int). The following example shows how to rotate all pages of a PDF document.

public static void rotateAllPages(PDFViewCtrl pdfViewCtrl, @IntRange(from = 0, to = 3) int rotateValue) {
    boolean shouldUnlock = false;
    try {
       // lock pdf doc
        pdfViewCtrl.docLock(true);
        shouldUnlock = true;
        // get pdf document
        PDFDoc pdfDoc = pdfViewCtrl.getDoc();
        // get page iteration
        PageIterator itr = pdfDoc.getPageIterator();
        while (itr.hasNext()) {
            Page page = (Page) itr.next();
            // rotate page
            page.setRotation(rotateValue);
        }
        // update page layout
        pdfViewCtrl.updatePageLayout();
    }catch (PDFNetException e) {
        e.printStackTrace();
    }finally {
        if (shouldUnlock){
            // unlock pdf
            pdfViewCtrl.docUnlock();
        }
    }
}

Get the answers you need: Support

close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

PDFTron Receives USD$71 Million Growth Investment Led By Silversmith Capital Partners

Learn more
close