Type TTableContent = class
	//目录
	Function Create(docx);overload;
	Begin
		Create(docx, nil);
	End;
	
	Function Create(docx, node);overload;
	Begin
		node_ := node;
		docx_ := docx;
		impl_ := TOfficeObj('TTableContentImpl');
		//_CheckNodes('endnotes.xml');
		//_CheckNodes('footnotes.xml');
		//contentType := docx.Zip().Get('[Content_Types].xml');
		//class(TSXml).AddOverrideContentType(contentType, 'wmf', 'image/x-wmf');
	End;
	
	Function SetDefaultFormat();
	Begin
		defultFont := impl_.sdtPr.rPr;
		defultFont.rFont.cstheme := 'minorBidi';
		defultFont.rFont.XMLeastAsia := '宋体';
		defultFont.rFont.XMLhAnsi := '宋体';
		defultFont.rFont.XMLascii := '宋体';
		//defultFont.kern := 2;
		defultFont.Size := 21/2;
		defultFont.SzCs := 24;
		defultFont.Lang := 'en-US';
		defultFont.bidi := 'ar-SA';
		defultFont.eastAsia := 'zh-CN';
		defultFont.Color := 'DBDBDB';
		defultFormat := impl_.sdtPr;
		defultFormat.ID := integer(time()*24*3600);
		//defultFormat.Color := 'DBDBDB';
		defultFormat.docPartObj.docPartGallery := 'Table of Contents';
		defultFormat.docPartObj.docPartUnique := 1;
		//impl_.sdtEndPr.rPr.Size := 21;
		//impl_.sdtEndPr.rPr.SzCs := 22;
		impl_.sdtEndPr.rPr.Bold := true;
	End;
	
	///应用目录样式
	Function Apply(); override;
	Begin
		// sdtContent 不应该updateNode
		tmp_impl := TOfficeObj('TTableContentImpl');
		tmp_impl.sdtPr := impl_.sdtPr;
		tmp_impl.sdtEndPr := impl_.sdtEndPr;
		arr := tmp_impl.Marshal();
		class(TSXml).UpdateNode(node_, arr['attributes'], arr['children']);
	End;
	
	///添加目录条目
	///UpperHeadingLevel：标题最高级别
	///LowerHeadingLevel：标题最低级别
	///  使用 UpperHeadingLevel 属性可设置起始标题级别（最高）。例如，若要设置 TOC 域语法 {TOC \o "1-3"}，可将 LowerHeadingLevel 属性设为 3，并将 UpperHeadingLevel 属性设为 1。
	Function Add(posOpt, UpperHeadingLevel, LowerHeadingLevel);
    Begin
		///目录
		mParagraph := TOfficeObj('TParagraph');
		mParagraph.pPr.LineSpacingRule := 'auto';
		mParagraph.pPr.LineSpacing := 240;
		mParagraph.pPr.AfterLines := 0;
		mParagraph.pPr.SpaceAfter := 0;
		mParagraph.pPr.BeforeLines := 0;
		mParagraph.pPr.SpaceBefore := 0;
		mParagraph.pPr.Alignment := 'center';
		mParagraph.pPr.FirstLineChars := 0;
		mParagraph.pPr.FirstLineIndent := 0;
		mParagraph.pPr.RightChars := 0;
		mParagraph.pPr.RightIndent := 0;
		mParagraph.pPr.LeftChars := 0;
		mParagraph.pPr.LeftIndent := 0;
		mParagraph.pPr.StyleID := _GetStyle('TOC');
		mParagraph.Run.T := '目录';
		mParagraph.Run.rPr.Lang := 'zh-CN';
		//mParagraph.Run.rPr.SetName('宋体', true);
		//mParagraph.Run.rPr.Size := 21/2;
		//mParagraph.Run.rPr.Bold := true;
		_AddStdContent(mParagraph);
		
		goback := TOfficeObj('TParagraph');
		id := docx_.Document().GetBookMarkID();
		goback.MarkStart.Name := '_GoBack';
		goback.MarkStart.ID := id;
		goback.MarkEnd.ID := id;
		//_AddStdContent(goback);
		
		//缺省目录，需要word或wps打开后，更新目录
		//_AddDefaultTableContent(UpperHeadingLevel, LowerHeadingLevel);
		
		//自定义页码计算与word、wps有差异，不推荐使用
		_AddTableContent(posOpt, UpperHeadingLevel, LowerHeadingLevel);
	End;
	
	Function _AddDefaultTableContent(UpperHeadingLevel, LowerHeadingLevel);
	Begin
		p := _AddItem(UpperHeadingLevel, LowerHeadingLevel, UpperHeadingLevel - 1, true);
		_AddStdContent(p);
		
		p2 := TOfficeObj('TParagraph');
		//p2.Format.SpaceAfter := 0;
		run := p2.AddRun();
		run.rPr.Bold := true;
		run.rPr.noProof := true;
		run.fldCharType := 'end';
		_AddStdContent(p2);
	End;
	
	Function _AddItem(UpperHeadingLevel, LowerHeadingLevel, level, first);
	Begin
		p := TOfficeObj('TParagraph');
		//标题段落属性
		p.Format.StyleId := _GetStyle(level);
		//p.Format.SpaceAfter := 0;
		p.Format.rPr.noProof := true;
		tab := TOfficeObj('TTabStop');
		tab.Val := 'right';
		tab.leader := 'dot';
		tab.Position := 8640;
		p.Format.Tabs.Add(0, tab);
		if first then Begin
			//fldCharType
			r1 := p.AddRun();
			r1.fldCharType := 'begin';
			r1.Dirty := 'true';

			//instrText
			r2 := p.AddRun();
			r2.instrTextSpace := 'preserve';
			r2.instrText := 'TOC \\o \"' $ UpperHeadingLevel $ '-' $ LowerHeadingLevel $ '\" \\h \\z \\u ';

			//fldCharType
			r3 := p.AddRun();
			r3.fldCharType := 'separate';
		End;
		return p;
	End;

	Function _AddTableContent(posOpt, UpperHeadingLevel, LowerHeadingLevel);
	Begin
		///获取标题列表 array((("Level":level,"Paragraph":"object","Text":title,"numId":,"ilvl":,"numArr":));
		numMap := array();
		r := docx_.Document().Body().GetHeadingListImpl(docx_, posOpt, UpperHeadingLevel, LowerHeadingLevel, numMap, true);
		for i:=0 to length(r)-1 do Begin
			p := _AddItem(UpperHeadingLevel, LowerHeadingLevel, r[i]['Level'], i = 0 ? true: false);
			//fldCharType
			h := p.AddHyperLink();
			bookmarke := _GetBookMarkId(r[i]['Paragraph']);
			h.Anchor := bookmarke;
			h.history := 1;

			r1 := h.AddRun();
			r1.rPr.StyleId := _GetStyle(level);
			r1.rPr.noProof := true;
			numStr := '';	//数字项目编号
			if r[i]['numId'] then Begin
				style := docx_.NumberingObject().NumberStyle(r[i]['numId']);//支持数字、字符串StyleId
				if ifObj(style) then
					numStr := style.GetText(r[i]['ilvl'], r[i]['numArr']);
			End
			if numStr <> '' then r1.T := numStr + ' ' + r[i]['Text'];
			else r1.T := r[i]['Text'];

			r2 := h.AddRun();
			r2.rPr.noProof := true;
			r2.rPr.WebHidden := true;
			r2.Tab := true;

			r3 := h.AddRun();
			r3.rPr.noProof := true;
			r3.rPr.WebHidden := true;
			r3.fldCharType := 'begin';

			//instrText
			r4 := h.AddRun();
			r4.rPr.noProof := true;
			r4.rPr.WebHidden := true;
			r4.instrTextSpace := 'preserve';
			r4.instrText := ' PAGEREF ' $ bookmarke $ ' \\h ';

			r5 := h.AddRun();
			r5.rPr.noProof := true;
			r5.rPr.WebHidden := true;
			r5.Tab := true;

			//fldCharType
			r6 := h.AddRun();
			r6.rPr.noProof := true;
			r6.rPr.WebHidden := true;
			r6.fldCharType := 'separate';

			//页码
			r7 := h.AddRun();
			r7.T := 0;//'' $ r[i]['pageNo'];

			//fldCharType
			r8 := h.AddRun();
			r8.rPr.noProof := true;
			r8.rPr.WebHidden := true;
			r8.fldCharType := 'end';

			_AddStdContent(p);
			
		End;

		p2 := TOfficeObj('TParagraph');
		//p2.Format.SpaceAfter := 0;
		run := p2.AddRun();
		run.rPr.Bold := true;
		run.rPr.noProof := true;
		run.fldCharType := 'end';
		_AddStdContent(p2);

	End;
	
	///更新目录
	Function UpdatePageNumbers();
	Begin
	End;
	
	Function Marshal();
	Begin
		return impl_.Marshal();
	End;
	
	Property Format read readFormat;
	Function readFormat();
	Begin
		return impl_.sdtPr;
	End;
	
	Property EndFormat read readEndFormat;
	Function readEndFormat();
	Begin
		return impl_.sdtEndPr;
	End;
	
	Property Font read readFont;
	Function readFont();
	Begin
		return impl_.sdtPr.rPr;
	End;

	Function _AddStdContent(o);
	Begin
		impl_.SdtContent.NewChildNode( array("field":"", "name":"w:p", "obj":o, "attrEx":"", "nodeType":"") );
	End;
	
	Function _GetStyle(level);
	Begin
		styleName := ifstring(level) ? level : 'TOC' $ (level + 1);
		style := docx_.StyleObject().GetStyleById(styleName);
		if not ifObj(style) then Begin
			style := docx_.StyleObject().AddDefaultStyle(styleName);
		End;
		return style.StyleId;
	End;
	
	Function _GetBookMarkId(p);
	Begin
		node := p.node_.FirstChildElement('w:bookmarkStart');
		if ifObj(node) then 
			return node.GetAttribute('w:name');
		bookMarkID := docx_.Document().GetBookMarkID();
		return p.AddBookMark();
	End;
	
	Function _CheckNodes(name);
	Begin
		z := docx_.Zip();
		xml := z.Get('word/' + name);
		if not ifObj(xml) then Begin
			xmlData := TOfficeTemplate(name);
			if not ifString(xmlData) then 
				return ;
			z.Add('word/' + name, xmlData);
			rels := 'word/_rels/document.xml.rels';
			relsObj := z.Get(rels);
			[rId, target] := class(TSXml).FindRelationshipRid(relsObj, '');
			rId ++;
			
			if name = 'endnotes.xml' then Begin
				class(TSXml).AddRelationshipRid(relsObj, name, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes', 'rId' $ rId);
				contentType := z.Get('[Content_Types].xml');
				class(TSXml).AddOverrideContentType(contentType, '/word/' + name, 'application/vnd.openxmlformats-officedocument.wordprocessingml.endnotes+xml');
			End
			else if name = 'footnotes.xml' then Begin
				class(TSXml).AddRelationshipRid(relsObj, name, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes', 'rId' $ rId);
				contentType := z.Get('[Content_Types].xml');
				class(TSXml).AddOverrideContentType(contentType, '/word/' + name, 'application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml');
			End;
		End;
	End;
	
	docx_;
	node_;
	impl_;
End;
