PDF使用PDFBox 2.0和Decrypting进行渲染

时间:2016-04-18 15:36:45

标签: 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();

        }

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

1 个答案:

答案 0 :(得分:2)

  

我相信BufferedImageImageIOUtil.writeImage(bim, pdfFilename + "-" + (pageCounter++) + ".png", 300);使用的bim.convertToImage()。如果删除了BufferedImage bim = pdfRenderer.renderImageWithDPI(pageCounter, 300, ImageType.RGB); ,我应该如何实现BufferedImage?

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

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

{{1}}