Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7904553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:07:54+00:00 2026-06-03T10:07:54+00:00

I’m trying to get the PageFormat correct when I print. Below is an example

  • 0

I’m trying to get the PageFormat correct when I print. Below is an example program that shows my dilemma: I get a different result when I use printJob.setPrintable(printable) than when I use printJob.setPageable(book) when I create a Book object using the default PageFormat from the Print job.

When I run it, and click “Print”, then “Print using Book”, I see this console output:

doPrint(false)
printing on 612.000000x792.000000 paper, imageable area=588.960000x768.960000
printing on 612.000000x792.000000 paper, imageable area=588.960000x768.960000
printing on 612.000000x792.000000 paper, imageable area=588.960000x768.960000
doPrint(true)
printing on 612.000000x792.000000 paper, imageable area=468.000000x648.000000
printing on 612.000000x792.000000 paper, imageable area=468.000000x648.000000

What gives? The default page format when using Book sucks and uses 1″ margin on each side of the page; the “real” page format only needs about 1/6″ margin on each side.

example program here:

package com.example.printing;

import java.awt.BasicStroke;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.geom.Rectangle2D;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import javax.swing.AbstractAction;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PrintRectangles extends JFrame 
{
    static final int Nrectangles = 3;

    static class RectangleThingy extends JPanel implements Printable
    {
        @Override public int print(Graphics graphics, 
                PageFormat pageFormat, 
                int pageIndex)
            throws PrinterException 
        {
            describePageFormat(pageFormat);
            if (pageIndex > 0) {
                return(NO_SUCH_PAGE);
            } 
            else {
                Graphics2D g2d = (Graphics2D)graphics;
                g2d.translate(pageFormat.getImageableX(), 
                        pageFormat.getImageableY());
                double w = pageFormat.getImageableWidth();
                double h = pageFormat.getImageableHeight();
                final int N = (Nrectangles - 1) / 2;
                final double spacing = 7.2; // 1/10 inch 
                g2d.setStroke(new BasicStroke(0.1f));
                for (int i = -N; i <= N; ++i)
                {
                    double dx = i*spacing;
                    Rectangle2D r = new Rectangle2D.Double(
                            dx, dx, w-2*dx, h-2*dx
                            );
                    g2d.draw(r);
                }
                Rectangle2D rthick = new Rectangle2D.Double(
                        0, 0, w, h
                        );
                g2d.setStroke(new BasicStroke(1.0f));
                g2d.draw(rthick);
                return(PAGE_EXISTS);
            }
        }

        private void describePageFormat(PageFormat pageFormat) {
            System.out.println(String.format("printing on %fx%f paper, imageable area=%fx%f",
                    pageFormat.getWidth(),
                    pageFormat.getHeight(),
                    pageFormat.getImageableWidth(),
                    pageFormat.getImageableHeight()
                    ));

        }
    }

    static private class PrintPreviewPanel extends JPanel
    {
        final private Printable p;
        final private PageFormat pageFormat;
        public PrintPreviewPanel(Printable p, PageFormat pf)
        {
            this.p = p;
            this.pageFormat = pf;
        }
        @Override public Dimension getPreferredSize() {
            return new Dimension((int)this.pageFormat.getWidth(), 
                    (int)this.pageFormat.getHeight());
        }
        @Override protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            try {
                p.print(g, this.pageFormat, 0);
            } catch (PrinterException e) {
                e.printStackTrace();
            }
        }       
    }

    public PrintRectangles(String title) {
        super(title);
        JPanel panel = new JPanel();
        setContentPane(panel);
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        JButton printButton = new JButton("Print");
        JButton printUsingBookButton = new JButton("Print using Book");
        JButton printPreviewButton = new JButton("Print preview");
        panel.add(printButton);
        panel.add(printUsingBookButton);
        panel.add(printPreviewButton);
        printButton.addActionListener(new AbstractAction("print") {
            @Override public void actionPerformed(ActionEvent e) {
                doPrint(false);
            }           
        });
        printUsingBookButton.addActionListener(new AbstractAction("printUsingBook") {
            @Override public void actionPerformed(ActionEvent e) {
                doPrint(true);
            }           
        });
        printPreviewButton.addActionListener(new AbstractAction("printPreview") {
            @Override public void actionPerformed(ActionEvent e) {
                doPrintPreview();
            }           
        });
    }

    protected void doPrint(boolean useBook) {
        RectangleThingy rectangleThingy = new RectangleThingy();
        System.out.println("doPrint("+useBook+")");
        try
        {
            PrinterJob printJob = PrinterJob.getPrinterJob();
            PageFormat pageFormat = printJob.getPageFormat(null);
            if (useBook)
            {
                Book book = new Book();
                book.append(rectangleThingy, pageFormat);               
                printJob.setPageable(book);
            }
            else
            {
                printJob.setPrintable(rectangleThingy);             
            }
            if (printJob.printDialog())
                printJob.print();
        } 
        catch(PrinterException pe) {
            System.out.println("Error printing: " + pe);
        }
    }

    protected void doPrintPreview() {
        RectangleThingy rt = new RectangleThingy();
        JFrame frame = new JFrame("print preview");

        // hack for now -- how do we get this from the printer?
        Paper paper = new Paper();
        double dotsperinch = 72;
        double margin = 0.125*dotsperinch;
        double w = 8.5*dotsperinch;
        double h = 11*dotsperinch;
        paper.setImageableArea(margin, margin, w-2*margin, h-2*margin);
        paper.setSize(w, h);
        PageFormat pfmt = new PageFormat();
        pfmt.setPaper(paper);
        frame.setContentPane(new PrintPreviewPanel(rt, pfmt));
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new PrintRectangles("PrintRectangles").start();
    }

    private void start() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T10:07:55+00:00Added an answer on June 3, 2026 at 10:07 am

    Hmm. After trying a number of fruitless efforts, it looks like setting a page to zero margin and then calling PrinterJob.validatePage() seems to be the only way I can get a valid minimum-margin PageFormat:

    static private PageFormat getMinimumMarginPageFormat(PrinterJob printJob) {
        PageFormat pf0 = printJob.defaultPage();
        PageFormat pf1 = (PageFormat) pf0.clone();
        Paper p = pf0.getPaper();
        p.setImageableArea(0, 0,pf0.getWidth(), pf0.getHeight());
        pf1.setPaper(p);
        PageFormat pf2 = printJob.validatePage(pf1);
        return pf2;     
    }
    

    and then I can change doPrint() to:

    protected void doPrint(boolean useBook) {
        RectangleThingy rectangleThingy = new RectangleThingy();
        System.out.println("doPrint("+useBook+")");
        try
        {
            PrinterJob printJob = PrinterJob.getPrinterJob();
            if (useBook)
            {
                Book book = new Book();
                book.append(rectangleThingy, getMinimumMarginPageFormat(printJob));
                printJob.setPageable(book);
            }
            else
            {
                printJob.setPrintable(rectangleThingy);        
            }
            if (printJob.printDialog())
                printJob.print();
        } 
        catch(PrinterException pe) {
            System.out.println("Error printing: " + pe);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.