Is there an easy way to tak a snippet of xhtml and insert it into a document, for example:
test paragraph.
hi | bye |
Is there an easy way to tak a snippet of xhtml and insert it into a document, for example:
test paragraph.
hi | bye |
Ive started by trying to load the html snippette into a Document, but I am getting a strange exception:
abc def.
”);abc def.
”);Hi
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Thanks for your request. you can use DocumentBuilder.insertHtml method to achieve this. Please see the following link for more information:
To load HTML strings try using the following code:
String html = " abc def.
InputStream stream = new ByteArrayInputStream(html.getBytes("UTF-8"));
Document doc = new Document(stream);
doc.save("C:\\temp\\out.doc");
Best regards.
It seems that your example does work when it is run from a simple java test class like what is below, but it fails when it is executed in a servlet under a Tomcat application server, still trying to track down why!
abc def.
”;abc def.
”;Hi
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Thank you for additional information. Actually, you can use DocumentBuilder class to build whole document. I think, this would be easier than building document using DOM.
For instance, you can try using the following code:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Specify page setup
builder.getPageSetup().setSectionStart(SectionStart.NEW_PAGE);
builder.getPageSetup().setPaperSize(PaperSize.A4);
// insert headings
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);
builder.writeln("This is my heading");
// Insert some HTML
String html = " abc def.
builder.insertHtml(html);
Please see the following link to learn more about DocumentBuilder:
Best regards.
Hi Alexey, thanks for the pointers, however I have already written a few thousand lines of code that generate my document without using DocumentBuilder, I am not quite sure how I would use document builder at the same time as using the basic Document class.
Hi Jacob,
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Thank you for additional information. I think, there is two ways you can achieve what you need:
1. Create an empty Document and DocumentBuilder and insert your HTML into this empty document. Then you can insert this document where you need.
2. In your code, you can move DocuemntBuilder cursor to the necessary position and insert HTML
Best regards.