com.aspose.words

Class PageInfo

  • java.lang.Object
    • com.aspose.words.PageInfo
public class PageInfo 
extends java.lang.Object

Represents information about a particular document page.

The page width and height returned by this object represent the "final" size of the page e.g. they are already rotated to the correct orientation.

Example:

Renders a page of a Word document into a BufferedImage using a specified zoom factor.
Document doc = new Document(getMyDir() + "Rendering.doc");

PageInfo pageInfo = doc.getPageInfo(0);

// Let's say we want the image at 50% zoom.
final float myScale = 0.50f;

// Let's say we want the image at this resolution.
final float myResolution = 200.0f;

Dimension pageSize = pageInfo.getSizeInPixels(myScale, myResolution);

BufferedImage img = new BufferedImage((int) pageSize.getWidth(), (int) pageSize.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = img.createGraphics();

try {
    // You can apply various settings to the Graphics object.
    gr.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

    // Fill the page background.
    gr.setPaint(Color.black);

    // Render the page using the zoom.
    doc.renderToScale(0, gr, 0, 0, myScale);
} finally {
    if (gr != null) {
        gr.dispose();
    }
}

ImageIO.write(img, "PNG", new File(getArtifactsDir() + "Rendering.RenderToScale.png"));

Example:

Shows how to print page size and orientation information for every page in a Word document.
Document doc = new Document(getMyDir() + "Rendering.doc");

// The first section has 2 pages
// We will assign a different printer paper tray to each one, whose number will match a kind of paper source
// These sources and their Kinds will vary depending on the installed printer driver
// Choose the default printer to be used for printing this document.
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
Media[] trays = (Media[]) printService.getSupportedAttributeValues(Media.class, null, null);

doc.getFirstSection().getPageSetup().setFirstPageTray(trays[0].getValue());
doc.getFirstSection().getPageSetup().setOtherPagesTray(trays[1].getValue());

System.out.println(MessageFormat.format("Document \"{0}\" contains {1} pages.", doc.getOriginalFileName(), doc.getPageCount()));

float scale = 1.0f;
float dpi = 96f;

for (int i = 0; i < doc.getPageCount(); i++) {
    // Each page has a PageInfo object, whose index is the respective page's number
    PageInfo pageInfo = doc.getPageInfo(i);

    // Print the page's orientation and dimensions
    System.out.println(MessageFormat.format("Page {0}:", i++));
    System.out.println(MessageFormat.format("\tOrientation:\t{0}", (pageInfo.getLandscape() ? "Landscape" : "Portrait")));
    System.out.println(MessageFormat.format("\tPaper size:\t\t{0} ({1:F0}x{2:F0}pt)", pageInfo.getPaperSize(), pageInfo.getWidthInPoints(), pageInfo.getHeightInPoints()));
    System.out.println(MessageFormat.format("\tSize in points:\t{0}", pageInfo.getSizeInPoints()));
    System.out.println(MessageFormat.format("\tSize in pixels:\t{0} at {1}% scale, {2} dpi", pageInfo.getSizeInPixels(1.0f, 96), scale * 100, dpi));

    // Paper source tray information
    System.out.println(MessageFormat.format("\tTray:\t{0}", pageInfo.getPaperTray()));
}
See Also:
Document.getPageInfo(int)

Property Getters/Setters Summary
floatgetHeightInPoints()
Gets the height of the page in points.
booleangetLandscape()
Returns true if the page orientation specified in the document for this page is landscape.
intgetPaperSize()
Gets the paper size as enumeration. The value of the property is PaperSize integer constant.
intgetPaperTray()
Gets the paper tray (bin) for this page as specified in the document. The value is implementation (printer) specific.
java.awt.DimensiongetSizeInPoints()
Gets the page size in points.
floatgetWidthInPoints()
Gets the width of the page in points.
 
Method Summary
java.awt.DimensiongetSizeInPixels(float scale, float dpi)
Calculates the page size in pixels for a specified zoom factor and resolution.
java.awt.DimensiongetSizeInPixels(float scale, float horizontalDpi, float verticalDpi)
Calculates the page size in pixels for a specified zoom factor and resolution.
 

    • Property Getters/Setters Detail

      • getHeightInPoints

        public float getHeightInPoints()
        
        Gets the height of the page in points.

        Example:

        Shows how to print page size and orientation information for every page in a Word document.
        Document doc = new Document(getMyDir() + "Rendering.doc");
        
        // The first section has 2 pages
        // We will assign a different printer paper tray to each one, whose number will match a kind of paper source
        // These sources and their Kinds will vary depending on the installed printer driver
        // Choose the default printer to be used for printing this document.
        PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
        Media[] trays = (Media[]) printService.getSupportedAttributeValues(Media.class, null, null);
        
        doc.getFirstSection().getPageSetup().setFirstPageTray(trays[0].getValue());
        doc.getFirstSection().getPageSetup().setOtherPagesTray(trays[1].getValue());
        
        System.out.println(MessageFormat.format("Document \"{0}\" contains {1} pages.", doc.getOriginalFileName(), doc.getPageCount()));
        
        float scale = 1.0f;
        float dpi = 96f;
        
        for (int i = 0; i < doc.getPageCount(); i++) {
            // Each page has a PageInfo object, whose index is the respective page's number
            PageInfo pageInfo = doc.getPageInfo(i);
        
            // Print the page's orientation and dimensions
            System.out.println(MessageFormat.format("Page {0}:", i++));
            System.out.println(MessageFormat.format("\tOrientation:\t{0}", (pageInfo.getLandscape() ? "Landscape" : "Portrait")));
            System.out.println(MessageFormat.format("\tPaper size:\t\t{0} ({1:F0}x{2:F0}pt)", pageInfo.getPaperSize(), pageInfo.getWidthInPoints(), pageInfo.getHeightInPoints()));
            System.out.println(MessageFormat.format("\tSize in points:\t{0}", pageInfo.getSizeInPoints()));
            System.out.println(MessageFormat.format("\tSize in pixels:\t{0} at {1}% scale, {2} dpi", pageInfo.getSizeInPixels(1.0f, 96), scale * 100, dpi));
        
            // Paper source tray information
            System.out.println(MessageFormat.format("\tTray:\t{0}", pageInfo.getPaperTray()));
        }

        Example:

        Shows how to implement your own Pageable document to completely customize printing of Aspose.Words documents.
        public void customPrint() throws Exception {
            Document doc = new Document(getMyDir() + "Rendering.doc");
        
            // Create an instance of our own Pageable document.
            MyPrintDocument printDoc = new MyPrintDocument(doc, 2, 6);
        
            // Print with the default printer
            PrinterJob pj = PrinterJob.getPrinterJob();
        
            // Set our custom class as the print target.
            pj.setPageable(printDoc);
        
            // Print the document to the default printer.
            pj.print();
        }
        
        /**
         * The way to print in Java is to implement a class which implements Printable and Pageable. The latter
         * allows for different pages to have different page size and orientation.
         * <p>
         * This class is an example on how to implement custom printing of an Aspose.Words document.
         * It selects an appropriate paper size, orientation when printing.
         */
        public class MyPrintDocument implements Pageable, Printable {
            public MyPrintDocument(final Document document) throws Exception {
                this(document, 1, document.getPageCount());
            }
        
            public MyPrintDocument(final Document document, final int fromPage, final int toPage) {
                mDocument = document;
                mFromPage = fromPage;
                mToPage = toPage;
            }
        
            /**
             * This is called by the Print API to retrieve the number of pages that are expected
             * to be printed.
             */
            public int getNumberOfPages() {
                return (mToPage - mFromPage) + 1;
            }
        
            /**
             * This is called by the Print API to retrieve the page format of the given page.
             */
            public PageFormat getPageFormat(final int pageIndex) {
        
                PageFormat format = new PageFormat();
                Paper paper = new Paper();
        
                try {
                    // Retrieve the page info of the requested page. The pageIndex starts at 0 and is the first page to print.
                    // We calculate the real page to print based on the start page.
                    PageInfo info = mDocument.getPageInfo(pageIndex + mFromPage - 1);
        
                    // Set the page orientation as landscape or portrait based off the document page.
                    boolean isLandscape = info.getLandscape();
                    format.setOrientation(isLandscape ? PageFormat.LANDSCAPE : PageFormat.PORTRAIT);
        
                    // Set some margins for the printable area of the page.
                    paper.setImageableArea(1.0, 1.0, paper.getWidth() - 2, paper.getHeight() - 2);
                } catch (Exception e) {
                    // If there are any errors then use the default paper size.
                }
        
                format.setPaper(paper);
        
                return format;
            }
        
            /**
             * Called for each page to be printed. We must supply an object which will handle the printing of the
             * specified page. In our case it's our class will always handle this.
             */
            public Printable getPrintable(final int pageIndex) {
                return this;
            }
        
            /**
             * Called when the specified page is to be printed. The page is rendered onto the supplied graphics object.
             */
            public int print(final Graphics g, final PageFormat pf, final int pageIndex) {
                try {
                    mDocument.renderToScale(pageIndex + mFromPage - 1, (Graphics2D) g, (int) pf.getImageableX(), (int) pf.getImageableY(), 1.0f);
                } catch (Exception e) {
                    // If there are any problems with rendering the document or when the given index is out of bounds we arrive here.
                    // We return Printable.NO_SUCH_PAGE is returned so that printing finishes here.
                    return Printable.NO_SUCH_PAGE;
                }
                return Printable.PAGE_EXISTS;
            }
        
            private Document mDocument;
            private int mFromPage;
            private int mToPage;
        }
      • getLandscape

        public boolean getLandscape()
        
        Returns true if the page orientation specified in the document for this page is landscape.

        Example:

        Shows how to print page size and orientation information for every page in a Word document.
        Document doc = new Document(getMyDir() + "Rendering.doc");
        
        // The first section has 2 pages
        // We will assign a different printer paper tray to each one, whose number will match a kind of paper source
        // These sources and their Kinds will vary depending on the installed printer driver
        // Choose the default printer to be used for printing this document.
        PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
        Media[] trays = (Media[]) printService.getSupportedAttributeValues(Media.class, null, null);
        
        doc.getFirstSection().getPageSetup().setFirstPageTray(trays[0].getValue());
        doc.getFirstSection().getPageSetup().setOtherPagesTray(trays[1].getValue());
        
        System.out.println(MessageFormat.format("Document \"{0}\" contains {1} pages.", doc.getOriginalFileName(), doc.getPageCount()));
        
        float scale = 1.0f;
        float dpi = 96f;
        
        for (int i = 0; i < doc.getPageCount(); i++) {
            // Each page has a PageInfo object, whose index is the respective page's number
            PageInfo pageInfo = doc.getPageInfo(i);
        
            // Print the page's orientation and dimensions
            System.out.println(MessageFormat.format("Page {0}:", i++));
            System.out.println(MessageFormat.format("\tOrientation:\t{0}", (pageInfo.getLandscape() ? "Landscape" : "Portrait")));
            System.out.println(MessageFormat.format("\tPaper size:\t\t{0} ({1:F0}x{2:F0}pt)", pageInfo.getPaperSize(), pageInfo.getWidthInPoints(), pageInfo.getHeightInPoints()));
            System.out.println(MessageFormat.format("\tSize in points:\t{0}", pageInfo.getSizeInPoints()));
            System.out.println(MessageFormat.format("\tSize in pixels:\t{0} at {1}% scale, {2} dpi", pageInfo.getSizeInPixels(1.0f, 96), scale * 100, dpi));
        
            // Paper source tray information
            System.out.println(MessageFormat.format("\tTray:\t{0}", pageInfo.getPaperTray()));
        }

        Example:

        Shows how to implement your own Pageable document to completely customize printing of Aspose.Words documents.
        public void customPrint() throws Exception {
            Document doc = new Document(getMyDir() + "Rendering.doc");
        
            // Create an instance of our own Pageable document.
            MyPrintDocument printDoc = new MyPrintDocument(doc, 2, 6);
        
            // Print with the default printer
            PrinterJob pj = PrinterJob.getPrinterJob();
        
            // Set our custom class as the print target.
            pj.setPageable(printDoc);
        
            // Print the document to the default printer.
            pj.print();
        }
        
        /**
         * The way to print in Java is to implement a class which implements Printable and Pageable. The latter
         * allows for different pages to have different page size and orientation.
         * <p>
         * This class is an example on how to implement custom printing of an Aspose.Words document.
         * It selects an appropriate paper size, orientation when printing.
         */
        public class MyPrintDocument implements Pageable, Printable {
            public MyPrintDocument(final Document document) throws Exception {
                this(document, 1, document.getPageCount());
            }
        
            public MyPrintDocument(final Document document, final int fromPage, final int toPage) {
                mDocument = document;
                mFromPage = fromPage;
                mToPage = toPage;
            }
        
            /**
             * This is called by the Print API to retrieve the number of pages that are expected
             * to be printed.
             */
            public int getNumberOfPages() {
                return (mToPage - mFromPage) + 1;
            }
        
            /**
             * This is called by the Print API to retrieve the page format of the given page.
             */
            public PageFormat getPageFormat(final int pageIndex) {
        
                PageFormat format = new PageFormat();
                Paper paper = new Paper();
        
                try {
                    // Retrieve the page info of the requested page. The pageIndex starts at 0 and is the first page to print.
                    // We calculate the real page to print based on the start page.
                    PageInfo info = mDocument.getPageInfo(pageIndex + mFromPage - 1);
        
                    // Set the page orientation as landscape or portrait based off the document page.
                    boolean isLandscape = info.getLandscape();
                    format.setOrientation(isLandscape ? PageFormat.LANDSCAPE : PageFormat.PORTRAIT);
        
                    // Set some margins for the printable area of the page.
                    paper.setImageableArea(1.0, 1.0, paper.getWidth() - 2, paper.getHeight() - 2);
                } catch (Exception e) {
                    // If there are any errors then use the default paper size.
                }
        
                format.setPaper(paper);
        
                return format;
            }
        
            /**
             * Called for each page to be printed. We must supply an object which will handle the printing of the
             * specified page. In our case it's our class will always handle this.
             */
            public Printable getPrintable(final int pageIndex) {
                return this;
            }
        
            /**
             * Called when the specified page is to be printed. The page is rendered onto the supplied graphics object.
             */
            public int print(final Graphics g, final PageFormat pf, final int pageIndex) {
                try {
                    mDocument.renderToScale(pageIndex + mFromPage - 1, (Graphics2D) g, (int) pf.getImageableX(), (int) pf.getImageableY(), 1.0f);
                } catch (Exception e) {
                    // If there are any problems with rendering the document or when the given index is out of bounds we arrive here.
                    // We return Printable.NO_SUCH_PAGE is returned so that printing finishes here.
                    return Printable.NO_SUCH_PAGE;
                }
                return Printable.PAGE_EXISTS;
            }
        
            private Document mDocument;
            private int mFromPage;
            private int mToPage;
        }
      • getPaperSize

        public int getPaperSize()
        
        Gets the paper size as enumeration. The value of the property is PaperSize integer constant.

        Example:

        Shows how to print page size and orientation information for every page in a Word document.
        Document doc = new Document(getMyDir() + "Rendering.doc");
        
        // The first section has 2 pages
        // We will assign a different printer paper tray to each one, whose number will match a kind of paper source
        // These sources and their Kinds will vary depending on the installed printer driver
        // Choose the default printer to be used for printing this document.
        PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
        Media[] trays = (Media[]) printService.getSupportedAttributeValues(Media.class, null, null);
        
        doc.getFirstSection().getPageSetup().setFirstPageTray(trays[0].getValue());
        doc.getFirstSection().getPageSetup().setOtherPagesTray(trays[1].getValue());
        
        System.out.println(MessageFormat.format("Document \"{0}\" contains {1} pages.", doc.getOriginalFileName(), doc.getPageCount()));
        
        float scale = 1.0f;
        float dpi = 96f;
        
        for (int i = 0; i < doc.getPageCount(); i++) {
            // Each page has a PageInfo object, whose index is the respective page's number
            PageInfo pageInfo = doc.getPageInfo(i);
        
            // Print the page's orientation and dimensions
            System.out.println(MessageFormat.format("Page {0}:", i++));
            System.out.println(MessageFormat.format("\tOrientation:\t{0}", (pageInfo.getLandscape() ? "Landscape" : "Portrait")));
            System.out.println(MessageFormat.format("\tPaper size:\t\t{0} ({1:F0}x{2:F0}pt)", pageInfo.getPaperSize(), pageInfo.getWidthInPoints(), pageInfo.getHeightInPoints()));
            System.out.println(MessageFormat.format("\tSize in points:\t{0}", pageInfo.getSizeInPoints()));
            System.out.println(MessageFormat.format("\tSize in pixels:\t{0} at {1}% scale, {2} dpi", pageInfo.getSizeInPixels(1.0f, 96), scale * 100, dpi));
        
            // Paper source tray information
            System.out.println(MessageFormat.format("\tTray:\t{0}", pageInfo.getPaperTray()));
        }
      • getPaperTray

        public int getPaperTray()
        
        Gets the paper tray (bin) for this page as specified in the document. The value is implementation (printer) specific.

        Example:

        Shows how to print page size and orientation information for every page in a Word document.
        Document doc = new Document(getMyDir() + "Rendering.doc");
        
        // The first section has 2 pages
        // We will assign a different printer paper tray to each one, whose number will match a kind of paper source
        // These sources and their Kinds will vary depending on the installed printer driver
        // Choose the default printer to be used for printing this document.
        PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
        Media[] trays = (Media[]) printService.getSupportedAttributeValues(Media.class, null, null);
        
        doc.getFirstSection().getPageSetup().setFirstPageTray(trays[0].getValue());
        doc.getFirstSection().getPageSetup().setOtherPagesTray(trays[1].getValue());
        
        System.out.println(MessageFormat.format("Document \"{0}\" contains {1} pages.", doc.getOriginalFileName(), doc.getPageCount()));
        
        float scale = 1.0f;
        float dpi = 96f;
        
        for (int i = 0; i < doc.getPageCount(); i++) {
            // Each page has a PageInfo object, whose index is the respective page's number
            PageInfo pageInfo = doc.getPageInfo(i);
        
            // Print the page's orientation and dimensions
            System.out.println(MessageFormat.format("Page {0}:", i++));
            System.out.println(MessageFormat.format("\tOrientation:\t{0}", (pageInfo.getLandscape() ? "Landscape" : "Portrait")));
            System.out.println(MessageFormat.format("\tPaper size:\t\t{0} ({1:F0}x{2:F0}pt)", pageInfo.getPaperSize(), pageInfo.getWidthInPoints(), pageInfo.getHeightInPoints()));
            System.out.println(MessageFormat.format("\tSize in points:\t{0}", pageInfo.getSizeInPoints()));
            System.out.println(MessageFormat.format("\tSize in pixels:\t{0} at {1}% scale, {2} dpi", pageInfo.getSizeInPixels(1.0f, 96), scale * 100, dpi));
        
            // Paper source tray information
            System.out.println(MessageFormat.format("\tTray:\t{0}", pageInfo.getPaperTray()));
        }
      • getSizeInPoints

        public java.awt.Dimension getSizeInPoints()
        
        Gets the page size in points.

        Example:

        Shows how to print page size and orientation information for every page in a Word document.
        Document doc = new Document(getMyDir() + "Rendering.doc");
        
        // The first section has 2 pages
        // We will assign a different printer paper tray to each one, whose number will match a kind of paper source
        // These sources and their Kinds will vary depending on the installed printer driver
        // Choose the default printer to be used for printing this document.
        PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
        Media[] trays = (Media[]) printService.getSupportedAttributeValues(Media.class, null, null);
        
        doc.getFirstSection().getPageSetup().setFirstPageTray(trays[0].getValue());
        doc.getFirstSection().getPageSetup().setOtherPagesTray(trays[1].getValue());
        
        System.out.println(MessageFormat.format("Document \"{0}\" contains {1} pages.", doc.getOriginalFileName(), doc.getPageCount()));
        
        float scale = 1.0f;
        float dpi = 96f;
        
        for (int i = 0; i < doc.getPageCount(); i++) {
            // Each page has a PageInfo object, whose index is the respective page's number
            PageInfo pageInfo = doc.getPageInfo(i);
        
            // Print the page's orientation and dimensions
            System.out.println(MessageFormat.format("Page {0}:", i++));
            System.out.println(MessageFormat.format("\tOrientation:\t{0}", (pageInfo.getLandscape() ? "Landscape" : "Portrait")));
            System.out.println(MessageFormat.format("\tPaper size:\t\t{0} ({1:F0}x{2:F0}pt)", pageInfo.getPaperSize(), pageInfo.getWidthInPoints(), pageInfo.getHeightInPoints()));
            System.out.println(MessageFormat.format("\tSize in points:\t{0}", pageInfo.getSizeInPoints()));
            System.out.println(MessageFormat.format("\tSize in pixels:\t{0} at {1}% scale, {2} dpi", pageInfo.getSizeInPixels(1.0f, 96), scale * 100, dpi));
        
            // Paper source tray information
            System.out.println(MessageFormat.format("\tTray:\t{0}", pageInfo.getPaperTray()));
        }
      • getWidthInPoints

        public float getWidthInPoints()
        
        Gets the width of the page in points.

        Example:

        Shows how to print page size and orientation information for every page in a Word document.
        Document doc = new Document(getMyDir() + "Rendering.doc");
        
        // The first section has 2 pages
        // We will assign a different printer paper tray to each one, whose number will match a kind of paper source
        // These sources and their Kinds will vary depending on the installed printer driver
        // Choose the default printer to be used for printing this document.
        PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
        Media[] trays = (Media[]) printService.getSupportedAttributeValues(Media.class, null, null);
        
        doc.getFirstSection().getPageSetup().setFirstPageTray(trays[0].getValue());
        doc.getFirstSection().getPageSetup().setOtherPagesTray(trays[1].getValue());
        
        System.out.println(MessageFormat.format("Document \"{0}\" contains {1} pages.", doc.getOriginalFileName(), doc.getPageCount()));
        
        float scale = 1.0f;
        float dpi = 96f;
        
        for (int i = 0; i < doc.getPageCount(); i++) {
            // Each page has a PageInfo object, whose index is the respective page's number
            PageInfo pageInfo = doc.getPageInfo(i);
        
            // Print the page's orientation and dimensions
            System.out.println(MessageFormat.format("Page {0}:", i++));
            System.out.println(MessageFormat.format("\tOrientation:\t{0}", (pageInfo.getLandscape() ? "Landscape" : "Portrait")));
            System.out.println(MessageFormat.format("\tPaper size:\t\t{0} ({1:F0}x{2:F0}pt)", pageInfo.getPaperSize(), pageInfo.getWidthInPoints(), pageInfo.getHeightInPoints()));
            System.out.println(MessageFormat.format("\tSize in points:\t{0}", pageInfo.getSizeInPoints()));
            System.out.println(MessageFormat.format("\tSize in pixels:\t{0} at {1}% scale, {2} dpi", pageInfo.getSizeInPixels(1.0f, 96), scale * 100, dpi));
        
            // Paper source tray information
            System.out.println(MessageFormat.format("\tTray:\t{0}", pageInfo.getPaperTray()));
        }

        Example:

        Shows how to implement your own Pageable document to completely customize printing of Aspose.Words documents.
        public void customPrint() throws Exception {
            Document doc = new Document(getMyDir() + "Rendering.doc");
        
            // Create an instance of our own Pageable document.
            MyPrintDocument printDoc = new MyPrintDocument(doc, 2, 6);
        
            // Print with the default printer
            PrinterJob pj = PrinterJob.getPrinterJob();
        
            // Set our custom class as the print target.
            pj.setPageable(printDoc);
        
            // Print the document to the default printer.
            pj.print();
        }
        
        /**
         * The way to print in Java is to implement a class which implements Printable and Pageable. The latter
         * allows for different pages to have different page size and orientation.
         * <p>
         * This class is an example on how to implement custom printing of an Aspose.Words document.
         * It selects an appropriate paper size, orientation when printing.
         */
        public class MyPrintDocument implements Pageable, Printable {
            public MyPrintDocument(final Document document) throws Exception {
                this(document, 1, document.getPageCount());
            }
        
            public MyPrintDocument(final Document document, final int fromPage, final int toPage) {
                mDocument = document;
                mFromPage = fromPage;
                mToPage = toPage;
            }
        
            /**
             * This is called by the Print API to retrieve the number of pages that are expected
             * to be printed.
             */
            public int getNumberOfPages() {
                return (mToPage - mFromPage) + 1;
            }
        
            /**
             * This is called by the Print API to retrieve the page format of the given page.
             */
            public PageFormat getPageFormat(final int pageIndex) {
        
                PageFormat format = new PageFormat();
                Paper paper = new Paper();
        
                try {
                    // Retrieve the page info of the requested page. The pageIndex starts at 0 and is the first page to print.
                    // We calculate the real page to print based on the start page.
                    PageInfo info = mDocument.getPageInfo(pageIndex + mFromPage - 1);
        
                    // Set the page orientation as landscape or portrait based off the document page.
                    boolean isLandscape = info.getLandscape();
                    format.setOrientation(isLandscape ? PageFormat.LANDSCAPE : PageFormat.PORTRAIT);
        
                    // Set some margins for the printable area of the page.
                    paper.setImageableArea(1.0, 1.0, paper.getWidth() - 2, paper.getHeight() - 2);
                } catch (Exception e) {
                    // If there are any errors then use the default paper size.
                }
        
                format.setPaper(paper);
        
                return format;
            }
        
            /**
             * Called for each page to be printed. We must supply an object which will handle the printing of the
             * specified page. In our case it's our class will always handle this.
             */
            public Printable getPrintable(final int pageIndex) {
                return this;
            }
        
            /**
             * Called when the specified page is to be printed. The page is rendered onto the supplied graphics object.
             */
            public int print(final Graphics g, final PageFormat pf, final int pageIndex) {
                try {
                    mDocument.renderToScale(pageIndex + mFromPage - 1, (Graphics2D) g, (int) pf.getImageableX(), (int) pf.getImageableY(), 1.0f);
                } catch (Exception e) {
                    // If there are any problems with rendering the document or when the given index is out of bounds we arrive here.
                    // We return Printable.NO_SUCH_PAGE is returned so that printing finishes here.
                    return Printable.NO_SUCH_PAGE;
                }
                return Printable.PAGE_EXISTS;
            }
        
            private Document mDocument;
            private int mFromPage;
            private int mToPage;
        }
    • Method Detail

      • getSizeInPixels

        public java.awt.Dimension getSizeInPixels(float scale, float dpi)
        Calculates the page size in pixels for a specified zoom factor and resolution.
        Parameters:
        scale - The zoom factor (1.0 is 100%).
        dpi - The resolution (horizontal and vertical) to convert from points to pixels (dots per inch).
        Returns:
        The size of the page in pixels.

        Example:

        Renders a page of a Word document into a BufferedImage using a specified zoom factor.
        Document doc = new Document(getMyDir() + "Rendering.doc");
        
        PageInfo pageInfo = doc.getPageInfo(0);
        
        // Let's say we want the image at 50% zoom.
        final float myScale = 0.50f;
        
        // Let's say we want the image at this resolution.
        final float myResolution = 200.0f;
        
        Dimension pageSize = pageInfo.getSizeInPixels(myScale, myResolution);
        
        BufferedImage img = new BufferedImage((int) pageSize.getWidth(), (int) pageSize.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D gr = img.createGraphics();
        
        try {
            // You can apply various settings to the Graphics object.
            gr.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        
            // Fill the page background.
            gr.setPaint(Color.black);
        
            // Render the page using the zoom.
            doc.renderToScale(0, gr, 0, 0, myScale);
        } finally {
            if (gr != null) {
                gr.dispose();
            }
        }
        
        ImageIO.write(img, "PNG", new File(getArtifactsDir() + "Rendering.RenderToScale.png"));
      • getSizeInPixels

        public java.awt.Dimension getSizeInPixels(float scale, float horizontalDpi, float verticalDpi)
        Calculates the page size in pixels for a specified zoom factor and resolution.
        Parameters:
        scale - The zoom factor (1.0 is 100%).
        horizontalDpi - The horizontal resolution to convert from points to pixels (dots per inch).
        verticalDpi - The vertical resolution to convert from points to pixels (dots per inch).
        Returns:
        The size of the page in pixels.

        Example:

        Shows how to print page size and orientation information for every page in a Word document.
        Document doc = new Document(getMyDir() + "Rendering.doc");
        
        // The first section has 2 pages
        // We will assign a different printer paper tray to each one, whose number will match a kind of paper source
        // These sources and their Kinds will vary depending on the installed printer driver
        // Choose the default printer to be used for printing this document.
        PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
        Media[] trays = (Media[]) printService.getSupportedAttributeValues(Media.class, null, null);
        
        doc.getFirstSection().getPageSetup().setFirstPageTray(trays[0].getValue());
        doc.getFirstSection().getPageSetup().setOtherPagesTray(trays[1].getValue());
        
        System.out.println(MessageFormat.format("Document \"{0}\" contains {1} pages.", doc.getOriginalFileName(), doc.getPageCount()));
        
        float scale = 1.0f;
        float dpi = 96f;
        
        for (int i = 0; i < doc.getPageCount(); i++) {
            // Each page has a PageInfo object, whose index is the respective page's number
            PageInfo pageInfo = doc.getPageInfo(i);
        
            // Print the page's orientation and dimensions
            System.out.println(MessageFormat.format("Page {0}:", i++));
            System.out.println(MessageFormat.format("\tOrientation:\t{0}", (pageInfo.getLandscape() ? "Landscape" : "Portrait")));
            System.out.println(MessageFormat.format("\tPaper size:\t\t{0} ({1:F0}x{2:F0}pt)", pageInfo.getPaperSize(), pageInfo.getWidthInPoints(), pageInfo.getHeightInPoints()));
            System.out.println(MessageFormat.format("\tSize in points:\t{0}", pageInfo.getSizeInPoints()));
            System.out.println(MessageFormat.format("\tSize in pixels:\t{0} at {1}% scale, {2} dpi", pageInfo.getSizeInPixels(1.0f, 96), scale * 100, dpi));
        
            // Paper source tray information
            System.out.println(MessageFormat.format("\tTray:\t{0}", pageInfo.getPaperTray()));
        }