Example usage for java.awt.print PageFormat PageFormat

List of usage examples for java.awt.print PageFormat PageFormat

Introduction

In this page you can find the example usage for java.awt.print PageFormat PageFormat.

Prototype

public PageFormat() 

Source Link

Document

Creates a default, portrait-oriented PageFormat .

Usage

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    PrinterJob pj = PrinterJob.getPrinterJob();
    Book book = new Book();
    PageFormat defaultFormat = new PageFormat();
    defaultFormat = pj.defaultPage(defaultFormat);
    PageFormat landscapeFormat = new PageFormat();
    landscapeFormat.setOrientation(PageFormat.LANDSCAPE);
    PagePrinter[] page = new PagePrinter[2];
    int pageWidth = (int) defaultFormat.getImageableWidth();
    int pageHeight = (int) defaultFormat.getImageableHeight();
    Font font = new Font("Helvetica", Font.BOLD, 18);
    page[0] = new PagePrinter();
    page[0].addPrintElement(new MyItem("AAA", font, 100, pageHeight / 2));
    page[0].addPrintElement(new MyItem("line", 0, pageHeight, pageWidth, pageHeight));

    page[1] = new PagePrinter();
    page[1].addPrintElement(new MyItem("rectangle", 100, 100, pageWidth - 200, pageHeight - 200));
    page[1].addPrintElement(new MyItem("oval", 120, 120, pageWidth - 240, pageHeight - 240));

    book.append(page[0], defaultFormat);
    book.append(page[1], landscapeFormat);

    pj.setPageable(book);//  ww  w .  j a  va  2  s. c  o  m
    pj.print();
}

From source file:JavaWorldPrintExample3.java

/**
 * Constructor: Example3/*w ww. j a  va 2s. co  m*/
 * <p>
 *  
 */
public JavaWorldPrintExample3() {

    //--- Create a new PrinterJob object
    PrinterJob printJob = PrinterJob.getPrinterJob();

    //--- Create a new book to add pages to
    Book book = new Book();

    //--- Add the cover page using the default page format for this print
    // job
    book.append(new IntroPage(), printJob.defaultPage());

    //--- Add the document page using a landscape page format
    PageFormat documentPageFormat = new PageFormat();
    documentPageFormat.setOrientation(PageFormat.LANDSCAPE);
    book.append(new Document(), documentPageFormat);

    //--- Add a third page using the same painter
    book.append(new Document(), documentPageFormat);

    //--- Tell the printJob to use the book as the pageable object
    printJob.setPageable(book);

    //--- Show the print dialog box. If the user click the
    //--- print button we then proceed to print else we cancel
    //--- the process.
    if (printJob.printDialog()) {
        try {
            printJob.print();
        } catch (Exception PrintException) {
            PrintException.printStackTrace();
        }
    }

}

From source file:JavaWorldPrintExample2.java

/**
 * Constructor: Example2/*from  www.  j a v  a  2 s .  c o m*/
 * <p>
 *  
 */
public JavaWorldPrintExample2() {

    //--- Create a new PrinterJob object
    PrinterJob printJob = PrinterJob.getPrinterJob();

    //--- Create a new book to add pages to
    Book book = new Book();

    //--- Add the cover page using the default page format for this print
    // job
    book.append(new IntroPage(), printJob.defaultPage());

    //--- Add the document page using a landscape page format
    PageFormat documentPageFormat = new PageFormat();
    documentPageFormat.setOrientation(PageFormat.LANDSCAPE);
    book.append(new Document(), documentPageFormat);

    //--- Tell the printJob to use the book as the pageable object
    printJob.setPageable(book);

    //--- Show the print dialog box. If the user click the
    //--- print button we then proceed to print else we cancel
    //--- the process.
    if (printJob.printDialog()) {
        try {
            printJob.print();
        } catch (Exception PrintException) {
            PrintException.printStackTrace();
        }
    }
}

From source file:JavaWorldPrintExample4.java

/**
 * Constructor: Example4/*from w  w w.  j a  v a  2 s .c om*/
 * <p>
 *  
 */
