1

I have the following HTML:

<div align='center' style='height:50px'>
    <H1>A Simple Sample Web Page</H1>
    <IMG SRC='http://sheldonbrown.com/images/scb_eagle_contact.jpeg'>
    <H4>By Sheldon Brown</H4>
    <H2>Demonstrating a few HTML features</H2>
</div>
HTML is really a very simple language. '
<P>
'command, which will insert a blank line.If you would like to make a link or 
bookmark to this page, the URL is:
<BR> 
http://sheldonbrown.com/web_sample1.html
</center>

But the image appears behind the text instead of below!

What's wrong?

if iText cannot handle it - which library is better?

This is my code:

// step 1
        Document document = new Document();

        // step 2
        PdfWriter.getInstance(document, new FileOutputStream("C:\\hello-world.pdf"));

        document.open();
        String content = "<div align='center' style='height:50px'><H1>A Simple Sample Web Page</H1><IMG SRC='http://sheldonbrown.com/images/scb_eagle_contact.jpeg'><H4>By Sheldon Brown</H4><H2>Demonstrating a few HTML features</H2></div>HTML is really a very simple language. '<P>' command, which will insert a blank line.If you would like to make a link or bookmark to this page, the URL is:<BR> http://sheldonbrown.com/web_sample1.html</center>";

        // use the snippet for the PDF document
        List<Element> objects = HTMLWorker.parseToList(new StringReader(content), null);
        for (Element element : objects)
            document.add(element);
        document.close();
Xavi López
  • 27,550
  • 11
  • 97
  • 161
Dejell
  • 13,947
  • 40
  • 146
  • 229

2 Answers2

1

Do you have any css applied to this HTML? Have you achieved to view this HTML in any other way with a browser (which) ? It renders like you describe here: http://jsfiddle.net/TjUSJ/.

Maybe you want to remove the height styling property on that <div>? It seems like it renders on the middle, but it is actually rendernig at 50px from the top. See this other fiddle, without height styling: http://jsfiddle.net/TjUSJ/1/

Also, remember that the <center> tag is deprecated

Community
  • 1
  • 1
Xavi López
  • 27,550
  • 11
  • 97
  • 161
  • Yes - I tried to remove the height (as you mentioned in your second example)- but still it doesn't work! – Dejell Nov 10 '11 at 12:29
  • I'm not that familiar with iText. I've used [Flying Saucer XHTML Renderer](http://code.google.com/p/flying-saucer/) with good results, though. It uses iText behind the scenes. Take into account that it'll require your document to be XHTML – Xavi López Nov 10 '11 at 15:19
1

The problem was that I was using an old version.

I switched to the last one - 5.1.2 and it works!

Dejell
  • 13,947
  • 40
  • 146
  • 229