我需要字符串转换为填补像一个段落的iText的元素。我不希望创建的转换一个新的文档,我只是希望它接受一个字符串,返回的东西我可以添加为PDF文档的一部分。输入我需要转换的一个例子是:
<h1>Hello<h1></br>
<h3>This is a test to demonstrate a simple html code I just need to convert</h3>
你可以分析你的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
你:
一个稍微更有趣css
:
String css = "h1 { background-color: lightblue; font-size: 20pt} h3 {font-family: verdana; text-align: center; color: red;}";
你得到