public JavaWorldPrintExample4() {

    //--- Create a new PrinterJob object
    PrinterJob printJob = PrinterJob.getPrinterJob();

    //--- Create a new book to add pages to
    Book book = new Book();

    //--- Add the cover page using the default page format for this print
    // job
    book.append(new IntroPage(), printJob.defaultPage());

    //--- Add the document page using a landscape page format
    PageFormat documentPageFormat = new PageFormat();
    documentPageFormat.setOrientation(PageFormat.LANDSCAPE);
    book.append(new Document(), documentPageFormat);

    //--- Tell the printJob to use the book as the pageable object
    printJob.setPageable(book);

    //--- Show the print dialog box. If the user click the
    //--- print button we then proceed to print else we cancel
    //--- the process.
    if (printJob.printDialog()) {
        try {
            printJob.print();
        } catch (Exception PrintException) {
            PrintException.printStackTrace();
        }
    }
}

From source file:playground.singapore.calibration.charts.CustomChartPanel.java

@Override
public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
    System.err.println("PRINTING");
    //Divide the current page format into sections based
    //on the layout instructions received in the constructor
    //a new pagelayout is created for each cell in the grid
    //that will then be passed along to the print method of
    //each chart panel.

    if (pageIndex != 0) {
        return NO_SUCH_PAGE;
    }//  w w w .j  a va2  s.  com

    List<PageFormat> pageFormats = new ArrayList<PageFormat>();

    //setup all the page formats needed for the grid cells.
    double x = pf.getImageableX();
    double y = pf.getImageableY();
    double cellWidth = pf.getImageableWidth() / layoutInstructions.getColumns();
    double cellHeight = pf.getImageableHeight() / layoutInstructions.getRows();

    for (int i = 1; i <= layoutInstructions.getRows(); i++) {
        double rowOffset = (i - 1) * cellHeight + y;
        for (int j = 1; j <= layoutInstructions.getColumns(); j++) {
            PageFormat format = new PageFormat();
            Paper paper = new Paper();
            double columnOffset = (j - 1) * cellWidth + x;
            paper.setImageableArea(columnOffset, rowOffset, cellWidth, cellHeight);
            format.setPaper(paper);
            pageFormats.add(format);
        }
    }

    //have each chartpanel print on the graphics context using its
    //particular PageFormat
    int size = Math.min(pageFormats.size(), panels.size());
    for (int i = 0; i < size; i++) {
        panels.get(i).print(g, pageFormats.get(i), pageIndex);

    }

    return PAGE_EXISTS;
}

From source file:playground.artemc.calibration.charts.CustomChartPanel.java

@Override
public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
    System.err.println("PRINTING");
    //Divide the current page format into sections based
    //on the layout instructions received in the constructor
    //a new pagelayout is created for each cell in the grid
    //that will then be passed along to the print method of
    //each chart panel.  

    if (pageIndex != 0) {
        return NO_SUCH_PAGE;
    }/*from   w w  w.  j a v a2s  .com*/

    List<PageFormat> pageFormats = new ArrayList<PageFormat>();

    //setup all the page formats needed for the grid cells.
    double x = pf.getImageableX();
    double y = pf.getImageableY();
    double cellWidth = pf.getImageableWidth() / layoutInstructions.getColumns();
    double cellHeight = pf.getImageableHeight() / layoutInstructions.getRows();

    for (int i = 1; i <= layoutInstructions.getRows(); i++) {
        double rowOffset = (i - 1) * cellHeight + y;
        for (int j = 1; j <= layoutInstructions.getColumns(); j++) {
            PageFormat format = new PageFormat();
            Paper paper = new Paper();
            double columnOffset = (j - 1) * cellWidth + x;
            paper.setImageableArea(columnOffset, rowOffset, cellWidth, cellHeight);
            format.setPaper(paper);
            pageFormats.add(format);
        }
    }

    //have each chartpanel print on the graphics context using its
    //particular PageFormat
    int size = Math.min(pageFormats.size(), panels.size());
    for (int i = 0; i < size; i++) {
        panels.get(i).print(g, pageFormats.get(i), pageIndex);

    }

    return PAGE_EXISTS;
}

From source file:de.dfki.owlsmx.gui.ResultVisualization.java

public void PrintGraphics2DToPS(String path) throws IOException {
    try {/*  w  w  w  .j  a  v a2 s.  c o m*/
        //TODO implement saving to PS
        GUIState.displayWarning("Incomplete feature",
                "This feature is still under heavy development and can not yet be used");
        print2DtoPS printer = new print2DtoPS(path, chartPanel, graphPrintHeight, graphPrintHeight);
        printer.print(this.createRPPanel().getGraphics(), new PageFormat(), 0);
    } catch (PrinterException pe) {
        GUIState.displayWarning(pe.getClass().toString(), "Couldn't create PS file!\n" + pe.getMessage());
    }
}

