java image zoom_Java Image.setAbsolutePosition方法代码示例

这段代码展示了如何使用iText库在PDF中添加图片,并使其可搜索。通过调整Image的大小和设置绝对位置,将图片适配到PDF中。如果需要创建可搜索的PDF,还会在图片下方添加不可见的文本,以实现文字搜索功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import com.itextpdf.text.Image; //导入方法依赖的package包/类

/**

* The addImageToPdf method is used to add image to pdf and make it searchable by adding image text in invisible mode

* w.r.t parameter 'isPdfSearchable' passed.

*

* @param pdfWriter {@link PdfWriter} writer of pdf in which image has to be added

* @param htmlUrl {@link HocrPage} corresponding html file for fetching text and coordinates

* @param imageUrl {@link String} url of image to be added in pdf

* @param isPdfSearchable true for searchable pdf else otherwise

* @param widthOfLine

*/

private void addImageToPdf(PdfWriter pdfWriter, HocrPage hocrPage, String imageUrl, boolean isPdfSearchable, final int widthOfLine) {

if (null != pdfWriter && null != imageUrl && imageUrl.length() > 0) {

try {

LOGGER.info("Adding image" + imageUrl + " to pdf using iText");

Image pageImage = Image.getInstance(imageUrl);

float dotsPerPointX = pageImage.getDpiX() / PDF_RESOLUTION;

float dotsPerPointY = pageImage.getDpiY() / PDF_RESOLUTION;

PdfContentByte pdfContentByte = pdfWriter.getDirectContent();

pageImage.scaleToFit(pageImage.getWidth() / dotsPerPointX, pageImage.getHeight() / dotsPerPointY);

pageImage.setAbsolutePosition(0, 0);

// Add image to pdf

pdfWriter.getDirectContentUnder().addImage(pageImage);

pdfWriter.getDirectContentUnder().add(pdfContentByte);

// If pdf is to be made searchable

if (isPdfSearchable) {

LOGGER.info("Adding invisible text for image: " + imageUrl);

float pageImagePixelHeight = pageImage.getHeight();

Font defaultFont = FontFactory.getFont(FontFactory.HELVETICA, 8, Font.BOLD, CMYKColor.BLACK);

// Fetch text and coordinates for image to be added

Map textCoordinatesMap = getTextWithCoordinatesMap(hocrPage, widthOfLine);

Set ketSet = textCoordinatesMap.keySet();

// Add text at specific location

for (String key : ketSet) {

int[] coordinates = textCoordinatesMap.get(key);

float bboxWidthPt = (coordinates[2] - coordinates[0]) / dotsPerPointX;

float bboxHeightPt = (coordinates[3] - coordinates[1]) / dotsPerPointY;

pdfContentByte.beginText();

// To make text added as invisible

pdfContentByte.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE);

pdfContentByte.setLineWidth(Math.round(bboxWidthPt));

// Ceil is used so that minimum font of any text is 1

// For exception of unbalanced beginText() and endText()

if (bboxHeightPt > 0.0) {

pdfContentByte.setFontAndSize(defaultFont.getBaseFont(), (float) Math.ceil(bboxHeightPt));

} else {

pdfContentByte.setFontAndSize(defaultFont.getBaseFont(), 1);

}

float xCoordinate = (float) (coordinates[0] / dotsPerPointX);

float yCoordinate = (float) ((pageImagePixelHeight - coordinates[3]) / dotsPerPointY);

pdfContentByte.moveText(xCoordinate, yCoordinate);

pdfContentByte.showText(key);

pdfContentByte.endText();

}

}

pdfContentByte.closePath();

} catch (BadElementException badElementException) {

LOGGER

.error("Error occurred while adding image" + imageUrl + " to pdf using Itext: "

+ badElementException.toString());

} catch (DocumentException documentException) {

LOGGER.error("Error occurred while adding image" + imageUrl + " to pdf using Itext: " + documentException.toString());

} catch (MalformedURLException malformedURLException) {

LOGGER.error("Error occurred while adding image" + imageUrl + " to pdf using Itext: "

+ malformedURLException.toString());

} catch (IOException ioException) {

LOGGER.error("Error occurred while adding image" + imageUrl + " to pdf using Itext: " + ioException.toString());

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值