problem displaying print margins in dialog
Goal : to get the correct print margins to display in the printDialog
Hi,
i am trying to pass a MediaPrintableArea request to a printdialog via a printRequestattribute and i am getting the wrong values displayed in the dialog.
I can get the margins to properly adjust on the actual printed paper, that is if i set the margins to x=5;y=5 the paper that is printed has the correct margins of 5mm and 5mm.
However the dialog shows 4.06 and 4.06 mm, i have tried this a bunch of different ways with no result i have included working demo code below and help would be greatly appreciated
you can display debug info by setting the boolean print variable at the top of the driver class
thankyou
Code:
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Media;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.OrientationRequested;
import javax.print.attribute.standard.PageRanges;
import model.MainPanelPrinter;
import view.DisplayFrame;
import view.MainPanel;
public class Driver {
private static MainPanel mainPanel;
static boolean PRINT_SETTINGS_BEFORE_ADJUSTMENT = true;
static boolean PRINT_SETTINGS_AFTER_ADJUSTMENT = true;
static boolean PRINT_MEDIA_SETTINGS = false;
static double x_margin_user_setting = 0;
static double y_margin_user_setting = 0;
static double paper_x_margin__after_adjustment = 0;
static double paper_y_margin__after_adjustment = 0;
static int paper_x_co_ord__after_adjustment = 0;
static int paper_y_co_ord__after_adjustment = 0;
/**
* @param args
*/
public static void main(String[] args) {
// show what we are attempting to print
MainPanel panel = new MainPanel();
DisplayFrame frame = new DisplayFrame(panel);
// create a prinatble which will draw onto the printers graphics Object
MainPanelPrinter printable = new MainPanelPrinter(mainPanel);
// create a set of printRequests to go to the printer and the printer dialog
PrintRequestAttributeSet printRequestAttributes = new HashPrintRequestAttributeSet();
printRequestAttributes.add(OrientationRequested.PORTRAIT);
printRequestAttributes.add(new PageRanges(1,1));
printRequestAttributes.add(MediaSizeName.ISO_A4);
// create a printer job
PrinterJob printerJob = PrinterJob.getPrinterJob();
// get the default page fromat from the printer job
PageFormat pageFormat = printerJob.defaultPage();
// create a paper object to manipulate
Paper paper = new Paper();
if(PRINT_SETTINGS_BEFORE_ADJUSTMENT)
printPaperInfo_BeforAdjustments(paper);
x_margin_user_setting = 5;
y_margin_user_setting = 5;
/**
* WORKS
*
* set the margins on the paper
* this WORKS and the margins will be reduced
* and the results are as expected
*
*
*/
paper.setImageableArea(x_margin_user_setting, y_margin_user_setting, paper.getWidth() - x_margin_user_setting * 2, paper.getHeight()
- y_margin_user_setting * 2);
// add the paper adjsutements to the pageformat
pageFormat.setPaper(paper);
// add the page format to the printer job with the printable
printerJob.setPrintable(printable, pageFormat);
if(PRINT_SETTINGS_BEFORE_ADJUSTMENT)
printPaperInfo_AfterAdjustment(paper,x_margin_user_setting,y_margin_user_setting);
paper_x_margin__after_adjustment = paper.getWidth()- paper.getImageableWidth();
paper_y_margin__after_adjustment = paper.getHeight() - paper.getImageableHeight();
if(PRINT_SETTINGS_AFTER_ADJUSTMENT)
printMarginInfo(paper_x_margin__after_adjustment,paper_y_margin__after_adjustment);
/**
* NOT WORKING
*
* attempting to add the printable area to the print request attributes to display in print dialog
*
* what i expect is margins in mm
*
* left = 5
* right = 5
*
* what i get is
*
* left = 4.06
* right = 4.06
* ???
*
*/
MediaPrintableArea mediaPrintableArea = new MediaPrintableArea((int)paper.getImageableX(),(int) paper.getImageableHeight(),(int) paper.getImageableWidth(),(int) paper.getImageableHeight(), MediaPrintableArea.MM);
// add the mediaPrinatble attribute to the print request
printRequestAttributes.add(mediaPrintableArea);
// pass the attributes to the print dialog
if (printerJob.printDialog(printRequestAttributes)) {
try {
if(PRINT_MEDIA_SETTINGS)
printMediaInformation(printerJob);
printerJob.print(printRequestAttributes);
} catch (PrinterException e) {
System.out.println(e);
}
}
}
private static void printMediaInformation(PrinterJob printerJob) {
Media[] res = (Media[])printerJob.getPrintService().getSupportedAttributeValues(Media.class, null,null);
MediaPrintableArea[] res2 = (MediaPrintableArea[])printerJob.getPrintService().getSupportedAttributeValues(MediaPrintableArea.class, null,null);
for(MediaPrintableArea item:res2)
{
System.out.println(item.getCategory().toString()+" the media items " + item.toString());
}
}
private static void printMarginInfo(double x_margin, double y_margin) {
System.out.println(" after x_margin total difference " + x_margin);
System.out.println(" after y_margin total difference " + y_margin);
System.out.println(" after x_margin single " + x_margin/2);
System.out.println(" after y_margin single " + y_margin/2);
}
private static void printPaperInfo_AfterAdjustment(Paper paper,
double x_margin_original, double y_margin_orginal) {
System.out.println(" margin passed to the dialog origin x_margin_ " + x_margin_original);
System.out.println(" margin passed to the dialog origin y_margin_ " + y_margin_orginal);
System.out.println(" after paper.getwidth() " + paper.getWidth());
System.out.println(" after paper.getwidth() " + paper.getHeight());
System.out.println(" after paper.getimagablewidth " + paper.getImageableWidth());
System.out.println(" after paper.getimageableheight " + paper.getImageableHeight());
System.out.println(" after paper.getImageableX() " + paper.getImageableX());
System.out.println(" after paper.getImageableY " + paper.getImageableY());
}
private static void printPaperInfo_BeforAdjustments(Paper paper) {
System.out.println(" before the setting x_origin " + paper.getImageableX());
System.out.println(" before the setting y_origin " + paper.getImageableY());
System.out.println(" before the setting paper.getWidth() " + paper.getWidth());
System.out.println(" before the setting paper.getHeight() " + paper.getHeight());
System.out.println(" before the setting getImageableWidth " + paper.getImageableWidth());
System.out.println(" before the setting getImageableHeight " + paper.getImageableHeight());
}
}
printer's painter
Code:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import view.MainPanel;
public class MainPanelPrinter implements Printable{
private MainPanel mainPanel;
public MainPanelPrinter(MainPanel mainPanel) {
this.mainPanel = mainPanel;
}
@Override
public int print(Graphics g, PageFormat pageFormat, int index)
throws PrinterException {
Graphics2D g2d = (Graphics2D) g;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
if(index==0)
{
mainPanel.paintForPrinting(g2d);
return Printable.PAGE_EXISTS;
}
else
return Printable.NO_SUCH_PAGE;
}
}
display frame
Code:
import java.awt.Dimension;
import java.awt.Point;
import javax.swing.JFrame;
public class DisplayFrame extends JFrame {
public DisplayFrame(MainPanel mainPanel)
{
super("DisplayFrame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(new Dimension(400,400));
setLocation(new Point(200,200));
add(mainPanel);
setVisible(true);
}
}
main panel
Code:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class MainPanel extends JPanel{
private Dimension currentSize;
private int x_margin = 10;
private int y_margin = 10;
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
currentSize = this.getSize();
g2d.drawRect(x_margin, y_margin, currentSize.width- 2*x_margin, currentSize.height - 2*y_margin);
g2d.drawString("Hey buddy",currentSize.width/2 -15, currentSize.height/2);
}
public void paintForPrinting(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
currentSize = this.getSize();
g2d.drawRect(x_margin, y_margin, currentSize.width- 2*x_margin, currentSize.height - 2*y_margin);
g2d.drawString("Hey buddy",currentSize.width/2 -15, currentSize.height/2);
}
}
Re: problem displaying print margins in dialog
is this a bad post ? ie too much code ?
Re: problem displaying print margins in dialog
Hello simo_mon,
This is what I'm getting when I run your program:
before the setting x_origin 72.0
before the setting y_origin 72.0
before the setting paper.getWidth() 612.0
before the setting paper.getHeight() 792.0
before the setting getImageableWidth 468.0
before the setting getImageableHeight 648.0
Is this what you get?
Stephen