为什么在使用Android中的iText库将图像转换为PDF时图像会被裁剪 [英] Why are images getting cropped while converting Images to PDF using iText library in android
问题描述
在我的应用程序中,我想将用户选择的图像转换为单个PDF文件。
我正在使用许多人建议的iText库。
用户选择多个图像,并使用它创建一个pdf,其中每个图像为1 pdf页。
In my application i want to convert the images selected by the user into one single PDF file. I am using iText library as suggested by many people. The user selects multiple images and one pdf is created with it where each image is 1 pdf page.
我正在使用的代码如下所示
The Code i am using is give as below
Document document = new Document(PageSize.A4);
try {
String path = Environment.getExternalStorageDirectory()+"/PDFile.pdf";
File file= new File(path);
if(file.exists())
{
}
else
{
file.createNewFile();
}
PdfWriter.getInstance(document,new FileOutputStream(path));
document.open();
for(int i =0; i<pdfImage.size();i++)
{
Image image = Image.getInstance(pdfImage.get(i));
image.scaleAbsolute(PageSize.A4);
image.setAbsolutePosition(0, 0);
document.add(image);
}
document.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
生成pdf但图片被裁剪。只有一半的图像是可见的其余部分被裁剪。
The pdf is getting generated but the image is cropped. Only half of the image is visible rest of it is cropped.
我是否必须为pdf设置采用图像大小的任何内容?
Do i have to set anything for the pdf to adopt to the image size ??
或者我是必须更改或调整图像大小以采用pdf页面大小??
Or do i have to change or resize the image to adopt to pdf page size??
请帮助!!我不知道如何解决这个问题???
Please help!! I have no idea how to resolve this???
推荐答案
当你这样做时:
Document document = new Document();
然后,您隐式创建一个页面大小称为A4的文档。即:宽度为595,高度为842个用户单位。
Then you implicitly create a document of which the pages have a page size known as A4. That is: a width of 595 and a height 842 user units.
如果添加较小的图像,则不会裁剪它们。如果添加更大的图像。图像将被裁剪...
If you add images that are smaller, they won't be cropped. If you add images that are bigger. The images will be cropped...
如果您希望图像完全适合页面,您有两种选择:
If you want an image to fit a page exactly, you have two options:
- 调整页面大小,或
- 调整图像大小。
这两个选项都是等价的,因为iText不会改变图像的分辨率:每个像素都会被保留。
Both options are equivalent, as iText will not change the resolution of the images: every pixel will be preserved.
选项1:
请参阅我对问题的回答:在iText Java上添加地图
See my answer to the question: Adding maps at iText Java
在这个问题中,我这样做:
In this question, I do this:
Image img = Image.getInstance(IMG);
Document document = new Document(img);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
img.setAbsolutePosition(0, 0);
document.add(img);
document.close();
文档
对象接受 Rectangle
作为参数。此矩形
以用户单位定义页面大小。由于 Image
类是 Rectangle
类的子类,我可以使用 Image
instance作为创建 Document
实例的参数。
The Document
object accepts a Rectangle
as parameter. This Rectangle
defines the page size in user units. As the Image
class is a subclass of the Rectangle
class, I can use the Image
instance as a parameter to create the Document
instance.
另一种选择是做这个:
Rectangle pagesize = new Rectangle(img.getScaledWidth(), img.getScaledHeight());
Document document = new Document(pagesize);
如果您的文档有不同的页面,则必须使用 setPageSize()
方法在触发新页面之前。
If your document has different pages, you must use the setPageSize()
method before triggering a new page.
选项2:
See my answer to the question: Backgroundimage in landscape and cover whole pdf with iTextSharp
代码看起来像这样(嗯,实际代码有点不同,但这样会也工作):
The code looks like this (well, the actual code is a tad different, but this will work too):
Document document = new Document(PageSize.A4.rotate());
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
Image image = Image.getInstance(IMAGE);
image.scaleAbsolute(PageSize.A4.rotate());
image.setAbsolutePosition(0, 0);
document.add(image);
document.close();
这里我有横向A4尺寸的页面,我缩放图像以使其适合页面完全。这是危险,因为这会改变图像的宽高比。这可能导致图像失真。用 scaleToFit()
替换 scaleAbsolute()
将避免这个问题,但如果方面你会有一些白色边距 - 图像的比例与页面的宽高比不同。
Here I have pages with size A4 in landscape, and I scale the image so that it fits the page completely. That is dangerous because this changes the aspect-ratio of the image. This can result in distorted images. Replacing scaleAbsolute()
by scaleToFit()
will avoid that problem, but you'll have some white margins if the aspect-ratio of the image is different from the aspect-ratio of the page.
重要:请注意,我使用了两种情况下都有setAbsolutePosition(0,0);
。我正在介绍此行,以便图像的左下角与页面的左下角重合。如果你不这样做,你会看到底部和左边的边距,你的图像将被剪裁到顶部和右边。
Important: Note that I used setAbsolutePosition(0, 0);
in both cases. I am introducing this line so that the lower-left corner of the image coincides with the lower-left corner of the page. If you don't do this, you will see a margin to the bottom and to the left, and your image will be clipped to the top and the right.
这篇关于为什么在使用Android中的iText库将图像转换为PDF时图像会被裁剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!