简体   繁体   English

在Itext中使用XMLWorkerHelper时未设置图像宽度和高度

[英]Image width and height not getting set while using XMLWorkerHelper in Itext

Hi i'm trying to add an image to my pdf. 嗨,我正在尝试将图像添加到我的PDF格式。 It's getting added but the problem is i'm not able to set user defined width and height. 它已被添加,但问题是我无法设置用户定义的宽度和高度。 I'm using XMLWorkerHelper to convert the HTML code and write it in PDF. 我正在使用XMLWorkerHelper转换HTML代码并将其写入PDF。

try {
String image="<img alt=\"Not Loading\" src=\"C:\\Users\\sathesh_S\\Desktop\\Itext\\football.jpg\" style=\"width: 50px; height: 75px\" />";    
OutputStream file = new FileOutputStream(new File("C:\\Test.pdf"));
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, file);
document.open();
InputStream is = new ByteArrayInputStream(image.getBytes());
XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);
document.close();
file.close();
}
catch (Exception e) 
{
e.printStackTrace();
}

Here i'm setting the width and height as 50 and 75 pixel. 在这里,我将宽度和高度设置为50和75像素。 But the original image is getting added to the PDF. 但原始图像正在添加到PDF中。 How can i rectify this. 我怎么能纠正这个。

XMLWorker doesn't support width and height css properties on images[1]. XMLWorker不支持图像上的宽度和高度css属性[1]。

The default image tag processor (ie com.itextpdf.tool.xml.html.Image ) uses width/height attributes of tag. 默认的图像标记处理器(即com.itextpdf.tool.xml.html.Image )使用标记的宽度/高度属性。 So there are two solutions: 所以有两种解决方案:

  1. write your own image tag processor (and probably CssAplier) for images (consult librabry source code for details), or simply: 为图像编写自己的图像标记处理器(可能还有CssAplier)(请参阅librabry源代码了解详细信息),或者只是:
  2. use height and width attributes of img tag, for example: 使用img标签的高度和宽度属性,例如:

     String image="<img src=\\"football.jpg\\" width=\\"50px\\" height=\\"75px\\"/>"; 

Second solution is much easier and should be enough in most cases. 第二种解决方案更容易,在大多数情况下应该足够了。

[1] http://demo.itextsupport.com/xmlworker/itextdoc/CSS-conformance-list.htm [1] http://demo.itextsupport.com/xmlworker/itextdoc/CSS-conformance-list.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM