Page and Body Dimensions

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF Merger for Java (v6)  /  Page and Body Dimensions

DynamicPDF Merger for Java (v6) Forum

 Mar 31 2016 2:31 PM
Having trouble understanding Page Dimensions and Page Body Dimensions.

I wrote a very simple test to simply add 2 rectangles to a page - one using the Page Dimensions (page.getDimensions()) and one using the Page Body Dimensions (page.getDimensions().getBody()) -- see code below

This results in a PDF that simply shows (presumably) both rectangles not only on top of each other but extending off of the page width and height.

My goal is to modify page dimensions for our particular format/layout

--- CODE BLOCK ---
public class TestPageDimensions
{

    public static void main ( final String[] args )
    {
        try {
            // Create a PDF Document
            com.cete.dynamicpdf.Document.addLicense( "foo" );
            final Document document = new Document();

            TestPageDimensions.writeDocument( document );
            TestPageDimensions.printToFile( document );
        } catch ( final Throwable t ) {
            t.printStackTrace();
        }

    }

    private static void writeDocument ( final Document document )
    {
        final PageDimensions dimensions = new PageDimensions( PageSize.LETTER,
                                                              PageOrientation.PORTRAIT );
        final Page newPage = new Page( dimensions );

        // create rectangle showing page dimensions
        final PageDimensions dim = newPage.getDimensions();
        final Rectangle pageRec = new Rectangle( dim.getTopMargin(), dim.getLeftMargin(),
                                                 dim.getWidth(), dim.getHeight() );
        newPage.getElements().add( pageRec );

        // create rectangle showing body dimensions
        final Dimensions body = dim.getBody();
        final Rectangle bodyRec = new Rectangle( body.getTop(), body.getLeft(), body.getWidth(),
                                                 body.getHeight() );
        newPage.getElements().add( bodyRec );

        document.getPages().add( newPage );
    }

    protected static void printToFile ( final Document document )
    {
        final File pdf = new File( "test.pdf" );
        System.out.println( "Printing PDF to: " + pdf.getAbsolutePath() );
        try {
            if ( pdf.exists() ) {
                pdf.delete();
            }
            document.draw( pdf.getAbsolutePath() );
        } catch ( final Throwable t ) {
            t.printStackTrace();
        }
    }
---
 Mar 31 2016 4:49 PM
Posted by a ceTe Software moderator
Hello,

When you create a page with default values as shown below the margins are set to 50 points on all sides.

PageDimensions dimensions = new PageDimensions( PageSize.LETTER, PageOrientation.PORTRAIT); Page newPage = new Page( dimensions );

You may specify your own margins as shown in this example. Also, refer to the topic on the coordinate system of the API.

The body of the page is within the defined margins and when you add a page element it is added within the body with respect to its top left corner. In your code, the x, y co-ordinates of the rectangles are incorrect. To visually see the page body by drawing a rectangle along the border, use the following code:

final Dimensions body = dim.getBody();
final Rectangle bodyRec = new Rectangle( 0, 0, body.getWidth(), body.getHeight() );

To visualize the page dimensions, use the following code.

final PageDimensions dim = newPage.getDimensions();
//reduce the height and width by 1 point to ensure the line does not fall outside the visible portion of the page.      
final Rectangle pageRec = new Rectangle(-dim.getTopMargin(),-dim.getLeftMargin(),dim.getWidth()-1,dim.getHeight()-1);


Thanks,
ceTe Software Support Team.
 Apr 01 2016 1:39 PM
So I combined the example (with the modified margins) with the code you supplied for the page and body border rectangles. However, the border rectangles do not draw correctly. It appears that the code to draw the rectangles you provided only works with the default margins of 50.

What I am looking for is a way to create custom margins and then be able to draw these rectangles so that they mark the proper boundaries of the page and body dimensions on the PDF page -- that will allow me to determine proper locations for elements on my PDF as well as determine when to create a new page because an element will extend past the bottom of the body.

--- CODE ---
    private static void writeDocument ( final Document document )
    {
        // Create a page dimensions object and set the margins
        PageDimensions dimensions = new PageDimensions( PageSize.LETTER, PageOrientation.PORTRAIT );
        dimensions.setBottomMargin( 50 );
        dimensions.setTopMargin( 50 );
        dimensions.setLeftMargin( 35 );
        dimensions.setRightMargin( 35 );

        // Create a page using a page dimensions object and add it to the document
        Page page = new Page( dimensions );
        document.getPages().add( page );

        final PageDimensions dim = page.getDimensions();
        // reduce the height and width by 1 point to ensure the line does not fall outside the
        // visible portion of the page.
        final Rectangle pageRec = new Rectangle( -dim.getTopMargin(), -dim.getLeftMargin(),dim.getWidth() - 1, dim.getHeight() - 1 );

        final Dimensions body = dim.getBody();
        final Rectangle bodyRec = new Rectangle( 0, 0, body.getWidth(), body.getHeight() );
       
        page.getElements().add( pageRec );
        page.getElements().add( bodyRec );

        document.getPages().add( page );
    }
 Apr 04 2016 9:36 AM
Posted by a ceTe Software moderator
Hello,

In the following line of code use -dim.getLeftMargin() as x co-ordinate and -dim.getTopMargin() as y co-ordinate to the rectangle correctly.

final Rectangle pageRec = new Rectangle(-dim.getLeftMargin(), -dim.getTopMargin() ,dim.getWidth() - 1, dim.getHeight() - 1 );

Regardless of what the margins are, if you want to draw a rectangle along the page dimensions, you would want to offset the x co-ordinate by the value of the left margin and y co-ordinate by the value of the top margin. Here is the modified code:

       // Create a page dimensions object and set the margins
        PageDimensions dimensions = new PageDimensions( PageSize.LETTER, PageOrientation.PORTRAIT );
        dimensions.setBottomMargin( 10 );
        dimensions.setTopMargin( 20 );
        dimensions.setLeftMargin( 25 );
        dimensions.setRightMargin( 65 );
        // Create a page using a page dimensions object and add it to the document
        Page page = new Page( dimensions );
        document.getPages().add( page );
        final PageDimensions dim = page.getDimensions();
        // reduce the height and width by 1 point to ensure the line does not fall outside the
        // visible portion of the page.
        final Rectangle pageRec = new Rectangle( -dim.getLeftMargin(), -dim.getTopMargin() ,dim.getWidth() - 1, dim.getHeight() - 1 );
        final Dimensions body = dim.getBody();
        final Rectangle bodyRec = new Rectangle( 0, 0, body.getWidth(), body.getHeight() );      
        page.getElements().add( pageRec );
        page.getElements().add( bodyRec );
        //layout grid.
        page.getElements().add(new LayoutGrid());
        document.getPages().add( page );

Moreover, if your goal is to determine proper location of the page elements while you build the PDF, we have a page element called LayoutGrid that places a grid within the page body to help visualize the location of page elements.

Thanks,
ceTe Software Support Team.

All times are US Eastern Standard time. The time now is 7:32 PM.