C#/VB.NET怎么实现在Word中插入或删除脚注(vb.net,Word,开发技术)

时间:2024-05-06 05:08:17 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

程序环境

本次测试时,在程序中引入Free Spire.Doc for .NET。可通过以下方法引用 Free Spire.Doc.dll文件:

方法1:将Free Spire.Doc for .NET下载到本地,解压,安装。安装完成后,找到安装路径下BIN文件夹中的 Spire.Doc.dll。然后在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“添加引用”,将本地路径BIN文件夹下的dll文件添加引用至程序。

方法2:通过NuGet安装。可通过以下2种方法安装:

(1)可以在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“管理NuGet包”,然后搜索“Free Spire.Doc”,点击“安装”。等待程序安装完成。

(2)将以下内容复制到PM控制台安装。

Install-Package FreeSpire.Doc -Version 10.8.0

在Word中的特定段落后插入脚注

以下是在指定段落后插入脚注的详细步骤。

  • 创建Document实例

  • 使用Document.LoadFromFile()方法加载示例Word文档。

  • 获取第一节,然后获取该节中的指定段落。

  • 使用Paragraph.AppendFootnote(FootnoteType.Footnote)方法在段落末尾添加脚注。

  • 设置脚注的文本内容、字体和颜色,然后设置脚注上标数字的格式。

  • 使用Document.SaveToFile() 方法保存结果文档。

完整代码

C#

