转换的HTML代码的字符串为PDF格式填补象文档的一个段落的iText的元件,具有itext5,xmlworker

问题描述 投票:0回答:1

我需要字符串转换为填补像一个段落的iText的元素。我不希望创建的转换一个新的文档,我只是希望它接受一个字符串,返回的东西我可以添加为PDF文档的一部分。输入我需要转换的一个例子是:

<h1>Hello<h1></br>
<h3>This is a test to demonstrate a simple html code I just need to convert</h3>
pdf itext xmlworkerhelper
1个回答
0
投票

你可以分析你的HTML字符串元素,这些元素添加到一个段落(顺便说一下,我修为测试你的HTML字符串):

String html = "<h1>Hello</h1><br/>\n" + 
        "<h3>This is a test to demonstrate a simple html code I just need to convert</h3>";
String css = null;
ElementList elements = XMLWorkerHelper.parseToElementList(html, css);
Paragraph paragraph = new Paragraph();
for (Element element : elements) {
    paragraph.add(element);
}

(从CreatePdf测试testHtmlToParagraph

并称paragraph到iText的后Document你:

screen shot

一个稍微更有趣css

String css = "h1 { background-color: lightblue; font-size: 20pt} h3 {font-family: verdana; text-align: center; color: red;}";

你得到

screen shot

© www.soinside.com 2019 - 2024. All rights reserved.