java pdfbox 将 pdf 转换为图像字节[]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15033118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:17:38  来源:igfitidea点击:

pdfbox convert pdf to image byte[]

javapdfbox

提问by user2100746

Using pdfbox, is it possible to convert a PDF (or a PDF byte[]) into an image byte[]? I've looked through several examples online and the only ones I can find describe how either to directly write the converted file to the filesystem or to convert it to a Java AWT object.

使用pdfbox,是否可以将PDF(或PDF字节[])转换为图像字节[]?我在网上浏览了几个示例,我能找到的唯一示例描述了如何将转换后的文件直接写入文件系统或将其转换为 Java AWT 对象。

I'd rather not incur the IO of writing an image file to the filesystem, read into a byte[], and then delete it.

我宁愿不产生将图像文件写入文件系统,读入字节 [],然后将其删除的 IO。

So this I can do:

所以我可以这样做:

String destinationImageFormat = "jpg";
boolean success = false;
InputStream is = getClass().getClassLoader().getResourceAsStream("example.pdf");
PDDocument pdf = PDDocument.load( is, true );

int resolution = 256;
String password = "";
String outputPrefix = "myImageFile";

PDFImageWriter imageWriter = new PDFImageWriter();    

success = imageWriter.writeImage(pdf, 
                    destinationImageFormat, 
                    password, 
                    1, 
                    2, 
                    outputPrefix, 
                    BufferedImage.TYPE_INT_RGB, 
                    resolution);

As well as this:

还有这个:

InputStream is = getClass().getClassLoader().getResourceAsStream("example.pdf");

PDDocument pdf = PDDocument.load( is, true );
List<PDPage> pages = pdf.getDocumentCatalog().getAllPages();

for ( PDPage page : pages )
{
    BufferedImage image = page.convertToImage();
}

Where I'm not clear on is how to tranform the BufferedImage into a byte[]. I know this is transformed into a file output stream in imageWriter.writeImage(), but I'm not clear on how the API works.

我不清楚的是如何将 BufferedImage 转换为字节 []。我知道这在 imageWriter.writeImage() 中被转换为文件输出流,但我不清楚 API 是如何工作的。

回答by aditsu quit because SE is EVIL

You can use ImageIO.write to write to an OutputStream. To get a byte[], use a ByteArrayOutputStream, then call toByteArray() on it.

您可以使用 ImageIO.write 写入 OutputStream。要获取 byte[],请使用 ByteArrayOutputStream,然后对其调用 toByteArray()。

回答by BeeNoisy

Add maven dependency:

添加maven依赖:

    <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
    <dependency>
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>pdfbox</artifactId>
        <version>2.0.1</version>
    </dependency>

And, conver a pdf to image:

并且,将 pdf 转换为图像:

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import javax.imageio.ImageIO;

private List<String> savePDF(String filePath) throws IOException {
    List<String> result = Lists.newArrayList();

    File file = new File(filePath);

    PDDocument doc = PDDocument.load(file);
    PDFRenderer renderer = new PDFRenderer(doc);

    int pageSize = doc.getNumberOfPages();
    for (int i = 0; i < pageSize; i++) {
        String pngFileName = file.getPath() + "." + (i + 1) + ".png";

        FileOutputStream out = new FileOutputStream(pngFileName);
        ImageIO.write(renderer.renderImageWithDPI(i, 96), "png", out);
        out.close();

        result.add(pngFileName);
    }
    doc.close();
    return result;
}

EDIT:

编辑:

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import javax.imageio.ImageIO;

private List<String> savePDF(String filePath) throws IOException {
    List<String> result = Lists.newArrayList();

    File file = new File(filePath);

    PDDocument doc = PDDocument.load(file);
    PDFRenderer renderer = new PDFRenderer(doc);

    int pageSize = doc.getNumberOfPages();
    for (int i = 0; i < pageSize; i++) {
        String pngFileName = file.getPath() + "." + (i + 1) + ".png";

        ByteArrayOutputStream out = new ByteArrayOutputStream(pngFileName);
        ImageIO.write(renderer.renderImageWithDPI(i, 96), "png", out);

        out.toByteArray(); // here you can get a byte array

        out.close();

        result.add(pngFileName);
    }
    doc.close();
    return result;
}

回答by Vahap Gencdal

 try {           
                PDDocument document = PDDocument.load(PdfInfo.getPDFWAY());
                if (document.isEncrypted()) {
                    document.decrypt(PdfInfo.getPASSWORD());
                }
                if ("bilevel".equalsIgnoreCase(PdfInfo.getCOLOR())) {
                    PdfInfo.setIMAGETYPE( BufferedImage.TYPE_BYTE_BINARY);
                } else if ("indexed".equalsIgnoreCase(PdfInfo.getCOLOR())) {
                    PdfInfo.setIMAGETYPE(BufferedImage.TYPE_BYTE_INDEXED);
                } else if ("gray".equalsIgnoreCase(PdfInfo.getCOLOR())) {
                    PdfInfo.setIMAGETYPE(BufferedImage.TYPE_BYTE_GRAY);
                } else if ("rgb".equalsIgnoreCase(PdfInfo.getCOLOR())) {
                    PdfInfo.setIMAGETYPE(BufferedImage.TYPE_INT_RGB);
                } else if ("rgba".equalsIgnoreCase(PdfInfo.getCOLOR())) {
                    PdfInfo.setIMAGETYPE(BufferedImage.TYPE_INT_ARGB);
                } else {
                    System.exit(2);
                }
                PDFImageWriter imageWriter = new PDFImageWriter();
                boolean success = imageWriter.writeImage(document, PdfInfo.getIMAGE_FORMAT(),PdfInfo.getPASSWORD(),
                        PdfInfo.getSTART_PAGE(),PdfInfo.getEND_PAGE(),PdfInfo.getOUTPUT_PREFIX(),PdfInfo.getIMAGETYPE(),PdfInfo.getRESOLUTION());
                if (!success) {
                    System.exit(1);
                }
                document.close();

        } catch (IOException | CryptographyException | InvalidPasswordException ex) {
            Logger.getLogger(PdfToImae.class.getName()).log(Level.SEVERE, null, ex);
        }
public class PdfInfo {
    private static String PDFWAY;    
    private static String OUTPUT_PREFIX;
    private static String PASSWORD;
    private static int START_PAGE=1;
    private static int END_PAGE=Integer.MAX_VALUE;
    private static String IMAGE_FORMAT="jpg";
    private static String COLOR="rgb";
    private static int RESOLUTION=256;
    private static int IMAGETYPE=24;
    private static String filename;
    private static String filePath="";
}