0
我試圖通過打印使用PDFBox 1.8.0從以下here的示例中去除對某些認證PDF文件的保護。PDFBox - 打印加密文件
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.IOException;
import javax.print.PrintException;
import javax.print.PrintService;
import org.apache.log4j.Logger;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPageable;
public class PDFPrinter {
private static final Logger LOGGER = Logger.getLogger(PDFPrinter.class);
public static void main(String[] args) throws IOException, PrinterException, PrintException {
LOGGER.info("Running...");
PDDocument pdDocument = PDDocument.load("C:\\_APPS\\input.pdf");
PrinterJob printerJob = PrinterJob.getPrinterJob();
PrintService printService = null;
if (printerJob.printDialog()) {
printService = printerJob.getPrintService();
LOGGER.info("Using printer: " + printService.getName());
}
printerJob.setPageable(new PDPageable(pdDocument));
printerJob.print();
pdDocument.close();
}
}
我正在使用Microsoft Print to PDF打印機。 我得到一個NPE在PDPageable類@ 74行說我是不是得了這個文件上沒有設置訪問權限:
java.lang.NullPointerException
at org.apache.pdfbox.pdmodel.PDPageable.<init>(PDPageable.java:74)
at org.apache.pdfbox.pdmodel.PDPageable.<init>(PDPageable.java:96)
at com.util.pdf.decrypt.Main.decryptAndOcr(Main.java:179)
at com.util.pdf.decrypt.Main.main(Main.java:82)
這的確是在檢查pdDocument.getCurrentAccessPermission()
空。 所以我想手動設置一些訪問權限,沒有用戶名或密碼,只與打印權限:
AccessPermission accessPermission = new AccessPermission();
accessPermission.setCanPrint(true);
StandardProtectionPolicy spp = new StandardProtectionPolicy("", "", accessPermission);
try {
pdDocument.protect(spp);
} catch (BadSecurityHandlerException bhe) {
bhe.printStackTrace();
}
但權限與pdDocument.getCurrentAccessPermission()
檢查時,仍然無效。
問題:如何在PDF上設置打印訪問權限?
注意:當我直接從操作系統使用Microsoft Print Print打印到PDF打印機時,將從打印的PDF中刪除證書,以便該過程正常工作。