Aspose.words for java将html转换成word时将H标签生成导航

您好,我使用的是Aspose.words for java 23.1版本,将html转换成word时会为html页面中的H标签在word中创建左侧导航,但是当H标签级别大于6级以后就不再创建导航,请问有办法解除这个限制,自定义这个最大值吗?

@yyl5201314, 您能否附上一个示例 HTML 文件来帮助我们重现该问题?

你好,这是我写的demo示例:

public class Demo {
    /**
     * license证书内容
     */
    private static final String LICENSE_CONTENT = "";

    private static final String htmlStrPart1 = "<html lang=\"zh-cn\" xml:lang=\"zh-cn\">\n" + " <head>\n"
        + "  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />\n"
        + "  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n"
        + "  <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\" />\n"
        + "  <meta name=\"copyright\" content=\"(C) 版权 2005\" />\n"
        + "  <meta name=\"DC.rights.owner\" content=\"(C) 版权 2005\" />\n"
        + "  <meta name=\"DC.Type\" content=\"topic\" />\n" + "  <meta name=\"DC.Title\" content=\"软件描述\" />\n"
        + "  <meta name=\"product\" content=\"\" />\n"
        + "  <meta name=\"DC.Relation\" scheme=\"URI\" content=\"../../../CaaS/CaaS/CPI_NODE/cn_77_11_112066.html\" />\n"
        + "  <meta name=\"DC.Relation\" scheme=\"URI\" content=\"../../../CaaS/Solution/User_Manuals/TV_RCS/cn_87_23_00003.html\" />\n"
        + "  <meta name=\"DC.Relation\" scheme=\"URI\" content=\"../../../CaaS/OMP/Licen_Desc/cn_87_16_00000.html\" />\n"
        + "  <meta name=\"DC.Relation\" scheme=\"URI\" content=\"../../../CaaS/Solution/User_Manuals/TV_RCS/cn_87_23_00098_4.html\" />\n"
        + "  <meta name=\"prodname\" content=\"\" />\n" + "  <meta name=\"version\" content=\"\" />\n"
        + "  <meta name=\"brand\" content=\"02 Home Presence V500R018C00 CPI\" />\n"
        + "  <meta name=\"DC.Creator\" content=\"h00173922,lwx313513,dwx539748,c00302454,zwx537498,l00167185,ywx527539,zwx421388,\" />\n"
        + "  <meta name=\"DC.Publisher\" content=\"20170921\" />\n"
        + "  <meta name=\"OWNER\" content=\"h00173922,lwx427009,dwx539748,c00302454,zwx537498,l00167185,ywx527539,zwx421388,\" />\n"
        + "  <meta name=\"IA\" content=\"\" />\n" + "  <meta name=\"SE\" content=\"\" />\n"
        + "  <meta name=\"TE\" content=\"\" />\n" + "  <meta name=\"DC.Format\" content=\"XHTML\" />\n"
        + "  <meta name=\"DC.Identifier\" content=\"ZH-CN_TOPIC_0081105543\" />\n"
        + "  <meta name=\"DC.Language\" content=\"zh-cn\" />\n" + "  <title>软件描述</title>\n"
        + "  <style type=\"text/css\">body { padding: 0; margin: 0; width: 100%; font-family: FZLTHJW; color: #333; font-size: 14px; line-height: 1.7em;}.msgph { font-family: FZLTHJW;}.h1innerspan { font-family: FZLTHJW;}.independenth1 { color: #333333; margin-top: 10px; margin-bottom: 10px; font-size: 28px; font-weight: bold; font-family: FZLTHJW;}.rowlinecopyright { color: red; margin-top: 10pt;}.unresolved { background-color: skyblue;}.noTemplate { background-color: yellow;}.base { background-color: #ffffff;}.nested0 { margin-top: 1em;}.p { margin-top: .6em; margin-bottom: .6em;}p { margin-top: .5em; margin-bottom: .5em;}.note p { margin-top: .5em; margin-bottom: .5em;}.tip p { margin-top: .5em; margin-bottom: .5em;}.danger p { margin-top: .5em; margin-bottom: .5em;}.warning p { margin-top: .5em; margin-bottom: .5em;}.notice p { margin-top: .5em; margin-bottom: .5em;}.caution p { margin-top: .5em; margin-bottom: .5em;}.attention p { margin-top: .5em; margin-bottom: .5em;}table p { margin-top: .2em; margin-bottom: .2em;}table .p { margin-top: .4em; margin-bottom: .2em;}.figcap { font-size: 10pt;}img { margin-top: .3em;}.figdesc { font-style: normal;}.figborder { border-style: solid; padding-left: 3px; border-width: 2px; padding-right: 3px; margin-top: 1em; border-color: Silver;}.figsides { border-left: 2px solid; padding-left: 3px; border-right: 2px solid; padding-right: 3px; margin-top: 1em; border-color: Silver;}.figtop { border-top: 2px solid; margin-top: 1em; border-color: Silver;}.figbottom { border-bottom: 2px solid; border-color: Silver;}.figtopbot { border-top: 2px solid; border-bottom: 2px solid; margin-top: 1em; border-color: Silver;}.fignone { font-size: 10pt; margin-top: 8pt; margin-bottom: 8pt; page-break-inside: avoid;}.familylinks { margin-top: 1.5em; margin-bottom: 1em;}.ullinks { margin: 3em 0; padding-left: 2em; margin-left: 2em; border-left: 3px solid #ddd;}.ullinks dl { margin-bottom: 1.6em;}.ullinks dl dt { font-size: 16px; margin-bottom: 5px;}.ullinks dl dd { font-size: 12px; color: #666;}.linklist { margin-bottom: 1em;}.linklistwithchild { margin-left: 1.5em; margin-bottom: 1em;}.sublinklist { margin-left: 1.5em; margin-bottom: 1em;}.relconcepts { margin-top: .6em; margin-bottom: .6em;}.reltasks { margin-top: .6em; margin-bottom: .6em;}.relref { margin-top: .6em; margin-bottom: .6em;}.relinfo { margin-top: .6em; margin-bottom: .6em;}.breadcrumb { font-size: smaller; margin-bottom: .6em;}.prereq { margin-left: 20px;}.parentlink { margin-top: .6em; margin-bottom: .6em;}.nextlink { margin-top: .6em; margin-bottom: .6em;}.previouslink { margin-top: .6em; margin-bottom: .6em;}.topictitle1 { margin-top: 0; margin-bottom: 1em; font-size: 14pt; color: #007af4;}.topictitle2 { margin-top: 1pc; margin-bottom: .45em; font-size: 1.17em; color: #007af4;}.topictitle3 { margin-top: 1pc; margin-bottom: .17em; font-size: 1.17em; font-weight: bold; color: #007af4;}.topictitle4 { margin-top: .83em; font-size: 1.17em; font-weight: bold;}.topictitle5 { font-size: 1.17em; font-weight: bold;}.topictitle6 { font-size: 1.17em; font-style: italic;}.sectiontitle { margin-top: 30px; margin-bottom: 15px; color: #333; font-size: 20px; font-weight: bold;}.section { margin-top: 1em; margin-bottom: 1em;}div.sectionTab { border: none; padding: 15px; position: relative; left: -1px; margin: 0;}.example { margin-top: 1em; margin-bottom: 1em;}.sectiontitle2contents:link { color: #007af4;}.sectiontitle2contents:visited { color: #800080;}.note { margin-top: 1em; margin-bottom: 1em;}.notetitle { font-weight: bold;}.notelisttitle { font-weight: bold;}.tip { margin-top: 1em; margin-bottom: 1em; background-color: #ffffcc;}.tiptitle { font-weight: bold;}.fastpath { margin-top: 1em; margin-bottom: 1em; background-color: #ffffcc;}.fastpathtitle { font-weight: bold;}.important { margin-top: 1em; margin-bottom: 1em; background-color: #ffffcc;}.importanttitle { font-weight: bold;}.remember { margin-top: 1em; margin-bottom: 1em; background-color: #ffffcc;}.remembertitle { font-weight: bold;}.restriction { margin-top: 1em; margin-bottom: 1em; background-color: #ffffcc;}.restrictiontitle { font-weight: bold;}.attention { margin-top: 1em; margin-bottom: 1em;}.attentiontitle { font-weight: bold;}.dangertitle { font-weight: bold;}.danger { margin-top: 1em; margin-bottom: 1em;}.noticetitle { font-weight: bold;}.notice { margin-top: 1em; margin-bottom: 1em;}.warningtitle { font-weight: bold;}.warning { margin-top: 1em; margin-bottom: 1em;}.cautiontitle { font-weight: bold;}.caution { margin-top: 1em; margin-bottom: 1em;}ul.simple { list-style-type: none;}li ul { margin-top: .6em;}li { margin-top: .6em; margin-bottom: .6em;}.note li { margin-top: .2em; margin-bottom: .2em;}.tip li { margin-top: .2em; margin-bottom: .2em;}.danger li { margin-top: .2em; margin-bottom: .2em;}.notice li { margin-top: .2em; margin-bottom: .2em;}.warning li { margin-top: .2em; margin-bottom: .2em;}.caution li { margin-top: .2em; margin-bottom: .2em;}.attention li { margin-top: .2em; margin-bottom: .2em;}table li { margin: 0 u0021important; padding: 0 u0021important;}ol ul { list-style: disc;}ul ul { list-style: disc;}ol ul ul { list-style: disc;}ol ul { list-style-type: disc;}table ol { margin: 0 !important; padding: 0 !important; list-style: none;}table ul { margin: 0 !important; padding: 0 !important; list-style: none;}table ul ul { margin: 0; padding: 0; list-style: none;}table ol ol { margin: 0; padding: 0; list-style: none;}table ol ul { margin: 0; padding: 0; list-style: none;}table ul ol { margin: 0; margin: 0; list-style: none;}.substepthirdol { list-style-type: lower-roman;}.firstcol { font-weight: bold; text-align: left;}th { background-color: #cfcfcf;}table { margin-bottom: 1.2em; border-collapse: collapse; border-spacing: 0;}table caption { margin-top: 8px; text-align: left; margin-bottom: 6px;}.bold { font-weight: bold;}.boldItalic { font-weight: bold; font-style: italic;}.italic { font-style: italic;}.underlined { text-decoration: underline;}.var { font-style: italic;}.shortcut { text-decoration: underline;}.dlterm { font-weight: bold;}dd { margin-top: .5em; margin-bottom: .5em;}.dltermexpand { font-weight: bold; margin-top: 1em;}*[compact=\"yes\"] &gt; li { margin-top: 0;}*[compact=\"no\"] &gt; li { margin-top: .53em;}.liexpand { margin-top: 1em; margin-bottom: 1em;}.sliexpand { margin-top: 1em; margin-bottom: 1em;}.dlexpand { margin-top: 1em; margin-bottom: 1em;}.ddexpand { margin-top: 1em; margin-bottom: 1em;}.stepexpand { margin-top: .3em; margin-bottom: .3em;}.substepexpand { margin-top: .3em; margin-bottom: .3em;}div.imageleft { text-align: left;}div.imagecenter { text-align: center;}div.imageright { text-align: right;}div.imagejustify { text-align: justify;}div.noblankline { text-align: center;}div.noblankline img { margin-top: 0;}pre.screen { margin-top: 2px; margin-bottom: 2px; padding: 5px; font-family: FZLTHJW; border: 0; background-color: #DDDDDD; white-space: pre-wrap; word-break: break-all;}pre.codeblock { margin-top: 2px; margin-bottom: 2px; padding: 5px; font-family: FZLTHJW; border: 0; background-color: #DDDDDD; white-space: pre-wrap; word-break: break-all;}.hrcopyright { color: #3f4e5d; margin-top: 18pt;}.hwcopyright { text-align: center; font-family: FZLTHJW;}.comment { margin: 2px 2px 2px 2px; font-family: FZLTHJW; font-size: 12px; background-color: #BBFFBB; color: #000000;}.dropdownAllButtonexpand { cursor: pointer; background-repeat: no-repeat; background-position: 0 4px; padding-left: 15px; background-image: url(icon-arrowrt.gif); text-decoration: underline; color: #007af4;}.dropdownAllButtoncollapse { cursor: pointer; background-repeat: no-repeat; background-position: 0 4px; padding-left: 15px; background-image: url(icon-arrowdn.gif); text-decoration: underline; color: #007af4;}.dropdowntitle { background-repeat: no-repeat; background-position: 0 4px; padding-left: 15px; cursor: pointer;}.dropdownexpand .dropdowntitle { background-image: url(icon-arrowdn.gif); text-decoration: underline; color: #007af4; margin: 0 0 8px 0;}.dropdowncollapse .dropdowncontext { display: none;}.dropdowncollapse .dropdowntitle { background-image: url(icon-arrowrt.gif); text-decoration: underline; color: #007af4;}.dropdowncollapsetable { border: 0;}.dropdowncollapsetable .dropdowncontext { display: none;}.dropdowncollapsetable .dropdowntitle { background-image: url(icon-arrowrt.gif); text-decoration: underline; color: #007af4;}pre { font-family: 新宋体; font-size: 12px; font-weight: normal; margin-left: 9; margin-top: 2; margin-bottom: 2;}.termcolor { color: blue; cursor: pointer;}#dhtmlgoodies_tooltip { background-color: #F0F0D2; border: 1px solid #000; position: absolute; display: none; z-index: 20000; padding: 2px; font-size: .9em; -moz-border-radius: 6px; font-family: FZLTHJW;}#dhtmlgoodies_tooltipShadow { position: absolute; background-color: #555; display: none; z-index: 10000; opacity: .7; filter: alpha(opacity=70); -khtml-opacity: .7; -moz-opacity: .7; -moz-border-radius: 6px;}.freeze { position: fixed; _position: absolute; _top: expression(eval(document.documentElement.scrollTop)); left: 10; top: 40px;}.freezeWithoutHead { position: fixed; _position: absolute; _top: expression(eval(document.documentElement.scrollTop)); left: 10; top: 0;}.topNavBar { background-color: #009DD9; position: fixed; width: 100%; left: 0; top: 0; box-shadow: 0 3px 6px rgba(0, 0, 0, 0.1); z-index: 999;}body, div, dl, dt, dd, ul, ol, li, pre, code, form, input, button, textarea, p, blockquote, th, td { margin: 0; padding: 0;}ol { margin: 1em 20px 1em 20px;}ul { margin: 1em 15px 1em 15px;}ol li { margin-top: .5em; margin-bottom: .5em;}ul li { margin-top: .5em; margin-bottom: .5em;}h1, h2, h3, h4, h5, h6 { margin: .5em 0; font-size: 100%; font-weight: normal;}* { zoom: 1;}.topNavBar .wrapBox { padding: 5px 30px;}.topNavBar .wrapBox a { color: #fff;}.topNavBar span { color: #fff;}.strong { font-weight: bold;}.fr { float: right;}a { color: #06f; outline: none; text-decoration: none;}custom_hyperlink { color: #06f; outline: none; text-decoration: none;}.topNavBar .font-st { font-weight: bold;}.font-st { font-family: FZLTHJW;}a * { cursor: pointer;}.articleBox { padding: 30px 5%; margin-top: 40px;}.articleBox a { text-decoration: underline;}.articleBoxWithoutHead { padding: 0px 5%;}.articleBoxWithoutHead a { text-decoration: underline;}h1.topicTitle-h1 { font-family: FZLTHJW; font-size: 24px; color: #333; margin-top: 10px; margin-bottom: 10px; line-height: 30px; font-weight: bold;}.footerNavBar { background-color: #DBDCDF; padding: 10px 15px; color: #666; height: 46px; position: relative;}.clearfix { zoom: 1;}.clearfix:after { content: \".\"; display: block; height: 0; clear: both; visibility: hidden;}.footerNavBar .parentlink { width: 80%; color: #333; margin-top: 0; margin-bottom: 0;}.footerNavBar .copyrightBottomBar { width: 80%; font-size: 12px; color: #999;}.footerNavBar .copyrightBottomBar_responsive { display: none;}.footerNavBar .copyrightBottomBarNoParentLink { margin-top: 13px; width: 80%; font-size: 12px; color: #999;}.footerNavBar .copyrightBottomBarNoParentLink_responsive { display: none;}.footerNavBar .bottomNavBtn { width: 200px; height: 24px; padding: 21px 15px 21px 40px; text-align: right; position: absolute; right: 0; top: 0; background: url(\"footerNavBar.jpg\") no-repeat;}.footerNavBar .bottomNavBtn a { display: block; width: 50%; line-height: 24px; height: 24px; overflow: hidden; float: left; text-align: center;}.footerNavBar a { font-weight: bold;}a:visited { color: #06f;}.tableBorder table { width: 100%; border: 0; table-layout: fixed; border-top: 1px solid #686868; border-bottom: 1px solid #686868;}.tableBorder table th { padding: 5px; background-color: #eee; font-weight: bold u0021important; border: 1px solid #686868; text-align: left;}.tableBorder table th p { font-weight: bold u0021important;}.tableBorder table td { padding: 5px; word-break: break-all; border: 1px solid #686868;}.tableNoBorder table { width: 100%; border: 0; border-top: 2px solid #666; border-bottom: 2px solid #666;}.tableNoBorder table th { padding: 5px; background-color: #eee; font-weight: bold; border: 0; border-bottom: 1px solid #ccc; text-align: left;}.tableNoBorder table td { padding: 5px; border: 0; word-break: break-all; border-bottom: 1px solid #ccc;}.tableNoBorderColor table { width: 100%; border: 0; border-top: 2px solid #005B7D; border-bottom: 2px solid #005B7D;}.tableNoBorderColor table th { padding: 5px; background-color: #009DD9; color: #fff; font-weight: bold; border: 0; border-bottom: 1px solid #0080B0;}.tableNoBorderColor table td { padding: 5px; border: 0; border-bottom: 1px solid #ccc;}.tableBorderColor table { width: 100%; border-top: 2px solid #005B7D; border-bottom: 2px solid #005B7D;}.tableBorderColor table th { padding: 5px; background-color: #009DD9; color: #fff; font-weight: bold; border: 1px solid #0080B0;}.tableBorderColor table td { padding: 5px; border: 1px solid #ccc;}table img { width: auto u0021important; height: auto u0021important; max-width: 100%; max-height: 800px;}.sectionTitleTab { background-color: #eee; margin-top: 2em; display: none;}.sectionTitleTab ul { padding: 0; margin: 0;}.sectionTitleTab li { padding: 0; margin: 0; float: left; display: inline-block; padding: 8px 0; font-size: 16px; list-style: none; *display: inline; border-top: 2px solid #eee;}.sectionTitleTab li a { color: #666; display: inline-block; padding: 0 20px; border-right: 1px solid #ddd; *display: inline;}.sectionTitleTab li:hover a { color: #009DD9;}.sectionTitleTab li.active { background-color: #fff; padding: 0; border: 1px solid #ccc; border-width: 0 1px; border-top: 2px solid #009DD9; position: relative; left: -1px;}.sectionTitleTab li.active a { color: #009DD9; padding: 8px 20px; border: 0;}.note, .tip, .fastpath, .important, .remember, .restriction, .attention, .danger, .notice, .warning, .caution { margin: 0px; padding: 5px; font-size: 12px; border-top: 0px solid #686868; border-bottom: 0px solid #686868;}.notetitle, .notelisttitle, .tiptitle, .fastpathtitle, .importanttitle, .remembertitle, .restrictiontitle, .attentiontitle, .dangertitle, .noticetitle, .warningtitle, .cautiontitle { font-weight: bold; line-height: 120%; font-size: 11px;}.notebody, .notelistbody, .tipbody, .fastpathbody, .importantbody, .rememberbody, .restrictionbody, .attentionbody, .dangerbody, .noticebody, .warningbody, .cautionbody { padding-left: 2em; line-height: 120%; margin-top: 0px;}.note li, .tip li, .danger li, .notice li, .warning li, .caution li, .attention li { margin-top: .2em; margin-bottom: .2em;}.dataTable th span.filterTh { display: inline; height: 18px; line-height: 18px; padding-left: 10px; background: url('filter-icon.png') no-repeat left -15px; cursor: pointer; float: left; margin-top: .4em;}.dataTable th div.filterThDiv { margin-left: 15px;}.dataTable th span.filterTh:hover { color: #c00;}.dataTable th.top span.filterTh { background-position: left 3px; color: #c00;}.dataTable th.bottom span.filterTh { background-position: left -33px; color: #c00;}.por { position: relative;}.filterBtn { display: inline-block; width: 20px; height: 20px; overflow: hidden; position: absolute; right: 2px; top: 6px; text-indent: -999px; cursor: pointer; background: url(\"filter-icon.png\") no-repeat center -48px; *display: inline; border-radius: 100px;}.filterBtnNone { display: inline-block; position: absolute; *display: inline;}.filterBtn i { display: none;}.filterBtn.disabled { background-position: center -67px; background-color: #fff; box-shadow: 0 0 3px rgba(0, 0, 0, .2);}.filterBtn:hover { background-position: center -67px; background-color: #ddd;}.filterBtn:active { background-color: #ccc;}.filterListBox { background-color: #fff; border: 2px solid #009DD9; width: 300px; position: absolute; right: 10px; top: 30px; box-shadow: 0 3px 10px rgba(0, 0, 0, .2); z-index: 100;}.filterListBox ul { font-size: 12px; font-weight: normal; display: block; width: 100%; list-style: none; margin: 0; height: 200px; overflow: auto;}.filterListBox ul li { display: block; text-align: left; white-space: normal;}.filterListBox ul li label { display: block; cursor: pointer; padding: 0 10px;}.filterListBox ul li label:hover { background-color: #eee;}.filterListBox ul li label:active { background-color: #ddd;}.filterListBox .filterListBoxTitle { padding: 5px 10px; border-top: 1px solid #ddd;}.filterListBox .filterListBoxTitle .filterListBoxTitleSubmit button { height: 24px; min-width: 48px; font-size: 12px; *margin-left: 10px;}.colHidden { width: 50%; min-width: 200px; position: absolute; right: 0; top: 0;}.colHidden .colHiddenTitle { position: absolute; right: 0; top: 0; z-index: 100; height: 20px; line-height: 31px;}.colHidden .colHiddenTitle span { display: inline-block; padding: 0 10px; text-align: center; font-size: 12px; background-color: #009DD9; color: #fff; cursor: pointer; border-radius: 3px; *display: inline;}.colHidden .colHiddenTitle.disabled span { background-color: #009DD9; color: #fff; border-radius: 0 0 0 3px;}.colHidden .colHiddenTitle span:hover { background-color: #21AFE5; color: #fff;}.colHidden .colHiddenTitle span:active { background-color: #0A90C4;}.colHidden .colHiddenWithoutTitle { position: absolute; right: 0; bottom: 13px; z-index: 100; height: 20px; line-height: 31px;}.colHidden .colHiddenWithoutTitle span { display: inline-block; padding: 0 10px; text-align: center; font-size: 12px; background-color: #009DD9; color: #fff; cursor: pointer; border-radius: 3px; *display: inline;}.colHidden .colHiddenWithoutTitle span { background-color: #009DD9; color: #fff; border-radius: 0 0 0 3px;}.colHidden .colHiddenWithoutTitle span:hover { background-color: #21AFE5; color: #fff;}.colHidden .colHiddenWithoutTitle span:active { background-color: #0A90C4;}.colHidden .colHiddenBody { position: absolute; top: 0; right: 0; z-index: 90; background-color: #fff; padding-top: 24px; border: 2px solid #009DD9; box-shadow: 0 3px 10px rgba(0, 0, 0, .2); width: 60%;}.colHidden .colHiddenBody .labList { padding: 10px; max-height: 400px; overflow: auto; font-size: 12px;}.colHidden .colHiddenBody .labList label { float: left; width: 25%; cursor: pointer; display: inline-block; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;}.colHidden .colHiddenBody .labList label:hover { background-color: #eee;}.colHidden .colHiddenBody .labList label:active { background-color: #ddd;}.colHidden .colHiddenBody .submitBar { border-top: 1px solid #ddd; padding: 10px;}.colHidden .colHiddenBody .submitBar button { *margin-left: 10px;}@media only screen and(max-width: 700px) { .colHidden .colHiddenBody .labList label { width: 100%; }}@media only screen and(max-width: 1000px) and(min-width: 700px) { .colHidden .colHiddenBody .labList label { width: 50%; }}@media only screen and(max-width: 1400px) and(min-width: 1000px) { .colHidden .colHiddenBody .labList label { width: 33.3%; }}@media only screen and(min-width: 1400px) { .colHidden .colHiddenBody .labList label { width: 25%; }}.rowFixed { margin-top: 34px; top: 0; position: fixed; width: auto; display: none; border: none;}.colFixed { position: fixed; width: auto; display: none; border: none; z-index: 50;}.colContainer { overflow-x: auto; position: relative;}.zoomPad { position: relative; z-index: 99; cursor: crosshair;}.zoomPreload { -moz-opacity: .8; opacity: .8; filter: alpha(opacity=80); color: #333; font-size: 12px; font-family: FZLTHJW; text-decoration: none; border: 1px solid #CCC; background-color: white; padding: 8px; text-align: center; background-image: url(../images/zoomloader.gif); background-repeat: no-repeat; background-position: 43px 30px; z-index: 110; width: 90px; height: 43px; position: absolute; top: 0; left: 0; *width: 100px; *height: 49px;}.zoomPup { overflow: hidden; background-color: #FFF; -moz-opacity: .6; opacity: .6; filter: alpha(opacity=60); z-index: 120; position: absolute; border: 1px solid #CCC; z-index: 101; cursor: crosshair;}.zoomOverlay { position: absolute; left: 0; top: 0; background: #FFF; z-index: 5000; width: 100%; height: 100%; display: none; z-index: 101;}.zoomWindow { position: absolute; left: 110%; top: 40px; background: #FFF; z-index: 6000; height: auto; z-index: 10000; z-index: 110;}.zoomWrapper { position: relative; border: 1px solid #999; z-index: 110;}.zoomWrapperTitle { display: block; background: #999; color: #FFF; height: 18px; line-height: 18px; width: 100%; overflow: hidden; text-align: center; font-size: 10px; position: absolute; top: 0; left: 0; z-index: 120; -moz-opacity: .6; opacity: .6; filter: alpha(opacity=60);}.zoomWrapperImage { display: block; position: relative; overflow: hidden; z-index: 110;}.zoomWrapperImage img { border: 0; display: block; position: absolute; z-index: 101;}.zoomIframe { z-index: -1; filter: alpha(opacity=0); -moz-opacity: .80; opacity: .80; position: absolute; display: block;}table.rowFixed { width: 90%;}.blueBtn { display: inline-block; height: 24px; min-width: 48px; font-size: 12px; background-color: #009DD9; color: #fff; border: 1px solid #009DD9; cursor: pointer; *display: inline;}.blueBtn:hover { background-color: #21AFE5;}.blueBtn:active { background-color: #0A90C4;}.grayBtn { display: inline-block; height: 24px; min-width: 48px; font-size: 12px; background-color: #fff; color: #444; border: 1px solid #ccc; cursor: pointer; *display: inline;}.grayBtn:hover { background-color: #eee;}.grayBtn:active { background-color: #ddd;}.videoForResponsive { display: none;}</style>\n"
        + " </head>\n" + " <body>\n"
        + "  <a name=\"ZH-CN_TOPIC_0081105543\"></a><a name=\"ZH-CN_TOPIC_0081105543\"></a>\n"
        + "  <div class=\"articleBoxWithoutHead\">\n";

