toc如何判断 word_c#使用DocX给word添加目录TOC
剛要做目錄的時候,我就想DocX應該提供了這個方面的函數。于是我就在討論區搜索,看看別人是怎么用的。
我搜索了catalog; catalogue; list; contents;,但是都沒有和目錄有關的東西,搞得我以為沒提供,于是就又各種搜索,什么list之類的,搜著搜著就找到了一篇。
說是需要下載一個patch,但是不造在哪。還有的提供了利用其他的xml布局來幫助生成目錄的。這些都不用管了,我用的是在DocX.cs里的源碼里直接修改下的好方法。
抄錄如下:
Hello together,
i'm sorry, i didn't like your solution proposals as I use docx to not have a need to include the open sdk. (But it took me to this solution 95% of the way, so thanks!)
I found out DocX can actually do that easily with just a few minor changes to the code.
To accomplish that goal add the following lines to DocX.cs into the class:
///
/// Activate Updating Fields on
Open
///
public void
UpdateFieldsOnOpen()
{
XElement element = new
XElement(XName.Get("updateFields", DocX.w.NamespaceName));
element.Add(new
XAttribute(XName.Get("val", DocX.w.NamespaceName),
"true"));
settings.Root.AddFirst(element);
}
Finally you need to
modify the save process slightly because docx reloads the settings part
directly before saving the document.
// Comment out or
remove those lines:
-??????????? using (TextReader tr = new
StreamReader(settingsPart.GetStream()))
-??????????????? settings =
XDocument.Load(tr);
// Insert this instead:
+??????????? if (settings == null
)
+??????????? {
+??????????????? using (TextReader tr =
new StreamReader(settingsPart.GetStream()))
+??????????????????? settings =
XDocument.Load(tr);
+??????????? }
Use these lines to
use the new option:
docx.UpdateFieldsOnOpen();
docx.Save();
As a
result, word will tell you that some fields need updating when you open the
document.
Well
after you bypassed that new preview mode that they put into word to complicate
usage. Which i know everybody at my workplace loves very much.
It
works on my computer :).
I
hope it will help YOU ALL TOO since you are great people that deserve TOCs.
Best regards,
shoff
如上所述,就是在DocX.cs的源代碼里改一下,在代碼末尾添加
1 ///
2 /// Activate Updating Fields on Open
3 ///
4 publicvoidUpdateFieldsOnOpen()
5 {
6 XElement
element = newXElement(XName.Get("updateFields", DocX.w.NamespaceName));
7 element.Add(new
XAttribute(XName.Get("val", DocX.w.NamespaceName), "true"));
8
9 settings.Root.AddFirst(element);
10 }
然后再修改下保存的函數save(),
1
2 //using (TextReader tr = new
StreamReader(settingsPart.GetStream()))
3 // settings = XDocument.Load(tr);
4 if(settings == null)
5 {
6 using(TextReader tr = newStreamReader(settingsPart.GetStream()))
7 settings
= XDocument.Load(tr);
8 }
把原本是注釋里的代碼改成現在的樣子。
然后重新生成下DocX.dll。
在使用的時候自己添加個函數
1 public staticvoidupdate(string docx)
2 {
3 using(DocX document =
DocX.Load(docx))
4 {
5 document.UpdateFieldsOnOpen();
6 document.Save();
7 }
8
9 }
讓文檔在打開的時候自動更新目錄域。
不過這樣的話你得首先有目錄,我用的辦法是新建一個word,給它加上目錄,然后創建新word的時候把這個的目錄頁給插入到新文檔里。
1 public staticvoidloadTOC(string s, stringpath, stringname)
2 {
3 if(!Directory.Exists(path))
4 {
5 Directory.CreateDirectory(path);
6 }
7 stringdocx = @path + "\\"+ name;
8 DocX
_template = DocX.Load(s);
9 DocX
_document = DocX.Create(docx);
10 _document.InsertDocument(_template);
11 _document.SaveAs(docx);
12 addNewpage(docx);
13
14 }
至此,目錄就可以正常使用了。每次打開就會提示更新域。
如上,結果今天一大早去翻看Example的時候,發現原來作者已經更新了,并且添加了操作目錄的代碼。使用起來也很簡單。
1 public staticvoidAddToc(string docx)
2 {
3
4 using(var document =
DocX.Load(docx))
5 {
6 document.InsertTableOfContents("目錄", TableOfContentsSwitches.O | TableOfContentsSwitches.U |
TableOfContentsSwitches.Z | TableOfContentsSwitches.H, "Heading2");
7 document.Save();
9 }
10 }
這樣就已經ok了,也不用修改什么代碼了,作用和之前的那個一樣。頓時心里一陣草泥馬奔騰而過。
提醒:多看example里的項目,有需求先在里面找。
總結
以上是生活随笔為你收集整理的toc如何判断 word_c#使用DocX给word添加目录TOC的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: QPF BGA封装
- 下一篇: 计算机教师继续教育心得,教师继续教育心得