Print in Java: page format and document : Print « 2D Graphics GUI « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » 2D Graphics GUI » PrintScreenshots 
Print in Java: page format and document
   


/**
 * Class: Example3
 * <p>
 
 @author Jean-Pierre Dube <[email protected]>
 @version 1.0
 @since 1.0
 */

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterJob;

public class JavaWorldPrintExample3 {
  public static void main(String[] args) {

    JavaWorldPrintExample3 example3 = new JavaWorldPrintExample3();
    System.exit(0);
  }

  //--- Private instances declarations
  private final static int POINTS_PER_INCH = 72;

  /**
   * Constructor: Example3
   * <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();
      }
    }

  }

  /**
   * Class: IntroPage
   * <p>
   
   * This class defines the painter for the cover page by implementing the
   * Printable interface.
   * <p>
   
   @author Jean-Pierre Dube <[email protected]>
   @version 1.0
   @since 1.0
   @see Printable
   */
  private class IntroPage implements Printable {

    /**
     * Method: print
     * <p>
     
     @param g
     *            a value of type Graphics
     @param pageFormat
     *            a value of type PageFormat
     @param page
     *            a value of type int
     @return a value of type int
     */
    public int print(Graphics g, PageFormat pageFormat, int page) {

      //--- Create the Graphics2D object
      Graphics2D g2d = (Graphics2Dg;

      //--- Translate the origin to 0,0 for the top left corner
      g2d.translate(pageFormat.getImageableX(), pageFormat
          .getImageableY());

      //--- Set the default drawing color to black
      g2d.setPaint(Color.black);

      //--- Draw a border arround the page
      Rectangle2D.Double border = new Rectangle2D.Double(00, pageFormat
          .getImageableWidth(), pageFormat.getImageableHeight());
      g2d.draw(border);

      //--- Print the title
      String titleText = "Printing in Java Part 2";
      Font titleFont = new Font("helvetica", Font.BOLD, 36);
      g2d.setFont(titleFont);

      //--- Compute the horizontal center of the page
      FontMetrics fontMetrics = g2d.getFontMetrics();
      double titleX = (pageFormat.getImageableWidth() 2)
          (fontMetrics.stringWidth(titleText2);
      double titleY = * POINTS_PER_INCH;
      g2d.drawString(titleText, (inttitleX, (inttitleY);

      return (PAGE_EXISTS);
    }
  }

  /**
   * Class: Document
   * <p>
   
   * This class is the painter for the document content.
   * <p>
   
   
   @author Jean-Pierre Dube <[email protected]>
   @version 1.0
   @since 1.0
   @see Printable
   */
  private class Document implements Printable {

    /**
     * Method: print
     * <p>
     
     @param g
     *            a value of type Graphics
     @param pageFormat
     *            a value of type PageFormat
     @param page
     *            a value of type int
     @return a value of type int
     */
    public int print(Graphics g, PageFormat pageFormat, int page) {

      //--- Create the Graphics2D object
      Graphics2D g2d = (Graphics2Dg;

      //--- Translate the origin to 0,0 for the top left corner
      g2d.translate(pageFormat.getImageableX(), pageFormat
          .getImageableY());

      //--- Set the drawing color to black
      g2d.setPaint(Color.black);

      //--- Draw a border arround the page using a 12 point border
      g2d.setStroke(new BasicStroke(12));
      Rectangle2D.Double border = new Rectangle2D.Double(00, pageFormat
          .getImageableWidth(), pageFormat.getImageableHeight());

      g2d.draw(border);

      //--- Print page 1
      if (page == 1) {
        //--- Print the text one inch from the top and laft margins
        g2d.drawString("This the content page of page: " + page,
            POINTS_PER_INCH, POINTS_PER_INCH);
        return (PAGE_EXISTS);
      }

      //--- Print page 2
      else if (page == 2) {
        //--- Print the text one inch from the top and laft margins
        g2d.drawString("This the content of the second page: " + page,
            POINTS_PER_INCH, POINTS_PER_INCH);
        return (PAGE_EXISTS);
      }

      //--- Validate the page
      return (NO_SUCH_PAGE);

    }
  }

// Example3


           
         
    
    
  
Related examples in the same category
1. The Printing code which implements Printable
2. Print an Image to print directly
3. Simplest SWT Print ExampleSimplest SWT Print Example
4. Print in Java 2: PrinterJob
5. Print in Java: Multi page
6. Print in Java 5
7. Print in Java 6
8. Simple Book for printingSimple Book for printing
9. Shapes PrintShapes Print
10. Display the print dialog and print
11. Print the printable area outlinePrint the printable area outline
12. Print the text file and print preview themPrint the text file and print preview them
13. Printable demoPrintable demo
14. Print Swing componentsPrint Swing components
15. BookBook
16. Another print demoAnother print demo
17. Book demoBook demo
18. Printing the Combined-Java 1.2-and-1.4 WayPrinting the Combined-Java 1.2-and-1.4 Way
19. Printing the Java 1.4 Way
20. Prompting for a Printer
21. Printing the Java 1.1 WayPrinting the Java 1.1 Way
22. ScribbleScribble
23. Printable Document
24. PrintFile -- Print a file named on the command linePrintFile -- Print a file named on the command line
25. Print to the standard output
26. PrintPanel is the base for an open-ended series of classesPrintPanel is the base for an open-ended series of classes
27. Print Demo: BookPrint Demo: Book
28. Print Test Print Test
29. Pageable TextPageable Text
30. The area of the printable area
31. The area of the actual page
32. Printing Pages with Different Formats
33. Setting the Orientation of a Printed Page
34. Print Dialog: change the default printer settings(default printer, number of copies, range of pages)
35. Printing to a File
36. Listening for Print Service Status Changes
37. Print Image
38. Overriding the Default Action of a JTextComponent
39. Displaying the Page Format Dialog: changes the default page format such as orientation and paper size.
40. Printable Component
41. Create PageFormats on a higher level
www.___j__a_v___a2_s__._c__om | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.