0
下面的代碼是用於編寫pdf文件;Outputstream - 創建不可編輯的pdf文件
BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
FontSelector fs = new FontSelector();
fs.addFont(new Font(bf));
String fileName = filePath3 + "//DEVIATION_REPORT.pdf";
OutputStream file = new FileOutputStream(new File(fileName));
Font smallFont = new Font(Font.FontFamily.COURIER, 6, Font.NORMAL);
Font headerFont = new Font(Font.FontFamily.HELVETICA, 7, Font.BOLD);
Font tabFont = new Font(Font.FontFamily.HELVETICA, 5, Font.NORMAL);
Font rusFont = new Font(bf, 5);
Font blueFont = new Font(bf, 5);
blueFont.setColor(BaseColor.BLUE);
Font redFont = new Font(bf, 5);
redFont.setColor(BaseColor.RED);
String comType;
if (compType == 2)
comType = "Character";
else
comType = "Word";
Document doc = new Document();
PdfWriter.getInstance(doc, file);
doc.open();
Image image1 = Image.getInstance(cmdpath + "ScRp.jpg");
image1.setAlignment(Element.ALIGN_LEFT);
Paragraph prg = new Paragraph("Compare", smallFont);
prg.setAlignment(Element.ALIGN_RIGHT);
doc.add(image1);
doc.add(prg);
doc.add(new Paragraph("__________________________________________________"));
doc.add(new Paragraph("Passed Report"));
doc.add(new Paragraph(" "));
PdfPTable table = new PdfPTable(2);
PdfPCell cell1 = new PdfPCell(new Paragraph("No", headerFont));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setPaddingBottom(5);
PdfPCell cell2 = new PdfPCell(new Paragraph("Details", headerFont));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setPaddingBottom(5);
table.addCell(cell1);
table.addCell(cell2);
doc.add(table);
doc.newPage();
doc.close();
file.close();
如果我想讓pdf不可編輯,該怎麼辦?我試過使用
PdfEncryptor.encrypt(
new PdfReader(fileName),
new FileOutputStream("HelloWorldProtected.pdf"),
null,
"StrongPassword".getBytes(),
PdfWriter.AllowPrinting,
PdfWriter.STRENGTH128BITS);
但編譯器在此停止。我等了超過15分鐘,但沒有發生任何事情。
我甚至會讓file.setreadOnly(),但它不會在編輯後保存它。它仍然允許用戶編輯。用戶可以將保存爲AS文件並替換爲不可編輯的文件。
是否有任何其他方式可以使文件不可編輯。請指教。
讚賞如果你可以分享你的工作代碼只讀部分。 –
作爲我的問題提到..當我做'PdfWriter rptWritter = PdfWriter.getInstance(doc,文件); rptWritter.setEncryption(USER_PASS.getBytes(),OWNER_PASS.getBytes(), PdfWriter.ALLOW_PRINTING,PdfWriter.ENCRYPTION_AES_128);'調試器在這裏停留很長時間。即使我等了15分鐘也沒有迴應。 –
漫長的等待15分鐘。問題可能是文件路徑上的文件寫入權限。 –