itext Image类图片自适应

itextAPI:https://www.coderanch.com/how-to/javadoc/itext-2.1.7/com/lowagie/text/Image.html

int pageNo = form.getFieldPositions(key).get(0).page;
Rectangle signRect = form.getFieldPositions(key).get(0).position;
float x = signRect.getLeft();
float y = signRect.getBottom();
// 根据路径读取图片
Image image = Image.getInstance(imgpath);
// 获取图片页面
PdfContentByte under = stamper.getOverContent(pageNo);
// 图片大小自适应
image.scaleToFit(signRect.getWidth(), signRect.getHeight());
// 添加图片
image.setAbsolutePosition(x, y);
under.addImage(image);

Image类方法:
scaleAbsolute(float newWidth, float newHeight); // 将图像缩放到绝对宽度和绝对高度。
scaleAbsoluteHeight(float newHeight); // 将图像缩放到绝对高度。
scaleAbsoluteWidth(float newWidth); //将图像缩放到绝对宽度。
scalePercent(float percent); // 将图像缩放到一定百分比。
scalePercent(float percentX, float percentY); //将图像的宽度和高度缩放到一定百分比。
scaleToFit(float fitWidth, float fitHeight); //缩放图像,使其适合特定的宽度和高度。
发布了13 篇原创文章 · 获赞 2 · 访问量 480

猜你喜欢

转载自blog.csdn.net/m_crayon/article/details/103483052