    private static final String htmlStrPart2 = "   <div id=\"body1508919496371\"></div>\n"
        + "   <ul class=\"ullinks\">\n" + "    <dl>\n" + "     <dt><custom_hyperlink href=\"####\">\n"
        + "       OMP服务介绍\n" + "      </custom_hyperlink>\n" + "     </dt>\n" + "     <dd>\n" + "     </dd>\n"
        + "    </dl>\n" + "   </ul>\n" + "  </div>\n" + " </body>\n" + "</html>";

    private static String hValue = "   <h1 class=\"topicTitle-h1\">软件描述</h1>\n";

    static {
        try (InputStream inputStream = new ByteArrayInputStream(LICENSE_CONTENT.getBytes(StandardCharsets.UTF_8))) {
            new License().setLicense(inputStream);
        } catch (Exception e) {
            // Aspose license.setLicense方法抛出Exception异常
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        // 这是保存文件名
        String saveDocName = "测试1020.docx";
        Document document = new Document();
        DocumentBuilder documentBuilder = new DocumentBuilder(document);
        // 光标指向文档开头
        documentBuilder.moveToDocumentStart();

        // 添加目录,居中对齐,字体为1级标题格式
        documentBuilder.getCurrentParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
        documentBuilder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);
        documentBuilder.writeln("目录");
        documentBuilder.getParagraphFormat().clearFormatting();

        // 插入目录表,层级最高9级
        documentBuilder.insertTableOfContents("\\o\"1-9\"\\h\\z\\u");
        StringBuilder stringBuilder = new StringBuilder("1");
        for (int i = 1; i <= 11; i++) {
            // 获取当前要插入的html的层级,逐级递增,最高9级
            int currentHeadingLevel = Math.min(i, 9);

            // 设置要插入的标题的层级
            documentBuilder.getParagraphFormat().setStyleIdentifier(i);

            // 插入标题
            documentBuilder.writeln(stringBuilder.toString());

            // 插入html内容
            documentBuilder.insertHtml(getHtmlStr(currentHeadingLevel));
            stringBuilder.append(".1");
        }
        // 使用updateFields,自动生成目录表
        document.updateFields();

        // 更新页面布局,更新目录节点对应的页码
        document.updatePageLayout();
        document.save(saveDocName);
    }

