4

Hi i'm trying to add an image to my 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.

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. But the original image is getting added to the PDF. How can i rectify this.

jmihalicza
  • 2,083
  • 12
  • 20
Sathesh S
  • 1,253
  • 3
  • 23
  • 54
  • Can you please post a complete working program? For example, your code snippet doesn't show how you are setting the width and height. – vy32 Nov 13 '13 at 15:50
  • I'm setting the width and height in the string itself. You can see the String image. – Sathesh S Nov 14 '13 at 09:00
  • Please post an entire program, rather than a code snippet. – vy32 Nov 14 '13 at 14:02

1 Answers1

4

XMLWorker doesn't support width and height css properties on images[1].

The default image tag processor (ie com.itextpdf.tool.xml.html.Image) uses width/height attributes of tag. 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:
  2. use height and width attributes of img tag, for example:

    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

bartnikiewi.cz
  • 131
  • 1
  • 4