Insert HTML into Word, including SVG

Insert HTML into Word, including SVG. SVG content is not displayed in Word.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertHtml("",true);
doc.save("test.docx");

ASPOSE.Word version 20.2 can output SVG to Word to display content normally, but the latest version 25.7 does not display it

test.zip (10.7 KB)

@dhzhao2016

To insert HTML into a Word document, including SVG content, you can utilize the HtmlLoadOptions class to specify how the HTML should be processed. In version 25.7 of Aspose.Words, SVG content may not display as expected due to changes in SVG handling compared to earlier versions.

Cause: The issue with SVG content not displaying correctly is likely related to the way Aspose.Words processes SVG images in this version.

Solution: To ensure SVG content is processed correctly, follow these steps:

  1. Use HtmlLoadOptions: Create an instance of HtmlLoadOptions and set the ConvertSvgToEmf property to true. This setting forces the conversion of SVG images to EMF format, which is more compatible with Word documents.

  2. Load the HTML: Use the Document class to load your HTML content along with the specified load options.

  3. Save the Document: Finally, save the document in the desired format.

Here’s a code example to illustrate this:

import com.aspose.words.*;

public class HtmlToWord {
    public static void main(String[] args) throws Exception {
        // Create a new document
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);

        // HTML content with SVG
        String htmlContent = "<html><body><h1>Hello World</h1><svg width='100' height='100'><circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' /></svg></body></html>";

        // Set up HTML load options
        HtmlLoadOptions loadOptions = new HtmlLoadOptions();
        loadOptions.setConvertSvgToEmf(true); // Convert SVG to EMF

        // Load HTML into the document
        doc = new Document(new ByteArrayInputStream(htmlContent.getBytes(StandardCharsets.UTF_8)), loadOptions);

        // Save the document
        doc.save("output.docx");
    }
}

Next Steps: Test this approach to see if it resolves the issue with SVG not displaying correctly in your Word document. If you continue to experience problems, check the Aspose documentation for any updates or changes related to SVG handling in the latest versions.

If you have any further questions or need additional assistance, feel free to ask!

@dhzhao2016
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-28510

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

The issues you have found earlier (filed as WORDSNET-28510) have been fixed in this Aspose.Words for Java 25.9 update.