11

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

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

所以我可以这样做:

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

还有这个:

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

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

4

3 回答 3

11

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

于 2013-02-22T21:12:14.063 回答
1

添加maven依赖:

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

并且,将 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;
}

编辑:

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;
}
于 2016-12-27T04:01:56.867 回答
0
 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="";
}
于 2014-01-14T21:11:33.910 回答