我刚刚将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();

        }


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

最佳答案

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


实际上,迁移指南中缺少一点点,应该是

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


代替

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





  第二件事是关于.decrypt()。他们没有提到.decrypt()。我应该用什么代替.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

07-26 08:48