PDFBox-将图像转换为PDF,PDF分辨率 [英] PDFBox - convert image to PDF, PDF resolution

查看:2070
本文介绍了PDFBox-将图像转换为PDF,PDF分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PDFBox v2将jpg图像转换为PDF。 JPG图像已经在文件系统上,因此我将其拾取并将其转换为PDF。下面是我的代码

I am using PDFBox v2 to convert jpg images to PDF. JPG image is already on the filesystem, so I just pick it up and convert it to PDF. Below is my code

public void convertImgToPDF(String imagePath, String fileName, String destDir) throws IOException {
        PDDocument document = new PDDocument();
        InputStream in = new FileInputStream(imagePath);
        BufferedImage bimg = ImageIO.read(in);
        float width = bimg.getWidth();
        float height = bimg.getHeight();
        PDPage page = new PDPage(new PDRectangle(width, height));
        document.addPage(page);
        PDImageXObject img = PDImageXObject.createFromFile(imagePath, document);
        PDPageContentStream contentStream = new PDPageContentStream(document, page);
        contentStream.drawImage(img, 0, 0);
        contentStream.close();
        in.close();
        document.save(destDir + "/" + fileName + ".pdf");
        document.close();
    }

此代码可以很好地完成转换。但是,我在转换后的PDF中观察到以下情况

This code does the conversion just fine. However, I have observed the following in converted PDFs


  1. 当我打开转换后的PDF时,它的打开速度非常慢(在Acrobat Reader中)。 PDF似乎在逐像素打开。如果我打开其他任何PDF,也可以正常打开。

  2. 对于转换后的PDF,acrobat Reader的默认大小显示为较小的值(例如15%或24%,附带屏幕截图)。即使它覆盖了屏幕的100%(15%的灵魂暗示我看到的图像要小得多,但事实并非如此)。当我将大小更改为100%时,会看到一个高度模糊的图像,该图像比实际图像大得多,并且必须向左/向右和从上/下滚动才能看到完整的图像。

这两个观察都使我感到,由于某种原因,正在生成的PDF分辨率比原本应该的分辨率高得多。这是一个公平的声明吗?如果是这样,我该如何纠正?

Both these observations make me feel that for some reason the PDF that is getting generated is a much higher resolution than it should have been. Would that be a fair statement? If so, how can I correct this?

编辑
附加了将被转换为PDF的JPG图像。当我打开PDF时,它显示25.9%的大小。在iPad上单击了此图像。

EDIT Attached the JPG image which is being converted to PDF. When I open the PDF, it shows a size of 25.9%. This image was clicked on iPad.

推荐答案

有一个重载的 drawImage()方法可以做到这一点

There is an overloaded drawImage() method to do that

contentStream.drawImage(PDImageXObject, float x, float y)
contentStream.drawImage(PDImageXObject, float x, float y, float height, float width)

通过在 contentStream.drawImage()上指定显式的宽度和高度,可以取得成就。例如,如果您想显示较小的图像4倍,但仍保持相同的分辨率,则可以使用

By specifying explicit width and height on contentStream.drawImage(), this can be achieved. For instance if you want to show the image 4 times smaller but still retain the same resolution, it can done using

scaleDownRatio = 0.25;
pageSize = PDRectangle.A4;

# this will draw image at bottom left corner
PDImageXObject pageImageXObject = LosslessFactory.createFromImage(document, bitmap);
contentStream.drawImage(pageImageXObject, 0, 0, bitmap.getWidth() * ratio, bitmap.getHeight() * ratio)

# to center it you can use something like
contentStream.drawImage(pageImageXObject, (pageSize.getWidth() - bitmap.getWidth() * ratio) / 2, (pageSize.getHeight() - bitmap.getHeight() * ratio) / 2, bitmap.getWidth() * ratio, bitmap.getHeight() * ratio)

注意:在您的情况下,图像仍不会即使将其缩放到四分之一大小也适合;您可以通过

Note: in your case, the image wouldn't still fit in even after scaling it to one-fourth the size; You can get the scale factor by

float widthRatio = bitmap.getWidth() / pageSize.getWidth();
float heightRatio = bitmap.getHeight() / pageSize.getHeight();
scaleDownRatio = (widthRatio<heightRatio)? widthratio:heightRatio;

这篇关于PDFBox-将图像转换为PDF,PDF分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