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); //缩放图像,使其适合特定的宽度和高度。

 

  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要实现itext AcroFields表格高度自适应,可以按照以下步骤操作: 1. 获取表格中每一行的单元格数量以及单元格中的内容。 2. 根据单元格中的内容计算出每个单元格所需的行数。 3. 计算出每行所需的最大行数。 4. 根据每个单元格所需的行数以及每行所需的最大行数设置表格的高度。 5. 遍历每个单元格并设置单元格的高度。 示例代码如下: ```java AcroFields form = reader.getAcroFields(); int rowCount = form.getRows().size(); // 获取表格行数 int colCount = form.getColumns().size(); // 获取表格列数 float maxRowHeight = 0f; // 最大行高 float[] columnWidths = new float[colCount]; // 列宽度数组 for (int i = 0; i < colCount; i++) { columnWidths[i] = form.getFieldPositions(form.getFieldName(i + 1)).get(0).position.getWidth(); } for (int i = 0; i < rowCount; i++) { for (int j = 0; j < colCount; j++) { String cellText = form.getField(String.format("%d_%d", j + 1, i + 1)); int cellRows = calculateRows(cellText, columnWidths[j]); // 计算单元格所需行数 maxRowHeight = Math.max(maxRowHeight, cellRows * 10f); // 10f为单元格行高 } } float tableHeight = maxRowHeight * rowCount; // 计算表格高度 form.setFieldProperty("table", "setfflags", PdfFormField.FF_READ_ONLY, null); // 将表格设置为只读 form.setFieldProperty("table", "setflags", PdfFormField.FLAGS_HIDDEN, null); // 隐藏表格 form.setFieldProperty("table", "setflags", PdfFormField.FLAGS_PRINT, null); // 设置表格可打印 PdfPTable table = new PdfPTable(colCount); table.setTotalWidth(columnWidths); table.setLockedWidth(true); for (int i = 0; i < rowCount; i++) { for (int j = 0; j < colCount; j++) { PdfPCell cell = new PdfPCell(new Phrase(form.getField(String.format("%d_%d", j + 1, i + 1)))); cell.setFixedHeight(maxRowHeight); table.addCell(cell); } } PdfPCell tableCell = new PdfPCell(table); tableCell.setFixedHeight(tableHeight); form.replacePushbuttonField("table", tableCell); ``` 其中,`calculateRows()`方法用于计算单元格所需的行数,示例代码如下: ```java private int calculateRows(String text, float width) { Font font = new Font(Font.FontFamily.HELVETICA, 12f); ColumnText ct = new ColumnText(null); ct.setSimpleColumn(0, 0, width, 10000); ct.addElement(new Paragraph(text, font)); int status = 0; try { status = ct.go(true); } catch (DocumentException e) { e.printStackTrace(); } return status == ColumnText.NO_MORE_COLUMN ? ct.getYLine() / 12 + 1 : 1; } ``` 在以上示例代码中,我们首先获取表格的行数和列数,然后遍历每个单元格,计算出每个单元格所需的行数,并根据每个单元格所需的行数以及每行所需的最大行数设置表格的高度,最后遍历每个单元格并设置单元格的高度。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值