    private static String getHtmlStr(int currentLevel) {
        // 将当前h1标签级别设置为当前html层级+1,最高9级
        int hLevel = currentLevel + 1;
        if (hLevel > StyleIdentifier.HEADING_9) {
            return htmlStrPart1 + hValue.replaceAll("h1", "h9") + htmlStrPart2;
        }
        return htmlStrPart1 + hValue.replaceAll("h1", "h" + hLevel) + htmlStrPart2;
    }
}

然后这是我生成word的效果:很明显,h标签从第6级以后就不再生成导航了
image.png (15.1 KB)

@yyl5201314, 从htmlStrPart1变量中删除最后一行后,您的程序将生成在 Word 中显示的文档,将为从 1 到 9 的所有标题添加标签。

您好,我修改后解决了我的问题,非常感谢!然后还想请问一下,我通过StyleIdentifier这个类发现创建目录和导航的最高级别为9,请问这个有办法调大吗?

@yyl5201314, 不,9 是最大值。

有一个可以获得更多编号级别的解决方法的描述:https://groups.google.com/g/microsoft.public.word.numbering/c/EM-57kyS3Ww。 请看一下是否可以帮助您。

您好,我想知道htmlStrPart1变量中删除最后一行后就能为从 1 到 9 的所有标题添加标签,这是什么原因?我尝试修改了class为其他,同样不可添加标签,这是什么原因呢?

