Java 使用 ITEXT 将图像拟合为 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19256275/
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-08-12 15:30:54  来源:igfitidea点击:

Fitting image into PDF using ITEXT

javaimagepdfitext

提问by user2582318

Hi im having some problems to add the image into the PDF using the itextPDF...

嗨,我在使用 itextPDF 将图像添加到 PDF 时遇到了一些问题...

i have more than 10.000 images scanned from original paper in past years, and different sizes/pixels

在过去的几年里,我从原始纸张上扫描了 10.000 多张图像,并且具有不同的尺寸/像素

when i add image into pdf using

当我使用

img.scaleAbsolute(823,640) or img.scaleToFit(823,640)

img.scaleAbsolute(823,640) 或 img.scaleToFit(823,640)

doesnt change the result of each image, for example this ones:

不会改变每个图像的结果,例如这个:

First One 654 is the correct one that fit perfectly in the pdf PAGE, the original tiff image has (2048 x 2929)

第一个 654 是正确的,完全适合 pdf PAGE,原始 tiff 图像具有 (2048 x 2929)

here image -> enter image description here

这里的图像 - > 在此处输入图片说明

here is the second one 9436 that original tiff image has (1470 x 2057)

这是原始 tiff 图像具有的第二个 9436 (1470 x 2057)

enter image description here

在此处输入图片说明

look the MARGIN of the pdf in the first and in the second one...

在第一个和第二个中查看 pdf 的边距...

there is a way to make EVERY image like the 654, no matter the original size??

有一种方法可以使每个图像都像 654 一样,无论原始大小如何??

thanks, aprreciate any idea, or i will have to open all the image in paint and edit it :(

谢谢,感谢任何想法,否则我将不得不在油漆中打开所有图像并对其进行编辑:(

采纳答案by user2582318

Solved for me:

为我解决:

//if you would have a chapter indentation
int indentation = 0;
//whatever
Image image = coolPic;

float scaler = ((document.getPageSize().getWidth() - document.leftMargin()
               - document.rightMargin() - indentation) / image.getWidth()) * 100;

image.scalePercent(scaler);

from @Franz Ebner

来自@Franz Ebner

iText Image Resize

iText 图像调整大小

回答by GregD

I know it's an old question, but I was looking for it myself and I've found:

我知道这是一个老问题,但我自己也在寻找,我发现:

Image image = ...;
image.scaleToFit(PageSize.A4.getWidth(), PageSize.A4.getHeight());

found it useful enough to share here.

发现它足够有用,在这里分享。