From source file:PageFormatFactory.java

/**
 * Creates a new pageformat using the given paper and the given orientation.
 *
 * @param paper       the paper to use in the new pageformat
 * @param orientation one of PageFormat.PORTRAIT, PageFormat.LANDSCAPE or PageFormat.REVERSE_LANDSCAPE
 * @return the created Pageformat/*from ww w  .j  a  v  a 2s. c  om*/
 * @throws NullPointerException if the paper given was null
 */
public PageFormat createPageFormat(final Paper paper, final int orientation) {
    if (paper == null) {
        throw new NullPointerException("Paper given must not be null");
    }
    final PageFormat pf = new PageFormat();
    pf.setPaper(paper);
    pf.setOrientation(orientation);
    return pf;
}

From source file:PageFormatFactory.java

/**
 * Restores a page format after it has been serialized.
 *
 * @param data the serialized page format data.
 * @return the restored page format.//  w  ww.j a  v  a 2s. c  o m
 * @deprecated This functionality is part of JCommon-Serializer
 */
public PageFormat createPageFormat(final Object[] data) {
    final Integer orientation = (Integer) data[0];
    final float[] dim = (float[]) data[1];
    final float[] rect = (float[]) data[2];
    final Paper p = new Paper();
    p.setSize(dim[0], dim[1]);
    p.setImageableArea(rect[0], rect[1], rect[2], rect[3]);
    final PageFormat format = new PageFormat();
    format.setPaper(p);
    format.setOrientation(orientation.intValue());
    return format;
}

From source file:com.servoy.j2db.util.Utils.java

/**
 * Create a PageFormat object from the dimensions and margins.
 *
 * @param width the actual paper width - ignoring orientation. It is the width of the paper as seen by the printer.
 * @param height the actual paper height - ignoring orientation. It is the height of the paper as seen by the printer.
 * @param lm left margin of the page, not paper. So this is the left margin affected by orientation, as used in application.
 * @param rm right margin of the page, not paper. So this is the right margin affected by orientation, as used in application.
 * @param tm top margin of the page, not paper. So this is the top margin affected by orientation, as used in application.
 * @param bm bottom margin of the page, not paper. So this is the bottom margin affected by orientation, as used in application.
 * @param orientation the orientation of the page. Establishes a relation between page and paper coordinates.
 * @param units INCHES or MM./* w w w.j a va2  s.c om*/
 * @return the required PageFormat object.
 */
public static PageFormat createPageFormat(double width, double height, double lm, double rm, double tm,
        double bm, int orientation, int units) {
    double pixWidth = convertPageFormatUnit(units, Size2DSyntax.INCH, width) * PPI;
    double pixHeight = convertPageFormatUnit(units, Size2DSyntax.INCH, height) * PPI;
    double pixLm = convertPageFormatUnit(units, Size2DSyntax.INCH, lm) * PPI;
    double pixRm = convertPageFormatUnit(units, Size2DSyntax.INCH, rm) * PPI;
    double pixTm = convertPageFormatUnit(units, Size2DSyntax.INCH, tm) * PPI;
    double pixBm = convertPageFormatUnit(units, Size2DSyntax.INCH, bm) * PPI;

    // The margins of the Paper object are relative to the physical paper, so independent
    // of the text orientation; The PageFormat object takes the orientation into account relative to the text.
    // We have to convert back to the paper-relative margins here...
    double paperLm;
    double paperRm;
    double paperTm;
    double paperBm;
    if (orientation == PageFormat.LANDSCAPE) {
        paperLm = pixTm;
        paperRm = pixBm;
        paperTm = pixRm;
        paperBm = pixLm;
    } else if (orientation == PageFormat.PORTRAIT) {
        paperLm = pixLm;
        paperRm = pixRm;
        paperTm = pixTm;
        paperBm = pixBm;
    } else
    // orientation == PageFormat.REVERSE_LANDSCAPE
    {
        paperLm = pixBm;
        paperRm = pixTm;
        paperTm = pixLm;
        paperBm = pixRm;
    }

    PageFormat pf = new PageFormat();
    pf.setOrientation(orientation);
    Paper paper = new Paper();
    paper.setSize(pixWidth, pixHeight);
    paper.setImageableArea(paperLm, paperTm, pixWidth - (paperLm + paperRm), pixHeight - (paperTm + paperBm));
    pf.setPaper(paper);

    return pf;
}