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

发布于 2025-01-09 19:41:33 字数 1811 浏览 1 评论 0原文

我正在使用 spring boot 项目来实现具有以下依赖项的代码:

<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>

我编写了以下代码,并且我可以使我的 pdf 文件受密码保护,但该代码将生成一个附加文件**[protectedOutput.pdf]**为了实现这一目标。 我希望我现有的 pdf 仅受密码保护,而不在特定路径上使用新的 pdf。

代码如下:

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);
    }


}

非常欢迎任何对此的建议。提前致谢!

I am using the spring boot project to implement my code with the following dependencies :

<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.
I want my existing pdf to only be made password protected without using a new pdf at a specific path.

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清秋悲枫 2025-01-16 19:41:33

使用 PdfReader/PdfStamper 对操作 PDF 时,阅读器仍然需要读取原始 PDF,而压模已经将新数据存储到其输出中。因此,它们不能直接处理同一个文件系统文件。

因此,如果您希望结果最终位于与源相同的文件系统文件中,并且不想在文件系统中创建临时文件,则必须将输入或输出临时存储在其他地方,例如内存中。

例如,要使用源文件的内存中副本:

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();

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.

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, e.g. in 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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文