java printerjob打印_java PrinterJob不打印以适合纸张

确保您首先翻译图形上下文以适应可想象区域…

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

(int) pageFormat.getImageableY());

接下来,确保使用PageFormat的imageableWidth和imageableHeight

double width = pageFormat.getImageableWidth();

double height = pageFormat.getImageableHeight();

而不是宽度/高度属性.许多这些东西都是从不同的背景下翻译出来的……

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

getImageableWidth / Height返回页面方向上下文中的页面大小

打印几乎假设dpi为72(不要压力,打印API可以处理更高的分辨率,但核心API假设为72dpi)

这意味着10x15cm的页面应转换为283.46456664×425.19684996像素.您可以使用System.out.println验证此信息,并将getImageableWidth / Height的结果转储到控制台.

如果您获得不同的设置,Java可能会覆盖默认页面属性

例如…

你有两个选择……

你可以…

显示PrintDialog并确保选择了正确的页面设置

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();

}

}

或者你可以……

只需手动手动设置纸张/页面值…

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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值