Results 1 to 2 of 2
  1. #1
    joelBeaton is offline Member
    Join Date
    Mar 2012
    Location
    England
    Posts
    13
    Rep Power
    0

    Default Printing Multiple page Documents to A4

    Hi All,

    Looking for a little help on how to print something from my GUI that is a potentially multi page out-put. Relatively new to Java programming (This being my first real project) but I would say I am at an intermediate level.

    The issues that I am having are:

    A) My GUIs DO print to a default size that is just smaller than A4 but with a WIDE margin at the top and left of the page.
    B) My text runs off the left of the page and does not wrap.
    C) If there is more than one page - it does not print (only the first page prints out).

    On C - I know this is because of the line

    if (page > 0)
    {
    return NO_SUCH_PAGE;
    }

    in the code below. But If I remove that condition literally THOUSANDS of pages try to print off, which is even worse.

    It looks like the pageable interface might be the way to go but I cant find ANY examples of this and I found the API hard to understand. Printing in Java does not seem to be a well dealt with subject? And I have written all my GUI code in swing.


    On how I implemented the printing function so far - first of all I created this class to deal with the printing function:

    Java Code:
    package backgroundclasses;
    
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.print.PageFormat;
    import java.awt.print.Paper;
    import java.awt.print.Printable;
    import java.awt.print.PrinterException;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.StringReader;
    
    /**
     * A simple class to handle printing to paper. 
     * @author Joel
     */
    public class PrintFunction implements Printable
    {
    
        private String printData;
    
        public PrintFunction(String printDataIn)
        {
            this.printData = printDataIn;
        }
    
        @Override
        public int print(Graphics g, PageFormat pf, int page) throws PrinterException
        {
            //If the print job should only have one page, uncomment the below.
            if (page > 0)
            {
                return NO_SUCH_PAGE;
            }
    
            // Adding the "Imageable" to the x and y puts the margins on the page.
            // To make it safe for printing.
    
            Graphics2D g2d = (Graphics2D) g;
            int x = (int) pf.getImageableX();
            int y = (int) pf.getImageableY();
            g2d.translate(x, y);
    
            // Calculate the line height
            Font font = new Font("Serif", Font.PLAIN, 12);
            FontMetrics metrics = g.getFontMetrics(font);
            int lineHeight = metrics.getHeight();
    
            BufferedReader br = new BufferedReader(new StringReader(printData));
    
            // Draw the page:
            try
            {
                String line;
                // Just a safety net in case no margin was added.
                x += 50;
                y += 50;
                while ((line = br.readLine()) != null)
                {
                    y += lineHeight;
                    g2d.drawString(line, x, y);
                }
            }
            catch (IOException e)
            {
                //
            }
    
            return PAGE_EXISTS;
        }
    }
    Then for each of the GUIs that call a print method the method written is:

    Java Code:
        /**
         * Method to print the contents of the Project Prepare Program to a
         * Printer (Paper)
         */
        private void printToPrinter()
        {
            String eol = System.getProperty("line.separator");
    
            // COMPLILE THE DATA TO BE PRINTED.
    
            String printData = "Project Prepare Program for: " + this.pp
                    + eol + eol
                    + "Program Information:" + eol + eol
                    + this.programInformationTextArea.getText() + eol + eol
                    + this.targetsTextArea.getText();
    
            PrinterJob job = PrinterJob.getPrinterJob();
    
            // Set the Page to A4 size
            
            PageFormat pf = new PageFormat();
            Paper paperSize = new Paper();
            paperSize.setSize(594.936, 841.536);
            pf.setOrientation(PageFormat.PORTRAIT);
    
            job.setPrintable(new PrintFunction(printData));
            boolean doPrint = job.printDialog();
            if (doPrint)
            {
                try
                {
                    job.print();
                }
                catch (PrinterException e)
                {
                    JOptionPane.showMessageDialog(frame,
                        "Ooops! There seems to have been an error while printing.");
                }
            }
        }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Printing Multiple page Documents to A4

    Moved from Advanced Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 0
    Last Post: 12-26-2011, 03:52 PM
  2. About printing Arraylist on JSP page....
    By vaibhavspawar in forum Advanced Java
    Replies: 0
    Last Post: 08-13-2010, 06:52 AM
  3. Printing only one page problem
    By AndreiDMS in forum Java 2D
    Replies: 0
    Last Post: 10-09-2009, 03:03 AM
  4. Printing contents of a web page
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-26-2007, 12:37 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •