com.aspose.words

Class HtmlVersion

  • java.lang.Object
    • com.aspose.words.HtmlVersion
public class HtmlVersion 
extends java.lang.Object

Utility class containing constants. Indicates the version of HTML is used when saving the document to SaveFormat.HTML and SaveFormat.MHTML formats.

Example:

Shows how to display a DOCTYPE heading when converting documents to the Xhtml 1.0 transitional standard.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.writeln("Hello world!");

HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.HTML);
{
    options.setHtmlVersion(HtmlVersion.XHTML);
    options.setExportXhtmlTransitional(showDoctypeDeclaration);
    options.setPrettyFormat(true);
}

doc.save(getArtifactsDir() + "HtmlSaveOptions.ExportXhtmlTransitional.html", options);

// Our document will only contain a DOCTYPE declaration heading if we have set the "ExportXhtmlTransitional" flag to "true".
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlSaveOptions.ExportXhtmlTransitional.html"), StandardCharsets.UTF_8);

if (showDoctypeDeclaration)
    Assert.assertTrue(outDocContents.contains(
            "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\r\n" +
                    "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n" +
                    "<html xmlns=\"http://www.w3.org/1999/xhtml\">"));
else
    Assert.assertTrue(outDocContents.contains("<html>"));

Example:

Shows how to save a document to a specific version of HTML.
Document doc = new Document(getMyDir() + "Rendering.docx");

HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.HTML);
{
    options.setHtmlVersion(htmlVersion);
    options.setPrettyFormat(true);
}

doc.save(getArtifactsDir() + "HtmlSaveOptions.HtmlVersions.html", options);

// Our HTML documents will have minor differences to be compatible with different HTML versions.
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlSaveOptions.HtmlVersions.html"), StandardCharsets.UTF_8);

switch (htmlVersion) {
    case HtmlVersion.HTML_5:
        Assert.assertTrue(outDocContents.contains("<a id=\"_Toc76372689\"></a>"));
        Assert.assertTrue(outDocContents.contains("<a id=\"_Toc76372689\"></a>"));
        Assert.assertTrue(outDocContents.contains("<table style=\"-aw-border-insideh:0.5pt single #000000; -aw-border-insidev:0.5pt single #000000; border-collapse:collapse\">"));
        break;
    case HtmlVersion.XHTML:
        Assert.assertTrue(outDocContents.contains("<a name=\"_Toc76372689\"></a>"));
        Assert.assertTrue(outDocContents.contains("<ul type=\"disc\" style=\"margin:0pt; padding-left:0pt\">"));
        Assert.assertTrue(outDocContents.contains("<table cellspacing=\"0\" cellpadding=\"0\" style=\"-aw-border-insideh:0.5pt single #000000; -aw-border-insidev:0.5pt single #000000; border-collapse:collapse\">"));
        break;
}

Field Summary
static final intXHTML = 0
Saves the document in compliance with the XHTML 1.0 Transitional standard.
static final intHTML_5 = 1
Saves the document in compliance with the HTML 5 standard.
 

    • Field Detail

      • XHTML = 0

        public static final int XHTML
        Saves the document in compliance with the XHTML 1.0 Transitional standard.

        Aspose.Words aims to output XHTML according to the XHTML 1.0 Transitional standard, but the output will not always validate against the DTD. Some structures inside a Microsoft Word document are hard or impossible to map to a document that will validate against the XHTML schema. For example, XHTML does not allow nested lists (UL cannot be nested inside another UL element), but in Microsoft Word document multilevel lists occur quite often.

      • HTML_5 = 1

        public static final int HTML_5
        Saves the document in compliance with the HTML 5 standard.