Notes: Java module to convert PDF document to Image_Files

 import java.awt.image.BufferedImage;

import java.io.File;

import java.util.Arrays;

import javax.imageio.ImageIO;

import org.apache.commons.lang3.StringUtils;

import org.apache.pdfbox.pdmodel.PDDocument;

import org.apache.pdfbox.rendering.ImageType;

import org.apache.pdfbox.rendering.PDFRenderer;


private static void convertPdfToImage(String pdfFilePath, String imageFolderPath) throws Exception {

        File file = new File(pdfFilePath);

        String fileExtension = ".jpg";

        int dpi=300;

        try (PDDocument doc = PDDocument.load(file)) {

            PDFRenderer pdfRenderer = new PDFRenderer(doc);

            for (int i = 0; i < doc.getPages().getCount(); i++) {

                BufferedImage img = pdfRenderer.renderImageWithDPI(i, dpi, ImageType.RGB);

                File outputFile = new File(imageFolderPath + StringUtils.removeEnd(file.getName(),".pdf") + "_pgNo" + i + fileExtension);

                outputFile.delete();

                System.out.println(outputFile.getAbsolutePath());

                System.out.println(Arrays.toString(ImageIO.getWriterFormatNames()));

                boolean flg=ImageIO.write(img, "jpg", outputFile);

                System.out.println(flg);

            }


        }

    }

No comments:

Post a Comment

If you have any doubts, please let me know.

Various types of salts effect on health (discussion with gemenie)

Is  rock salt or bitnoon helps in digestion or just a airuvedic nuska This is for informational purposes only. For medical advice or diagnos...