[英]How to make pdf file password protected?
我想讓pdf文件受密碼保護。 我只是用谷歌搜索它並找到下面給出的一個很好的解決方案。 它工作正常但是在我使用下面給定的代碼保護 pdf 后,它清除了我的 pdf 中已經存在的所有數據。
此代碼使用的 jar 文件是:
itextpdf-5.2.1.jar
bcmail-jdk16-1.46.jar
bcprov-jdk16-1.46.jar
bctsp-jdk16-1.46.jar
保護 PDF 的代碼:
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Secure_file {
private static String USER_PASSWORD = "password";
private static String OWNER_PASSWORD = "secured";
public static void main(String[] args) throws IOException {
Document document = new Document();
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("E:\\sample.pdf"));
writer.setEncryption(USER_PASSWORD.getBytes(),OWNER_PASSWORD.getBytes(), PdfWriter.ALLOW_PRINTING,PdfWriter.ENCRYPTION_AES_128);
document.open();
document.add(new Paragraph("This is Password Protected PDF document."));
document.close();
writer.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
我需要在這個程序中做哪些改變?
如果您查找iText in Action 關鍵字,您會發現加密指向示例 part3.chapter12。 加密.pdf 該示例的方法createPdf
本質上等同於您的代碼,但方法encryptPdf
是您想要的:
/** User password. */
public static byte[] USER = "Hello".getBytes();
/** Owner password. */
public static byte[] OWNER = "World".getBytes();
...
public void encryptPdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
stamper.setEncryption(USER, OWNER,
PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
stamper.close();
reader.close();
}
使用 iText5 的示例。 如果使用 iText7 非常相似,但使用另一個類而不是stampler。
PdfReader reader = new PdfReader(dp.getStream());
File tempFile = File.createTempFile("someFilename", FILE_EXTENSION_PDF);
tempFile.deleteOnExit();
FileOutputStream os = new FileOutputStream(tempFile);
PdfStamper stamper = new PdfStamper(reader, os);
String pdfPassword = "1234"
String pdfAdminPassword = "5678"
stamper.setEncryption(
pdfPassword.getBytes(),
pdfAdminPassword.getBytes(),
PdfWriter.ALLOW_PRINTING,
PdfWriter.ENCRYPTION_AES_128);
reader.close();
stamper.close();
InputStream encryptedFileIs = new FileInputStream(tempFile);
或 apache lib pdfbox
PDDocument document = PDDocument.load(dp.getStream());
AccessPermission ap = new AccessPermission();
StandardProtectionPolicy spp = new StandardProtectionPolicy("1234", "1234", ap);
spp.setEncryptionKeyLength(128);
spp.setPermissions(ap);
document.protect(spp);
File tempFile = File.createTempFile("someFilename", FILE_EXTENSION_PDF);
tempFile.deleteOnExit();
FileOutputStream os = new FileOutputStream(tempFile);
document.save(os);
document.close();
InputStream encryptedFileIs = new FileInputStream(tempFile);
祝你好運,編碼愉快:)
stamper.setEncryption(USER, OWNER,PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
我已使用此代碼為 pdf 添加密碼。 打開pdf時它會要求輸入密碼
我使用過 FOP 請參閱此文檔
FOUserAgent userAgent = fopFactory.newFOUserAgent();
useragent.getRendererOptions().put("encryption-params", new PDFEncryptionParams(
null, "password", false, false, true, true));
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, userAgent);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.