java 如何使pdf文件受密码保护?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25681239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make pdf file password protected?
提问by user3747920
I want to make pdf file password protected. I just goolge it for the same and find a good solution given below. It's working fine But it wipe out all the data which is already there in my pdf after i secure pdf using below given code.
我想让pdf文件受密码保护。我只是用谷歌搜索它并找到下面给出的一个很好的解决方案。它工作正常但是在我使用下面给定的代码保护 pdf 后,它清除了我的 pdf 中已经存在的所有数据。
Used jar files for this code are:
此代码使用的 jar 文件是:
itextpdf-5.2.1.jar
itextpdf-5.2.1.jar
bcmail-jdk16-1.46.jar
bcmail-jdk16-1.46.jar
bcprov-jdk16-1.46.jar
bcprov-jdk16-1.46.jar
bctsp-jdk16-1.46.jar
bctsp-jdk16-1.46.jar
Code to secure PDF :
保护 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();
}
}
}
what changes i need to made in this program ?
我需要在这个程序中做哪些改变?
回答by mkl
If you look up the iText in Action keywordsyou'll find encryptionpointing to the sample part3.chapter12.EncryptionPdf. That sample's method createPdf
essentially is equivalent to your code but the method encryptPdf
is what you want:
如果您查找iText in Action 关键字,您会发现加密指向示例 part3.chapter12。EncryptionPdf。该示例的方法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();
}
回答by Manikandarajan
stamper.setEncryption(USER, OWNER,PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
I've used this code to add password for the pdf. it will ask for the password while opening the pdf
我已使用此代码为 pdf 添加密码。打开pdf时它会要求输入密码
回答by Jayampathy Wijesena
回答by panayot_kulchev_bg
example using iText5. If use iText7 is very similar but using another class instead of stampler.
使用 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);
or apache lib pdfbox
或 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);
Good luck and happy coding :)
祝你好运,编码愉快:)