Convert HTML to DOCX in Java
A DOCX file is a Microsoft Word document that typically contains text but can contain a wide range of data, including tables, graphics, video, sounds, etc. The DOCX file is highly editable, easy to use, and manageable in size. This format is popular because of the variety of options it offers users to write any documents and is available through numerous programs.
The ability to easily and reliably convert HTML documents to other formats is one of the main features of Aspose.HTML for Java. In this article, you find information on how to convert an HTML document into a ( DOCX) file format using convertHTML()
methods of the
Converter class, and how to apply DocSaveOptions
.
HTML to DOCX by a single line of Java code
HTML to DOCX conversion is a highly sought-after feature, and Aspose.HTML for Java offers an easy solution. Through static methods in the Converter class, users can convert an HTML document into a DOCX file with just a single line of code!
1// Invoke the ConvertHTML() method to convert HTML to DOCX
2Converter.convertHTML("<h1>Convert HTML to DOCX!</h1>", ".", new DocSaveOptions(), $o("convert-with-single-line.docx"));
Convert HTML to DOCX
Let’s walk through the step-by-step instructions for a simple HTML to DOCX conversion scenario:
- Load an HTML file using one of HTMLDocument() constructors of the
HTMLDocument class. You can load HTML from a file, HTML code, stream, or URL (see the
Create HTML Document article). In the example, we use
HTMLDocument(address)
constructor that initializes an HTML document from a file. - Create a new
DocSaveOptions
object. - Use the
сonvertHTML(document, options, savePath)
method of the Converter class to save HTML as a DOCX file.
Please review the following Java code snippet, which shows the HTML to DOCX conversion process with step-by-step instructions:
1// Initialize an HTML document from the file
2HTMLDocument document = new HTMLDocument($o("canvas.html"));
3
4// Initialize DocSaveOptions
5DocSaveOptions options = new DocSaveOptions();
6
7// Convert HTML to DOCX
8Converter.convertHTML(document, options, $o("canvas-output.docx"));
You can download the complete examples and data files from GitHub.
Convert HTML to DOCX using DocSaveOptions
With Aspose.HTML for Java, you can convert files programmatically with full control over a wide range of conversion parameters. To convert HTML to DOCX with DocSaveOptions
specifying, you should follow a few steps:
- Load an HTML file using one of the
HTMLDocument()
constructors of the HTMLDocument class. - Create a new
DocSaveOptions
object and specify the required properties. Use thegetPageSetup()
method to specify the page size and margins for the output document. - Use the
сonvertHTML()
method of the Converter class to save HTML as a DOCX file.
The following Java example shows how to use DocSaveOptions
and create a DOCX file with custom page size and margins:
1// Initialize an HTML document from the file
2HTMLDocument document = new HTMLDocument($i("canvas.html"));
3
4// Initialize DocSaveOptions. Set up the pag size 600x400 pixels and margins
5DocSaveOptions options = new DocSaveOptions();
6options.getPageSetup().setAnyPage(new Page(new Size(600, 400), new Margin(10, 10, 10, 10)));
7
8// Convert HTML to DOCX
9Converter.convertHTML(document, options, $o("canvas-output-options.docx"));
The DocSaveOptions()
constructor initializes an instance of the DocSaveOptions
class that is passed to convertHTML()
method. The method takes the document
, options
, output file path savePath
and performs the conversion operation.
Conclusion
Converting HTML to DOCX is an essential feature for creating professional, editable documents from web content. Aspose.HTML for Java provides a simple and effective way to achieve this with the Converter
and DocSaveOptions
classes.
With its simple API and customizable options, Aspose.HTML for Java allows developers to create DOCX documents that meet specific requirements, streamlining workflows for content management and document creation.
You can download the complete examples and data files from GitHub.
Aspose.HTML offers a free online HTML to DOCX Converter that converts HTML to DOCX with high quality, easy and fast. Just upload, convert your files and get the result in a few seconds!