Munchmallow
Munchmallow

Reputation: 311

PDF Rendering with PDFBox 2.0 and Decrypting

I have just upgraded PDFBox version from 1.8 to 2.0. Migration says that .convertToImage() has been removed and there is no a line in their example code for BufferedImage, but it is used in .writeImage()

Their code:

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

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

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

My whole code:

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

        }

Getting Started content is under construction. That is why I cannot check my problems.

Upvotes: 2

Views: 2589

Answers (1)

mkl
mkl

Reputation: 96074

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

Actually there is a tiny bit missing in the migration guide, it should be

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

instead of

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 now has multiple load overloads which also accept a password, e.g.

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

or

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

Upvotes: 2

Related Questions