简体   繁体   中英

Printing a bufferedImage has a margin of 3 inches

I'm trying to print a bufferedImage to my printer. For some reason the print starts 3 inches from the left edge of the label and continous on the second label, instead of using one label only. According to my printer setup there are no margins at all. Do I miss something in order to start the printing without any margins? Here is my code:

 private BufferedImage image;

 public void print(int bufWidth, int bufHeight, BufferedImage bufImage, int printerId, String printerName)
 {
   _log.debugDetailPrefixed("printerHandler", "print()", "ImageWidth", bufImage.getWidth(), "ImageHeight", bufImage.getHeight());

   try
   {
     image = bufImage;
     PrinterJob printerJob = getPrinterJob(printerName);

     int width = bufImage.getWidth();
     int height = bufImage.getHeight();

 PageFormat pf = printerJob.defaultPage();
 Paper paper = pf.getPaper();
 paper.setSize(width, height);

 double imageableX = fromCMToPPI(0.1);
 double imageableY = fromCMToPPI(0.1);
 double imageableWidth = width - fromCMToPPI(0.1);
 double imageableHeight = height - fromCMToPPI(0.1);
 paper.setImageableArea(imageableX, imageableY, imageableWidth, imageableHeight);

 pf.setOrientation(PageFormat.LANDSCAPE);
 pf.setPaper(paper);
 PageFormat validatePage = printerJob.validatePage(pf);

 printerJob.setPrintable(new MyPrintable(), validatePage);

 printerJob.print();
   }
   catch (Exception ex)
   {
     _log.errorPrefixed("printerTask", "print", "Exception", ex.getMessage());
     ex.printStackTrace();
   }
 }

 public static final float DPI = 300;

 protected double fromPPItoCM(double dpi) {
   return dpi / DPI / 0.393700787;
 }

 protected double fromCMToPPI(double cm) {
   return toPPI(cm * 0.393700787);
 }

 protected double toPPI(double inch) {
   return inch * DPI;
 }

  public class MyPrintable implements Printable {

  @Override
  public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
   int result = NO_SUCH_PAGE;
   if (pageIndex < 1) {
       Graphics2D g2d = (Graphics2D) graphics;

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

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

       //Get the relation between the label width and image width:
       double sx = width /image.getWidth();
       //Get the relation between the label height and image height:
       double sy = height/image.getHeight();

       //Use the relation to scale the image to the printer
       g2d.scale(sx,sy);

       g2d.drawImage(image, 0, 0, null);
       result = PAGE_EXISTS;
   }
    return result;
  }
 }

The defaultPage() PageFormat usually has a margin. You need to change it to remove that margin using the setImageableArea() method.

So I guess, adding paper.setImageableArea(0, 0, width, height); will solves the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM