PDFlib
BASKET
PDFlib

pagination/page_sizes

Create some pages with various page sizes. Create pages with A4 format and Portrait orientation, with A4 format and Landscape orientation, with Letter format and Portrait orientation, and with Letter format and Landscape orientation. Create a page with a size according to the dimensions of the image to be placed on the page.

Download Java Code     Show Output PDF

/* $Id: page_sizes.java,v 1.7 2007/08/30 18:30:35 katja Exp $

 * Page sizes:

 * Create some pages with various page sizes

 *

 * Create pages with A4 format and Portrait orientation, with A4 format and

 * Landscape orientation, with Letter format and Portrait orientation, and

 * with Letter format and Landscape orientation.

 *

 * Create a page with a size according to the dimensions of the image to be

 * placed on the page.

 *

 * Required software: PDFlib Lite/PDFlib/PDFlib+PDI/PPS 7

 * Required data: image file

 */

package com.pdflib.cookbook.pdflib.pagination;

import com.pdflib.pdflib;

import com.pdflib.PDFlibException;


public class page_sizes

{

    public static void main (String argv[])

    {

    /* This is where the data files are. Adjust as necessary. */

    String searchpath = "../input";

    String outfile = "page_sizes.pdf";

    String title = "Page Sizes";


    pdflib p = null;

    String imagefile = "nesrin.jpg";

    int font, image;

    double imagewidth, imageheight;


    try {

        p = new pdflib();


        p.set_parameter("SearchPath", searchpath);


        /* This means we must check return values of load_font() etc. */

        p.set_parameter("errorpolicy", "return");


        if (p.begin_document(outfile, "") == -1)

            throw new Exception("Error: " + p.get_errmsg());


        p.set_info("Creator", "PDFlib Cookbook");

        p.set_info("Title", title + " $Revision: 1.7 $");


        /* For PDFlib Lite: change "unicode" to "winansi" */

        font = p.load_font("Helvetica-Bold", "unicode", "");

        if (font == -1)

            throw new Exception("Error: " + p.get_errmsg());


        /* Create a page in A4 format and Portrait orientation */

        p.begin_page_ext(0, 0, "width=a4.width height=a4.height");


        p.fit_textline("A4 format in Portrait orientation.", 50, 50,

                       "font=" + font + " fontsize=20");

        p.end_page_ext("");


        /* Create a page in A4 format and Landscape orientation */

        p.begin_page_ext(0, 0, "width=a4.height height=a4.width");


        p.fit_textline("A4 format in Landscape orientation.", 50, 50,

                       "font=" + font + " fontsize=20");

        p.end_page_ext("");


        /* Create a page in Letter format and Portrait orientation */

        p.begin_page_ext(0, 0, "width=letter.width height=letter.height");


        p.fit_textline("Letter format in Portrait orientation.", 50, 50,

                       "font=" + font + " fontsize=20");

        p.end_page_ext("");


        /* Create a page in Letter format and Landscape orientation */

        p.begin_page_ext(0, 0, "width=letter.height height=letter.width");


        p.fit_textline("Letter format in Landscape orientation.", 50, 50,

                       "font=" + font + " fontsize=20");

        p.end_page_ext("");


        /* Create a page with a size according to the dimensions of

         * the image to be placed on the page

         */

        image = p.load_image("auto", imagefile, "");

        if (image == -1)

            throw new Exception("Error: " + p.get_errmsg());


        /* Get the width and height of the image */

        imagewidth = p.get_value("imagewidth", 0.0);

        imageheight = p.get_value("imageheight", 0.0);


        /* Start the page with the size of the image to be fit on the page */

        p.begin_page_ext(imagewidth, imageheight, "");


        /* Fit the image */

        p.fit_image(image, 0, 0, "");


        p.close_image(image);

        p.end_page_ext("");

        p.end_document("");


        } catch (PDFlibException e) {

            System.err.print("PDFlib exception occurred:\n");

            System.err.print("[" + e.get_errnum() + "] " + e.get_apiname() +

                ": " + e.get_errmsg() + "\n");

        } catch (Exception e) {

            System.err.println(e.getMessage());

        } finally {

            if (p != null) {

                p.delete();

            }

        }

    }

}