翻譯|使用教程|編輯:李顯亮|2019-10-25 10:44:18.197|閱讀 530 次
概述:本系列教程將為大家?guī)鞸pire.Doc for .NET在使用過程中的各類實(shí)際操作,word文檔中經(jīng)常會使用腳注和尾注來為文檔添加說明。本文主要描述如何使用C# 為Word文檔添加和刪除腳注尾注。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Spire.Doc for .NET是一個(gè)專業(yè)的Word .NET庫,設(shè)計(jì)用于幫助開發(fā)人員高效地開發(fā)創(chuàng)建、閱讀、編寫、轉(zhuǎn)換和打印任何來自.NET( C#, VB.NET, ASP.NET)平臺的Word文檔文件的功能。
本系列教程將為大家?guī)?strong>Spire.Doc for .NET在使用過程中的各類實(shí)際操作,word文檔中經(jīng)常會使用腳注和尾注來為文檔添加說明。本文主要描述如何使用C# 為Word文檔添加和刪除腳注尾注。
點(diǎn)擊下載最新版Spire.Doc for .NET
word文檔中經(jīng)常會使用腳注和尾注來為文檔添加說明。腳注一般位于當(dāng)前頁面的底部或文字下方,用于作為文檔某處內(nèi)容的注釋。而尾注一般位于文檔的末尾,列出引文的出處等。接下來主要描述如何使用C# 為Word文檔添加腳注尾注。
//新建一個(gè)word文檔對象并加載需要添加腳注尾注的word文檔
Document document = new Document();
document.LoadFromFile("Test.docx", FileFormat.Docx2010);
//獲取第一個(gè)段落
Paragraph paragraph = document.Sections[0].Paragraphs[0];
//添加腳注
Footnote footnote = paragraph.AppendFootnote(FootnoteType.Footnote);
//在第一段里查找字符串“Spire.Doc for .NET” 并用來添加腳注
DocumentObject obj = null;
for (int i = 0; i < paragraph.ChildObjects.Count; i++)
{
    obj = paragraph.ChildObjects[i];
    if (obj.DocumentObjectType == DocumentObjectType.TextRange)
    {
        TextRange textRange = obj as TextRange;
      
        if (textRange.Text == "Spire.Doc for .NET")
        {
            //為添加腳注的字符串設(shè)置加粗格式
            textRange.CharacterFormat.Bold = true;
            //插入腳注
            paragraph.ChildObjects.Insert(i + 1, footnote);
            break;
        }
    }
}
//添加腳注內(nèi)容并設(shè)置字體格式
TextRange text = footnote.TextBody.AddParagraph().AppendText("Spire.Doc腳注");
text.CharacterFormat.FontName = "Arial Black";
text.CharacterFormat.FontSize = 10;
text.CharacterFormat.TextColor = Color.DarkGray;
footnote.MarkerCharacterFormat.FontName = "Calibri";
footnote.MarkerCharacterFormat.FontSize = 12;
footnote.MarkerCharacterFormat.Bold = true;
footnote.MarkerCharacterFormat.TextColor = Color.DarkGreen;
//獲取第三段落
Paragraph paragraph2 = document.Sections[0].Paragraphs[2];
//添加尾注并設(shè)置尾注和格式
Footnote endnote = paragraph2.AppendFootnote(FootnoteType.Endnote);
TextRange text2 = endnote.TextBody.AddParagraph().AppendText("Spire.Doc尾注");
text2.CharacterFormat.FontName = "Arial Black";
text2.CharacterFormat.FontSize = 10;
text2.CharacterFormat.TextColor = Color.DarkGray;
endnote.MarkerCharacterFormat.FontName = "Calibri";
endnote.MarkerCharacterFormat.FontSize = 12;
endnote.MarkerCharacterFormat.Bold = true;
endnote.MarkerCharacterFormat.TextColor = Color.DarkGreen;
//保存文檔
document.SaveToFile("添加腳注尾注.docx", FileFormat.Docx2010);
下面將介紹通過使用Spire.Doc for .NET來刪除Word中的腳注、尾注。
//實(shí)例化Document類的對象,并加載測試文檔
 Document document = new Document();
 document.LoadFromFile("test.docx");
 //獲取第一節(jié)
 Section section = document.Sections[0];
 //遍歷所有段落
 foreach (Paragraph para in section.Paragraphs)
 {
     int index = -1;
     //遍歷段落中的子對象,并刪除腳注、尾注
       for (int i = 0, cnt = para.ChildObjects.Count; i < cnt; i++)
     {
         ParagraphBase pBase = para.ChildObjects[i] as ParagraphBase;
         
         if (pBase is Footnote)
         {
             index = i;
             break;
         }
     }
     if (index > -1)
     para.ChildObjects.RemoveAt(index);
 }            
 //保存文檔
 document.SaveToFile("result.docx", FileFormat.Docx);
推薦閱讀:【想要快速完成文檔格式轉(zhuǎn)換嗎?Spire系列組件格式轉(zhuǎn)換完整攻略來啦!】
*購買Spire.Doc正版授權(quán)的朋友可以點(diǎn)擊哦~~
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn