Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To convert HTML to DOCX in Java, load the source with HTMLDocument, create DocSaveOptions, and call
Converter.convertHTML(). You can also pass an HTML string directly to a supported overload and convert it in one statement.
DOCX is an editable Microsoft Word document format that can contain text, tables, and images. Aspose.HTML for Java converts HTML content to DOCX with the convertHTML() methods and lets you configure the output through DocSaveOptions.
The following example passes an HTML string, base URI, DocSaveOptions, and output path directly to Converter.convertHTML():
1// Convert HTML to DOCX in one line using Java
2
3// Invoke the convertHTML() method to convert HTML to DOCX
4Converter.convertHTML("<h1>Convert HTML to DOCX!</h1>", ".", new DocSaveOptions(), "convert-with-single-line.docx");The base URI ("." in the example) is used to resolve relative resources referenced by the HTML content. The result is saved as convert-with-single-line.docx.
Follow these steps to convert an existing HTML file:
DocSaveOptions object for DOCX output.Converter.convertHTML(document, options, outputPath) to convert HTML to DOCX.In this example, the input is canvas.html, and the result is saved as canvas-output.docx.
1// Convert HTML to DOCX using Java
2
3// Initialize an HTML document from a file
4HTMLDocument document = new HTMLDocument("canvas.html");
5
6// Initialize DocSaveOptions
7DocSaveOptions options = new DocSaveOptions();
8
9// Convert HTML to DOCX
10Converter.convertHTML(document, options, "canvas-output.docx");Download complete examples and data files from GitHub.
The DocSaveOptions class controls DOCX page layout, background, CSS processing, font embedding, and the resolution of internal images.
| Method | Use it to |
|---|---|
setBackgroundColor(value) | Set the color that fills each output page. The default is transparent. |
getCss() | Access CSS processing options. |
setDocumentFormat(value) | Set the output document format. The default is DOCX. |
setFontEmbeddingRule(value) | Configure font embedding. The default rule is None. |
setHorizontalResolution(value) | Set horizontal resolution for internal images. The default is 300 dpi. |
setVerticalResolution(value) | Set vertical resolution for internal images. The default is 300 dpi. |
getPageSetup() | Access the page setup used to configure output page size and margins. |
Use DocSaveOptions when the DOCX output requires a specific page size or margins:
HTMLDocument.DocSaveOptions.getPageSetup().setAnyPage(...) to set the page size and margins.Converter.convertHTML().The following example converts canvas.html to canvas-output-options.docx with a 600 × 400 pixel page and 10-pixel margins:
1// Convert HTML to DOCX in Java with custom page size and margins
2
3// Initialize an HTML document from a file
4HTMLDocument document = new HTMLDocument("canvas.html");
5
6// Initialize DocSaveOptions. Set up the pag size 600x400 pixels and margins
7DocSaveOptions options = new DocSaveOptions();
8options.getPageSetup().setAnyPage(new Page(new Size(600, 400), new Margin(10, 10, 10, 10)));
9
10// Convert HTML to DOCX
11Converter.convertHTML(document, options, "canvas-output-options.docx");Yes. Use the free online HTML to DOCX Converter for a quick manual conversion. Aspose.HTML for Java is a commercial API for programmatic conversion and can be evaluated before licensing; see Licensing.
HTML and DOCX use different layout models, so complex CSS may not map identically to Word document layout. Use DocSaveOptions to configure page size and margins, then verify the resulting DOCX for your source content.
Use the free online HTML to DOCX Converter for quick manual conversion without installing software.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.