java - 使用 PDFBox 2.0 渲染 PDF 并解密

标签 java pdfbox

我刚刚将 PDFBox 版本从 1.8 升级到 2.0。 Migration表示 .convertToImage() 已被删除,并且示例代码中没有 BufferedImage 的一行,但它在 .writeImage()

他们的代码:

PDDocument document = PDDocument.load(new File(pdfFilename));
PDFRenderer pdfRenderer = new PDFRenderer(document);
int pageCounter = 0;
for (PDPage page : document.getPages())
{ 
    pdfRenderer.renderImageWithDPI(pageCounter, 300, ImageType.RGB);

    // suffix in filename will be used as the file format
    ImageIOUtil.writeImage(bim, pdfFilename + "-" + (pageCounter++) + ".png", 300);
}
document.close();

我相信 BufferedImage 在 ImageIOUtil.writeImage(bim, pdfFilename + "-"+ (pageCounter++) + ".png", 300); 中用作 bim。如果他们删除了 .convertToImage(),我应该如何实现 BufferedImage?

第二件事是关于.decrypt()。他们没有提到.decrypt()。我应该使用什么来代替 .decrypt()

我的整个代码:

try {
            String sourceDir = "/home/linux/books/text.pdf";

            File sourceFile = new File(sourceDir);

                PDDocument document = PDDocument.load(sourceFile);
                PDFRenderer pdfRenderer = new PDFRenderer(document);
                if (document.isEncrypted()) {
                    try {
                        System.out.println("Trying to decrypt it...");
                        document.decrypt("");
 // error says: The method decrypt(String) is undefined for the type PDDocument.
 // it was working on pdfbox 1.8.
                        document.setAllSecurityToBeRemoved(true);
                        System.out.println("The file has been decrypted in .");
                    }
                    catch (Exception e) {
                        throw new Exception("cannot be decrypted. ", e);
                    }
                }

                PDPage firstPage = (PDPage) document.getDocumentCatalog().getPages().get(0);
                pdfRenderer.renderImageWithDPI(0, 300, ImageType.RGB);

                String fileName = sourceFile.getName().replace(".pdf", "");

                    BufferedImage image = firstPage.convertToImage(BufferedImage.TYPE_INT_RGB, 300);  

                    ImageIOUtil.writeImage(image , fileName+".jpg",300);

                document.close();

        } catch (Exception e) {
                e.printStackTrace();

        }

入门内容正在构建中。这就是为什么我无法检查我的问题。

最佳答案

I believe BufferedImage is used in ImageIOUtil.writeImage(bim, pdfFilename + "-" + (pageCounter++) + ".png", 300); as bim. How should I implement BufferedImage if they have removed .convertToImage()?

其实迁移指南里少了一点点,应该是

BufferedImage bim = pdfRenderer.renderImageWithDPI(pageCounter, 300, ImageType.RGB);

而不是

pdfRenderer.renderImageWithDPI(pageCounter, 300, ImageType.RGB);

Second thing is about .decrypt(). They have not mentioned about .decrypt(). What should I use instead of .decrypt()?

PDDocument 现在有多个 load 重载,它们也接受密码,例如

/**
 * Parses a PDF. Unrestricted main memory will be used for buffering PDF streams.
 * 
 * @param file file to be loaded
 * @param password password to be used for decryption
 * 
 * @return loaded document
 * 
 * @throws IOException in case of a file reading or parsing error
 */
public static PDDocument load(File file, String password) throws IOException

/**
 * Parses a PDF. The given input stream is copied to the memory to enable random access to the pdf.
 * Unrestricted main memory will be used for buffering PDF streams.
 * 
 * @param input stream that contains the document.
 * @param password password to be used for decryption
 * 
 * @return loaded document
 * 
 * @throws IOException in case of a file reading or parsing error
 */
public static PDDocument load(InputStream input, String password)
        throws IOException

关于java - 使用 PDFBox 2.0 渲染 PDF 并解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36698492/

相关文章:

java - 按尺寸值自动缩放文本大小

java - 是否有用于 XMPP 客户端的 Android native 库?

java - 在同步块(synchronized block)中锁定自己的对象时出现问题

validation - 通过Ghostscript将PS文件转换为PDF/A,色彩空间问题

java - PDFBox IO异常: COSStream has been closed and cannot be read

java - 动画排序算法

java - Hibernate Validator 验证集

java - 如何设置PDFBox的字符编码

java - PDFBox - 无法编码由代理项对组成的字符串

pdf - 使用 PDFBox 获取每一行的字体