usingSpire.Doc;usingSpire.Doc.Documents;usingSpire.Doc.Fields;usingSystem.Drawing;namespaceAddFootnote{classProgram{staticvoidMain(string[]args){//创建Document实例Documentdocument=newDocument();//加载Word文档示例document.LoadFromFile("我与地坛.docx");//获取第一节Sectionsection=document.Sections[0];//获取节中的指定段落Paragraphparagraph=section.Paragraphs[1];//在段落末尾添加脚注Footnotefootnote=paragraph.AppendFootnote(FootnoteType.Footnote);//设置脚注的文本内容TextRangetext=footnote.TextBody.AddParagraph().AppendText("即使世界毁灭,那又怎样,天地崩塌,于我何干,我在乎的只有他。");//设置文本字体和颜色text.CharacterFormat.FontName="宋体";text.CharacterFormat.FontSize=12;text.CharacterFormat.TextColor=Color.DarkBlue;//设置脚注上标数字的格式footnote.MarkerCharacterFormat.FontName="Calibri";footnote.MarkerCharacterFormat.FontSize=15;footnote.MarkerCharacterFormat.Bold=true;footnote.MarkerCharacterFormat.TextColor=Color.DarkCyan;//保存结果文档document.SaveToFile("添加脚注.docx",FileFormat.Docx);}}}

VB.NET

ImportsSpire.DocImportsSpire.Doc.DocumentsImportsSpire.Doc.FieldsImportsSystem.DrawingNamespaceAddFootnoteFriendClassProgramPrivateSharedSubMain(ByValargsAsString())'创建Document实例DimdocumentAsDocument=NewDocument()'加载Word文档示例document.LoadFromFile("我与地坛.docx")'获取第一节DimsectionAsSection=document.Sections(0)'获取节中的指定段落DimparagraphAsParagraph=section.Paragraphs(1)'在段落末尾添加脚注DimfootnoteAsFootnote=paragraph.AppendFootnote(FootnoteType.Footnote)'设置脚注的文本内容DimtextAsTextRange=footnote.TextBody.AddParagraph().AppendText("即使世界毁灭,那又怎样,天地崩塌,于我何干,我在乎的只有他。")'设置文本字体和颜色text.CharacterFormat.FontName="宋体"text.CharacterFormat.FontSize=12text.CharacterFormat.TextColor=Color.DarkBlue'设置脚注上标数字的格式footnote.MarkerCharacterFormat.FontName="Calibri"footnote.MarkerCharacterFormat.FontSize=15footnote.MarkerCharacterFormat.Bold=Truefootnote.MarkerCharacterFormat.TextColor=Color.DarkCyan'保存结果文档document.SaveToFile("添加脚注.docx",FileFormat.Docx)EndSubEndClassEndNamespace

效果图

C#/VB.NET怎么实现在Word中插入或删除脚注

在Word中的特定文本后插入脚注

我们还可以在文档中任何位置的指定文本后插入脚注。以下是详细步骤。

  • 创建Document实例。

  • 使用Document.LoadFromFile() 方法加载Word文档。

  • 使用Document.FindString() 方法查找指定的文本。

  • 使用TextSelection.GetAsOneRange() 方法获取指定文本的文本范围。

  • 使用TextRange.OwnerParagraph 属性获取文本范围所在的段落。

  • 使用Paragraph.ChildObjects.IndexOf() 方法获取段落中文本范围的位置索引。

  • 使用Paragraph.AppendFootnote(FootnoteType.Footnote)方法添加脚注,然后使用Paragraph.ChildObjects.Insert() 方法在指定文本后插入脚注

  • 设置脚注的文本内容、字体和颜色,然后设置脚注上标数字的格式。

  • 使用Document.SaveToFile() 方法保存结果文档。

完整代码

C#

usingSpire.Doc;usingSpire.Doc.Documents;usingSpire.Doc.Fields;usingSystem.Drawing;namespaceInsertFootnote{classProgram{staticvoidMain(string[]args){//创建Document实例Documentdocument=newDocument();//加载Word文档示例document.LoadFromFile("我与地坛.docx");//查找指定的文本字符串TextSelectionselection=document.FindString("最苦的母亲",false,true);//获取指定文本的文本范围TextRangetextRange=selection.GetAsOneRange();//获取文本范围所在的段落Paragraphparagraph=textRange.OwnerParagraph;//获取段落中文本范围的位置索引intindex=paragraph.ChildObjects.IndexOf(textRange);//添加脚注Footnotefootnote=paragraph.AppendFootnote(FootnoteType.Footnote);//在指定段落后插入脚注paragraph.ChildObjects.Insert(index+1,footnote);//设置脚注的文本内容TextRangetext=footnote.TextBody.AddParagraph().AppendText("不知道儿子的不幸在母亲那儿总是要加倍的。");//设置文本字体和颜色text.CharacterFormat.FontName="宋体";text.CharacterFormat.FontSize=12;text.CharacterFormat.TextColor=Color.DarkBlue;//设置脚注上标数字的格式footnote.MarkerCharacterFormat.FontName="Calibri";footnote.MarkerCharacterFormat.FontSize=15;footnote.MarkerCharacterFormat.Bold=true;footnote.MarkerCharacterFormat.TextColor=Color.DarkGreen;//保存结果文档document.SaveToFile("插入脚注.docx",FileFormat.Docx);}}}

VB.NET

ImportsSpire.DocImportsSpire.Doc.DocumentsImportsSpire.Doc.FieldsImportsSystem.DrawingNamespaceInsertFootnoteFriendClassProgramPrivateSharedSubMain(ByValargsAsString())'创建Document实例DimdocumentAsDocument=NewDocument()'加载Word文档示例document.LoadFromFile("我与地坛.docx")'查找指定的文本字符串DimselectionAsTextSelection=document.FindString("最苦的母亲",False,True)'获取指定文本的文本范围DimtextRangeAsTextRange=selection.GetAsOneRange()'获取文本范围所在的段落DimparagraphAsParagraph=textRange.OwnerParagraph'获取段落中文本范围的位置索引DimindexAsInteger=paragraph.ChildObjects.IndexOf(textRange)'添加脚注DimfootnoteAsFootnote=paragraph.AppendFootnote(FootnoteType.Footnote)'在指定段落后插入脚注paragraph.ChildObjects.Insert(index+1,footnote)'设置脚注的文本内容DimtextAsTextRange=footnote.TextBody.AddParagraph().AppendText("不知道儿子的不幸在母亲那儿总是要加倍的。")'设置文本字体和颜色text.CharacterFormat.FontName="宋体"text.CharacterFormat.FontSize=12text.CharacterFormat.TextColor=Color.DarkBlue'设置脚注上标数字的格式footnote.MarkerCharacterFormat.FontName="Calibri"footnote.MarkerCharacterFormat.FontSize=15footnote.MarkerCharacterFormat.Bold=Truefootnote.MarkerCharacterFormat.TextColor=Color.DarkGreen'保存结果文档document.SaveToFile("插入脚注.docx",FileFormat.Docx)EndSubEndClassEndNamespace

效果图

C#/VB.NET怎么实现在Word中插入或删除脚注

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:C#/VB.NET怎么实现在Word中插入或删除脚注的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Js解构赋值的常见场景有哪些下一篇:

5 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18