@yyl5201314 MS Word 文档和 HTML 文档模型有很大不同。 在您的 HTML 代码段中存在嵌套的 DIV 元素,而 MS Word 文档中没有类似的内容。 因此,最好减少 HTML 中的嵌套,以在 MS Word 中获得类似的结果。
另外,在 HTML 中,H 标签仅适用于 1-6 级别:
https://www.w3schools.com/tags/tag_hn.asp

我觉得这是不是有些不合理?因为几乎所有HTML都会有DIV嵌套的情况。或者请问你有没有其他方式来解决我的问题?我的需求是想为html中的H标签生成导航,但是级别不是H标签原来的级别,而是从当前HTML所在的级别开始往后推,比如,当前HTML的标题为1.1.1,导航级别为3级,我期望为其中的H2标签创建导航,级别为3+2=5。

@yyl5201314 首先,请注意,Aspose.Words 设计用于处理 MS Word 文档。 HTML 文档和 MS Word 文档对象模型有很大不同,在将一种格式转换为另一种格式后,并不总是能够提供 100% 的保真度并保留所有功能。
Denis 建议的解决方法,即减少 HTML 中的嵌套似乎可以正常工作。

请问你们有其他解决方法吗?我的需求是想为html中的H标签生成导航,但是级别不是H标签原来的级别,而是从当前HTML所在的级别开始往后推,比如,当前HTML的标题为1.1.1,导航级别为3级,我期望为其中的H2标签创建导航,级别为3+2=5。

@yyl5201314 我建议您避免使用 HTML 并使用 Aspose.Words 和内置样式在文档中构建标题。 这将使过程更加透明,更不容易出错。