Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java PDF到图像转换高尺寸图像_Java_Pdf_Icepdf - Fatal编程技术网

Java PDF到图像转换高尺寸图像

Java PDF到图像转换高尺寸图像,java,pdf,icepdf,Java,Pdf,Icepdf,我使用下面的代码将PDF转换为PNG图像 Document document = new Document(); try { document.setFile(myProjectPath); System.out.println("Parsed successfully..."); } catch (PDFException ex) { System.out.println("

我使用下面的代码将PDF转换为PNG图像

        Document document = new Document();
        try {
            document.setFile(myProjectPath);
            System.out.println("Parsed successfully...");
        } catch (PDFException ex) {
            System.out.println("Error parsing PDF document " + ex);
        } catch (PDFSecurityException ex) {
            System.out.println("Error encryption not supported " + ex);
        } catch (FileNotFoundException ex) {
            System.out.println("Error file not found " + ex);
        } catch (IOException ex) {
            System.out.println("Error handling PDF document " + ex);
        }

        // save page caputres to file.
        float scale = 1.0f;
        float rotation = 0f;

        // Paint each pages content to an image and write the image to file
        InputStream fis2 = null;
        File file = null;
        for (int i = 0; i < 1; i++) {
            BufferedImage image = (BufferedImage) document.getPageImage(i,
                    GraphicsRenderingHints.SCREEN,
                    Page.BOUNDARY_CROPBOX, rotation, scale);
            RenderedImage rendImage = image;
            // capture the page image to file
            try {
                System.out.println("\t capturing page " + i);
                file = new File(myProjectActualPath + "myImage.png");
                ImageIO.write(rendImage, "png", file);
                fis2 = new BufferedInputStream(new FileInputStream(myProjectActualPath + "myImage.png"));
            } catch (IOException ioe) {
                System.out.println("IOException :: " + ioe);
            } catch (Exception e) {
                System.out.println("Exception :: " + e);
            }
            image.flush();
        }
我的意思是

import org.icepdf.core.exceptions.PDFException;
import org.icepdf.core.exceptions.PDFSecurityException;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.pobjects.Page;
import org.icepdf.core.util.GraphicsRenderingHints;

您应该能够通过更改比例来更改文件的大小。PDF通常比渲染图像小得多。它们可以表示文本和矢量图形,渲染图像将使用大量字节来表示。事实上,我有点惊讶,您的任何PNG都与pdf大小相同,除非pdf只是图片。

您可以使用PDFBox从pdf示例中提取图像:

    List<PDPage> pages = document.getDocumentCatalog().getAllPages();
    for(PDPage page : pages) {
        Map<String, PDXObjectImage> images = page.getResources().getImages();

        for(PDXObjectImage image : images.values()){
            //TODO: write image to disk
        }   
    }

您使用的是什么PDF API?PDFBox允许设置BuffereImage的目标DPI。@dngfng:我更新了问题…部分大小问题是png格式-它不提供任何压缩。如果使用jpg作为输出格式,则图像应小得多。@dngfng:float scale=0.1f;将5.9 MB带到500 KB。如果质量正常,你可以这样做,但是我仍然会考虑从PDF中提取图像,而不是基于PDF生成一个新的图像。我已经用下面的PDFbox发布了一个例子。在这种情况下,你可能想考虑从PDF中提取图像,而不是创建PDF页面的图像
    List<PDPage> pages = document.getDocumentCatalog().getAllPages();
    for(PDPage page : pages) {
        Map<String, PDXObjectImage> images = page.getResources().getImages();

        for(PDXObjectImage image : images.values()){
            //TODO: write image to disk
        }   
    }
image.getSuffix();