有人知道如何使用openpdfjavalib为现有pdf设置密码吗



如何使用OpenPdf java lib为现有PDF设置密码?我尝试过下面的代码,但那是创建的新的pdf没有内容

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class PasswordProtectedPDF {
public static final String ENCRYPTED_PDF = "F://knpcode//result//OpenPDF//PP.pdf";
// User and owner password
final static String USER_PASSWORD = "user";
final static String OWNER_PASSWORD = "owner";
public static void main(String[] args) {
try {
Document doc = new Document();
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(ENCRYPTED_PDF));
// set password, user permissions and encryption
writer.setEncryption(USER_PASSWORD.getBytes(), OWNER_PASSWORD.getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128); 
doc.open();


doc.close();
writer.close();
} catch (DocumentException | FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

要向新PDF添加密码,我们使用PdfWriter.setEncryption((方法。

当我们需要保护现有的PDF时,使用PdfStamper API。在实例化PdfStamper时,它接受源文件作为PdfReader,接受目标文件作为OutputStream。PdfStamper可以在写入目标文件时添加一些额外的PDF内容。PdfStamper.setEncryption在将加密设置为现有PDF时使用与PdfWriter.setEncryption相同的参数。

已更新您的代码以使用PDFStamper而不是PDFWriter。


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfStamper;

public class PasswordProtectedPDF {
// User and owner password
final static String USER_PASSWORD = "user";
final static String OWNER_PASSWORD = "owner";
public static void main(String[] args) {
try {
File f = new File("F://knpcode//result//OpenPDF//ENCRYPTED_PP.pdf");
FileOutputStream out = new FileOutputStream(f);
PdfReader reader = new PdfReader("F://knpcode//result//OpenPDF//PP.pdf");
PdfStamper stamper = new PdfStamper(reader, out);

// set password, user permissions and encryption
stamper.setEncryption(USER_PASSWORD.getBytes(), OWNER_PASSWORD.getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
// Don't forget to add this line as no bytes are written to that output stream up until you close the PdfStamper instance. 
stamper.close();
} catch ( IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

使用谷歌"javaopenpdf密码示例";我发现了这个网站:https://knpcode.com/java-programs/password-protected-pdf-using-openpdf-java/提供一些有关您问题的信息。

这里有两个示例程序(只是复制/粘贴它们,而不是测试(。别忘了将Bouncy Castle作为安全提供商。

加密("安全"(PDF:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class PasswordProtectedPDF {
public static final String ENCRYPTED_PDF = "F://knpcode//result//OpenPDF//PP.pdf";
// User and owner password
final static String USER_PASSWORD = "user";
final static String OWNER_PASSWORD = "owner";
public static void main(String[] args) {
try {
Document doc = new Document();
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(ENCRYPTED_PDF));
// set password, user permissions and encryption
writer.setEncryption(USER_PASSWORD.getBytes(), OWNER_PASSWORD.getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128); 
doc.open();
Paragraph para = new Paragraph("Password protected PDF where only content printing is permitted content can't be copied.");
doc.add(para);
doc.close();
writer.close();
} catch (DocumentException | FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

使用OpenPDF:读取受密码保护的PDF

import java.io.IOException;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.parser.PdfTextExtractor;
public class ReadPDF {
// PDF to be read
public static final String READ_PDF = "F://knpcode//result//OpenPDF//PP.pdf";
final static String OWNER_PASSWORD = "owner";
public static void main(String[] args) {
PdfReader pdfreader = null;
try {
pdfreader = new PdfReader(READ_PDF, OWNER_PASSWORD.getBytes());
// get pages in PDF
int pages = pdfreader.getNumberOfPages();
PdfTextExtractor pdfTextExtractor = new PdfTextExtractor(pdfreader);
// Iterate through pages to read content
for(int i = 1; i <= pages; i++) {
// Extract content of each page
String contentOfPage = pdfTextExtractor.getTextFromPage(i, true);
System.out.println(contentOfPage );
}         
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(pdfreader != null) {
pdfreader.close();
}
}   
}
}

最新更新