好的,所以我刚刚开始研究一个可以打印出其图形的程序。我的几乎与位于此处 http://docs.oracle.com/javase/tutorial/2d/printing/examples/HelloWorldPrinter.java的 Oracle 的版权相同
所以基本上我是一个完整的菜鸟,并试图弄清楚如何将我的页面设置为 8.5x11in 和 300dpi 但无济于事:( 在我所有失败的尝试之后,我什至没有关于这个主题的工作代码。我知道它有一些东西与
Paper.setSize()
andPrinterResolution
但我无法从 javadocs 中收集到足够的信息来理解这些。请帮助。
编辑:我相信我发现Paper.setSize(72*8.5,72*11);
将页面大小设置为 8.5x11 但 dpi 仍然是 72。这是我到目前为止的代码。
public int print(Graphics g, PageFormat pf, int page) throws
PrinterException {
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Paper pg = new Paper();
pg.setSize(72*8.5,72*11);
pf.setPaper(pg);
if (page > 0) { /* We have only one page, and 'page' is zero-based */
return NO_SUCH_PAGE;
}
/* User (0,0) is typically outside the imageable area, so we must
* translate by the X and Y values in the PageFormat to avoid clipping
*/
//Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
/* Now we perform our rendering */
g.drawString("Hello world! :D", 100, 100);
/* tell the caller that this page is part of the printed document */
return PAGE_EXISTS;
}