java PrinterJob无法打印以适合纸张
我目前在使用默认打印机打印jpeg文件时陷入困境。在我的程序中,当我从文件夹中选择图像时,我需要使用打印机默认设置(纸张尺寸,边距,方向)进行打印。
目前我得到这个:
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();
现在,我的打印机的默认大小设置为:10 x 15厘米(4 x 6英寸),但是当我将程序设置为打印给定图像时,它仅显示一小部分纸张。
请帮帮我。
回答:
首先,请确保您将Graphics
上下文翻译成适合可成像的区域…
g2d.translate((int) pageFormat.getImageableX(), (int) pageFormat.getImageableY());
接下来,请确保您使用的是imageableWidth
和imageableHeight
的PageFormat
double width = pageFormat.getImageableWidth();double height = pageFormat.getImageableHeight();
而不是width
/ height
属性。其中许多东西都是从不同的上下文中翻译过来的…
graphics.drawImage(image, 0, 0, (int)width, (int)height, null);
该getImageableWidth/Height
页面方向的范围内返回页面大小
打印几乎都假设dpi为72(不要强调,打印API可以处理更高的分辨率,但是核心API假设72dpi)
这意味着10x15cm的页面应转换为283.46456664x425.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;
}
以上是 java PrinterJob无法打印以适合纸张 的全部内容, 来源链接: utcz.com/qa/421202.html