繁体   English   中英

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

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

嗨,我正在尝试将图像添加到我的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();
}

在这里,我将宽度和高度设置为50和75像素。 但原始图像正在添加到PDF中。 我怎么能纠正这个。

XMLWorker不支持图像上的宽度和高度css属性[1]。

默认的图像标记处理器(即com.itextpdf.tool.xml.html.Image )使用标记的宽度/高度属性。 所以有两种解决方案:

  1. 为图像编写自己的图像标记处理器(可能还有CssAplier)(请参阅librabry源代码了解详细信息),或者只是:
  2. 使用img标签的高度和宽度属性,例如:

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

第二种解决方案更容易,在大多数情况下应该足够了。

[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