Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

java PrinterJob not printing to fit paper

i am stuck currently when printing a jpeg file with the default printer. In my program when i select an image from a folder, i need to print it using the printer default settings (paper size, margins, orientation).

Currently i got this:

PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
final BufferedImage image = ImageIO.read(new File("car.jpg"));

PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintService(printService);
printJob.setPrintable(new Printable(){
  @Override
  public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException{
      if (pageIndex == 0) {
          graphics.drawImage(image, 0, 0, (int)pageFormat.getWidth(), (int)pageFormat.getHeight(), null);
          return PAGE_EXISTS;
      else return NO_SUCH_PAGE;
  }
}

printJob.print();

The default settings for my printer right now for size is: 10 x 15 cm (4 x 6 in) but when i set my program to print the given image, it displays only a small section of the paper.

Please help me out.

EDIT

thanks everyone for their help, i managed to find the answer posted by another user at Borderless printing

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Make sure that you are, first, translating the Graphics context to fit within inthe imagable area...

g2d.translate((int) pageFormat.getImageableX(),
              (int) pageFormat.getImageableY());

Next, make sure you are using the imageableWidth and imageableHeight of the PageFormat

double width = pageFormat.getImageableWidth();
double height = pageFormat.getImageableHeight();

and not the width/height properties. Many of these things get translated from different contexts...

graphics.drawImage(image, 0, 0, (int)width, (int)height, null);

The getImageableWidth/Height returns the page size within the context of the page orientation

Printing pretty much assumes a dpi of 72 (don't stress, the printing API can handle much higher resolutions, but the core API assumes 72dpi)

This means that a page of 10x15cm should translate to 283.46456664x425.19684996 pixels. You can verify this information by using a System.out.println and dumping the results of getImageableWidth/Height to the console.

If you're getting different settings, it's possible that Java has overridden the default page properties

For example...

You have two choices...

You could...

Show the PrintDialog and ensure that the correct page settings are selected

PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new PrinterResolution(300, 300, PrinterResolution.DPI));
aset.add(new MediaPrintableArea(0, 0, 150, 100, MediaPrintableArea.MM));

PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(new PrintTask()); // You Printable here

if (pj.printDialog(aset)) {
    try {
        pj.print(aset);
    } catch (PrinterException ex) {
        ex.printStackTrace();
    }
}

Or you could...

Just manually set the paper/page values manually...

public static void main(String[] args) {
    PrinterJob pj = PrinterJob.getPrinterJob();
    PageFormat pf = pj.defaultPage();
    Paper paper = pf.getPaper();
    // 10x15mm
    double width = cmsToPixel(10, 72);
    double height = cmsToPixel(15, 72);
    paper.setSize(width, height);
    // 10 mm border...
    paper.setImageableArea(
                    cmsToPixel(0.1, 72),
                    cmsToPixel(0.1, 72),
                    width - cmsToPixel(0.1, 72),
                    height - cmsToPixel(0.1, 72));
    // Orientation
    pf.setOrientation(PageFormat.PORTRAIT);
    pf.setPaper(paper);
    PageFormat validatePage = pj.validatePage(pf);
    pj.setPrintable(new Printable() {
        @Override
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            // Your code here
            return NO_SUCH_PAGE;
        }

    },  validatePage);
    try {
        pj.print();
    } catch (PrinterException ex) {
        ex.printStackTrace();
    }
}

// The number of CMs per Inch
public static final double CM_PER_INCH = 0.393700787d;
// The number of Inches per CMs
public static final double INCH_PER_CM = 2.545d;
// The number of Inches per mm's
public static final double INCH_PER_MM = 25.45d;

/**
 * Converts the given pixels to cm's based on the supplied DPI
 *
 * @param pixels
 * @param dpi
 * @return
 */
public static double pixelsToCms(double pixels, double dpi) {
    return inchesToCms(pixels / dpi);
}

/**
 * Converts the given cm's to pixels based on the supplied DPI
 *
 * @param cms
 * @param dpi
 * @return
 */
public static double cmsToPixel(double cms, double dpi) {
    return cmToInches(cms) * dpi;
}

/**
 * Converts the given cm's to inches
 *
 * @param cms
 * @return
 */
public static double cmToInches(double cms) {
    return cms * CM_PER_INCH;
}

/**
 * Converts the given inches to cm's
 *
 * @param inch
 * @return
 */
public static double inchesToCms(double inch) {
    return inch * INCH_PER_CM;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...