1、添加pom依赖
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.3</version>
</dependency>
2、带对话框打印文件
filePath是远程文件路径
public void printWithDialog(String filePath) {
try {
URL url = new URL(filePath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
PDDocument document = PDDocument.load(connection.getInputStream());
//打印本地文件
// PDDocument document = PDDocument.load(new File(filaPath));
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(new PDFPageable(document));
if (job.printDialog()){
job.print();
}
} catch (IOException e) {
e.printStackTrace();
} catch (PrinterException e) {
e.printStackTrace();
}
}