有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java iText的PDF格式不好

我有以下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>

但是图像显示在文本后面而不是下面

怎么了

如果iText无法处理它-哪个库更好

这是我的代码:

// 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();

共 (2) 个答案

  1. # 1 楼答案

    你对这个HTML应用了css吗?你用浏览器(哪种)以任何其他方式查看这个HTML了吗?它的呈现方式与您在这里描述的一样:http://jsfiddle.net/TjUSJ/

    也许您想删除<div>上的高度样式属性?它看起来像是在中间渲染,但实际上是在距顶部50像素处渲染。看到这把没有height样式的小提琴了吗:http://jsfiddle.net/TjUSJ/1/

    另外,请记住^{} tag is deprecated

  2. # 2 楼答案

    问题是我使用的是旧版本

    我切换到了最后一个版本——5.1.2,效果很好