简体   繁体   English

如何使用 java8 和 iText 密码保护现有的 pdf 文件?

[英]How to password protect a existing pdf file using java8 & iText?

I am using the spring boot project to implement my code with the following dependencies:我正在使用 spring 引导项目来实现我的代码,并具有以下依赖项:

<dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13.2</version>
        </dependency>
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk15on</artifactId>
            <version>1.69</version>
        </dependency>

I have written the following code, and I can make my pdf file password protected, but the code will produce an additional file**[protectedOutput.pdf]** to make that happen.我写了下面的代码,我可以让我的 pdf 文件密码受保护,但是代码会生成一个额外的文件**[protectedOutput.pdf]** 来实现这一点。 I want my existing pdf to only be made password protected without using a new pdf at a specific path.我希望现有的 pdf 仅受密码保护,而无需在特定路径上使用新的 pdf。

Code is as follows:代码如下:

package com.example.encryptMyPdf;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class EncryptMyPdfApplication{
    public static void main(String[] args) throws FileNotFoundException, DocumentException, IOException {
        PdfReader reader = new PdfReader("/Users/ayushg/desktop/protected.pdf");
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("/Users/ayushg/desktop/protectedOutput.pdf"));
        stamper.setEncryption("password".getBytes(), "owner_password".getBytes(),PdfWriter.ALLOW_COPY, PdfWriter.ENCRYPTION_AES_256);
        stamper.close();
        reader.close();
        System.out.println("pdf is password protected now ");
        SpringApplication.run(EncryptMyPdfApplication.class, args);
    }


}

Any suggestions on the same are very much welcomed.非常欢迎对此提出任何建议。 Thanks in advance!提前致谢!

When manipulating a PDF using a PdfReader / PdfStamper pair, the reader still needs the original PDF to read from while the stamper already stores new data to its output. Thus, they cannot both directly work on the same file system file.当使用PdfReader / PdfStamper对操作 PDF 时,读取器仍然需要原始 PDF 来读取,而压模器已经将新数据存储到其 output。因此,它们不能直接在同一个文件系统文件上工作。

So if you want the result to end up in the same file system file as the source and don't want to create temporary files in the file system either, you'll have to temporarily store input or output elsewhere, eg in memory.因此,如果您希望结果在与源相同的文件系统文件中结束,并且不想在文件系统中创建临时文件,则必须将输入或 output 临时存储在其他地方,例如 memory。

For example, to use an in-memory copy of the source file:例如,要使用源文件的内存副本:

java.io.File file = new java.io.File("...");
byte[] bytes = java.nio.file.Files.readAllBytes(file.toPath());
PdfReader reader = new PdfReader(bytes);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(file));
...
stamper.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM