Image width and height are not set when using XMLWorkerHelper in Itext

Hi, I am trying to add an image to my pdf. This is an addition, but the problem is that I cannot set the width and height of the user. I use XMLWorkerHelperto convert HTML code and write it in PDF format.

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 set the width and height to both 50 and 75 pixels. But the original image is added to the PDF. How can I fix this.

+4
source share
1 answer

XMLWorker does not support css width and height properties on images [1].

The default image tag processor (i.e. com.itextpdf.tool.xml.html.Image) uses the width / height attributes for the tag. So there are two solutions:

  • (, , CssAplier) ( librabry ) :
  • img, :

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

.

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

+3

All Articles