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:

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

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

for (int i = 0; i < doc.getPageCount(); i++)
{
    PageInfo pageInfo = doc.getPageInfo(i);
    System.out.println(MessageFormat.format("Page {0}. PaperSize:{1} ({2}x{3}pt), Orientation:{4}, PaperTray:{5}", i + 1, pageInfo.getPaperSize(), pageInfo.getWidthInPoints(), pageInfo.getHeightInPoints(), pageInfo.getLandscape() ? "Landscape" : "Portrait", pageInfo.getPaperTray()));
}

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.
float MY_SCALE = 0.50f;

Dimension pageSize = pageInfo.getSizeInPixels(MY_SCALE, 96.0f);

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, MY_SCALE);
} finally
{
    if (gr != null) gr.dispose();
}

ImageIO.write(img, "PNG", new File(getMyDir() + "\\Artifacts\\Rendering.RenderToScale.png"));
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:

        Retrieves page size and orientation information for every page in a Word document.
        Document doc = new Document(getMyDir() + "Rendering.doc");
        
        System.out.println(MessageFormat.format("Document \"{0}\" contains {1} pages.", doc.getOriginalFileName(), doc.getPageCount()));
        
        for (int i = 0; i < doc.getPageCount(); i++)
        {
            PageInfo pageInfo = doc.getPageInfo(i);
            System.out.println(MessageFormat.format("Page {0}. PaperSize:{1} ({2}x{3}pt), Orientation:{4}, PaperTray:{5}", i + 1, pageInfo.getPaperSize(), pageInfo.getWidthInPoints(), pageInfo.getHeightInPoints(), pageInfo.getLandscape() ? "Landscape" : "Portrait", 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(Document document) throws Exception
            {
                this(document, 1, document.getPageCount());
            }
        
            public MyPrintDocument(Document document, int fromPage, 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(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(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(Graphics g, PageFormat pf, 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:

        Retrieves page size and orientation information for every page in a Word document.
        Document doc = new Document(getMyDir() + "Rendering.doc");
        
        System.out.println(MessageFormat.format("Document \"{0}\" contains {1} pages.", doc.getOriginalFileName(), doc.getPageCount()));
        
        for (int i = 0; i < doc.getPageCount(); i++)
        {
            PageInfo pageInfo = doc.getPageInfo(i);
            System.out.println(MessageFormat.format("Page {0}. PaperSize:{1} ({2}x{3}pt), Orientation:{4}, PaperTray:{5}", i + 1, pageInfo.getPaperSize(), pageInfo.getWidthInPoints(), pageInfo.getHeightInPoints(), pageInfo.getLandscape() ? "Landscape" : "Portrait", 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(Document document) throws Exception
            {
                this(document, 1, document.getPageCount());
            }
        
            public MyPrintDocument(Document document, int fromPage, 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(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(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(Graphics g, PageFormat pf, 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:

        Retrieves page size and orientation information for every page in a Word document.
        Document doc = new Document(getMyDir() + "Rendering.doc");
        
        System.out.println(MessageFormat.format("Document \"{0}\" contains {1} pages.", doc.getOriginalFileName(), doc.getPageCount()));
        
        for (int i = 0; i < doc.getPageCount(); i++)
        {
            PageInfo pageInfo = doc.getPageInfo(i);
            System.out.println(MessageFormat.format("Page {0}. PaperSize:{1} ({2}x{3}pt), Orientation:{4}, PaperTray:{5}", i + 1, pageInfo.getPaperSize(), pageInfo.getWidthInPoints(), pageInfo.getHeightInPoints(), pageInfo.getLandscape() ? "Landscape" : "Portrait", 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:

        Retrieves page size and orientation information for every page in a Word document.
        Document doc = new Document(getMyDir() + "Rendering.doc");
        
        System.out.println(MessageFormat.format("Document \"{0}\" contains {1} pages.", doc.getOriginalFileName(), doc.getPageCount()));
        
        for (int i = 0; i < doc.getPageCount(); i++)
        {
            PageInfo pageInfo = doc.getPageInfo(i);
            System.out.println(MessageFormat.format("Page {0}. PaperSize:{1} ({2}x{3}pt), Orientation:{4}, PaperTray:{5}", i + 1, pageInfo.getPaperSize(), pageInfo.getWidthInPoints(), pageInfo.getHeightInPoints(), pageInfo.getLandscape() ? "Landscape" : "Portrait", pageInfo.getPaperTray()));
        }
      • getSizeInPoints

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

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

        Example:

        Retrieves page size and orientation information for every page in a Word document.
        Document doc = new Document(getMyDir() + "Rendering.doc");
        
        System.out.println(MessageFormat.format("Document \"{0}\" contains {1} pages.", doc.getOriginalFileName(), doc.getPageCount()));
        
        for (int i = 0; i < doc.getPageCount(); i++)
        {
            PageInfo pageInfo = doc.getPageInfo(i);
            System.out.println(MessageFormat.format("Page {0}. PaperSize:{1} ({2}x{3}pt), Orientation:{4}, PaperTray:{5}", i + 1, pageInfo.getPaperSize(), pageInfo.getWidthInPoints(), pageInfo.getHeightInPoints(), pageInfo.getLandscape() ? "Landscape" : "Portrait", 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(Document document) throws Exception
            {
                this(document, 1, document.getPageCount());
            }
        
            public MyPrintDocument(Document document, int fromPage, 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(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(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(Graphics g, PageFormat pf, 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.
      • 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.