spire.pdf for java支持在java应用程序中打印pdf文档。本文将介绍以下几种常见方式打印pdf文档:
- 静默打印
- 显示打印对话框打印
- 打印pdf时自定义纸张大小
静默打印即使用默认打印机直接打印pdf文档。打印时,我们可以设置打印份数,设置纸张打印页边距等。
import com.spire.pdf.*;
import java.awt.print.*;
public class print {
public static void main(string[] args) {
//加载文档
pdfdocument pdf = new pdfdocument();
pdf.loadfromfile("sample.pdf");
printerjob loprinterjob = printerjob.getprinterjob();
pageformat lopageformat = loprinterjob.defaultpage();
paper lopaper = lopageformat.getpaper();
//删除默认页边距
lopaper.setimageablearea(0,0,lopageformat.getwidth(),lopageformat.getheight());
//设置打印份数
loprinterjob.setcopies(2);
lopageformat.setpaper(lopaper);
loprinterjob.setprintable(pdf,lopageformat);
try {
loprinterjob.print();
} catch (printerexception e) {
e.printstacktrace();
}
}
}
显示打印对话框打印pdf文档
import com.spire.pdf.*;
import java.awt.print.*;
public class print {
public static void main(string[] args) {
//加载文档
pdfdocument pdf = new pdfdocument();
pdf.loadfromfile("sample.pdf");
printerjob loprinterjob = printerjob.getprinterjob();
pageformat lopageformat = loprinterjob.defaultpage();
paper lopaper = lopageformat.getpaper();
//删除默认页边距
lopaper.setimageablearea(0,0,lopageformat.getwidth(),lopageformat.getheight());
lopageformat.setpaper(lopaper);
loprinterjob.setprintable(pdf,lopageformat);
//显示打印对话框
if (loprinterjob.printdialog()) {
try {
loprinterjob.print();
} catch (printerexception e) {
e.printstacktrace();
}
}
}
}
打印时自定义纸张大小
import com.spire.pdf.*;
import java.awt.print.*;
public class print {
public static void main(string[] args) {
//加载文档
pdfdocument pdf = new pdfdocument();
pdf.loadfromfile("sample.pdf");
printerjob loprinterjob = printerjob.getprinterjob();
pageformat lopageformat = loprinterjob.defaultpage();
//设置打印纸张大小
paper lopaper = lopageformat.getpaper();
lopaper.setsize(500,600);
lopageformat.setpaper(lopaper);
loprinterjob.setprintable(pdf,lopageformat);
try {
loprinterjob.print();
} catch (printerexception e) {
e.printstacktrace();
}
}
}