Learn how to convert an HTML file to Word DOCX, DOC, DOT, DOCM etc. in Java: https://blog.aspose.com/2021/11/29/convert-html-to-word-in-java/
Last active
November 30, 2021 13:43
-
-
Save aspose-com-gists/6292eed8c4680f319ff8c909e6728582 to your computer and use it in GitHub Desktop.
Convert HTML Files to Word DOCX, DOC, DOCM, etc. in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a new document | |
Document doc = new Document(); | |
// Create a document builder | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Insert HTML | |
builder.insertHtml("<ul>\r\n" + | |
"<li>Item1</li>\r\n" + | |
"<li>Item2</li>\r\n" + | |
"</ul>"); | |
// Save as DOCX | |
doc.save("html-string-to-word.docx", SaveFormat.DOCX); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Load HTML file using Document class | |
Document document = new Document("template.html"); | |
// Convert HTML file to Word DOCX format | |
document.save("output.docx", SaveFormat.DOCX); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create and initialize URL | |
URL oracleURL = new URL("https://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html"); | |
// Get web page as input stream | |
InputStream is = oracleURL.openStream(); | |
// Initialize HTML load options | |
HtmlLoadOptions htmloptions = new HtmlLoadOptions(); | |
// Load stream into Document object | |
Document doc = new Document(is, htmloptions); | |
// Save as DOCX | |
doc.save("output.docx", SaveFormat.DOCX); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment