本文介绍如何使用spire.doc for java 提供的printerjob类打印word文档。通过该类,可以设置打印纸张大小,打印份数,是否弹出打印对话框等,且使用该方法打印出来的文档清晰度更高。
import com.spire.doc.*;
import java.awt.print.*;
public class wordprint {
public static void main(string[] args) throws exception {
//加载文档
document doc = new document();
doc.loadfromfile("sample.docx");
printerjob loprinterjob = printerjob.getprinterjob();
pageformat lopageformat = loprinterjob.defaultpage();
//设置打印纸张大小
paper lopaper = lopageformat.getpaper();
lopaper.setsize(600, 500);
lopageformat.setpaper(lopaper);
//删除默认页边距
lopaper.setimageablearea(0, 0, lopageformat.getwidth(), lopageformat.getheight());
//设置打印份数
loprinterjob.setcopies(1);
loprinterjob.setprintable(doc, lopageformat);
//设置打印对话框
if (loprinterjob.printdialog()) {
//执行打印
try {
loprinterjob.print();
} catch (printerexception e)
{
e.printstacktrace();
}
}
}
}