700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Word控件Spire.Doc 【脚注】教程(1) 使用C#或VB.NET在 Word 文档中插入脚注和尾注

Word控件Spire.Doc 【脚注】教程(1) 使用C#或VB.NET在 Word 文档中插入脚注和尾注

时间:2021-01-26 18:40:27

相关推荐

Word控件Spire.Doc 【脚注】教程(1) 使用C#或VB.NET在 Word 文档中插入脚注和尾注

Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。

E-iceblue功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式

Spire.Doc 最新下载(qun:767755948)/product/3368/download

脚注和尾注是简短的注释,可用于对文档中的某些单词或句子提供解释、注释或引用。脚注通常出现在包含其参考编号的页面底部,而尾注出现在文档或章节的末尾。如果您正在用 Word 撰写学术论文,则插入脚注或尾注可能是必不可少的。本文将演示如何用Spire.Doc for .NET.在 C# 和 的 Word 文档中插入脚注和尾注。

安装 Spire.Doc for .NET

首先,您需要将 Spire.Doc for .NET 包中包含的 DLL 文件添加为 .NET 项目中的引用。DLL 文件可以从过以下方式安装 NuGet.

PM> Install-Package Spire.Doc

在 C# 和 Word 中插入脚注

脚注由两部分组成 :脚注参考标记和相应的脚注文本。要插入特定文本的脚注,您需要搜索文本并获取文本所在的段落,然后在段落中添加脚注,然后在找到的文本后插入脚注引用标记并设置脚注文本。具体步骤如下:

初始化文档使用加载 Word 文档 Document.LoadFromFile() 方法。使用 Document.FindString() 方法,并使用以下命令将找到的文本作为单个文本范围获取 TextSelection.GetAsOneRange() 方法。通过以下方式访问文本范围的所有者段落 TextRange.OwnerParagraph 属性,并使用 Paragraph.ChildObjects.IndexOf() 方法。使用在段落中添加脚注 Paragraph.AppendFootnote(FootnoteType.Footnote) 方法。在文本范围后插入脚注引用标记,使用 Paragraph.ChildObjects.Insert() 方法。使用设置脚注文本 Footnote.TextBody.AddParagraph().追加文本() 方法。设置脚注文本和参考标记的格式,例如字体名称、字体大小和文本颜色。保存结果文档使用 Document.SaveToFile() 方法。

C#

using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;using System.Drawing;namespace InsertFootnote{internal class Program{static void Main(string[] args){//Initialize an instance of the Document classDocument document = new Document();//Load a Word documentdocument.LoadFromFile(@"Sample.docx");//Find a specific text in the documentTextSelection selection = document.FindString("Spire.Doc for .NET", false, true);//Get the found text as a single text rangeTextRange textRange = selection.GetAsOneRange();//Get the owner paragraph of the text rangeParagraph paragraph = textRange.OwnerParagraph;//Get the index of the text range in the paragraphint index = paragraph.ChildObjects.IndexOf(textRange);//Add a footnote to the paragraphFootnote footnote = paragraph.AppendFootnote(FootnoteType.Footnote);//Insert the footnote reference mark after the text rangeparagraph.ChildObjects.Insert(index + 1, footnote);//Set the footnote texttextRange = footnote.TextBody.AddParagraph().AppendText("Developed by E-iceblue Co., LTD.");//Set format for the footnote texttextRange.CharacterFormat.FontName = "Arial Black";textRange.CharacterFormat.FontSize = 12;textRange.CharacterFormat.TextColor = Color.DarkGray;//Set format for the footnote reference markfootnote.MarkerCharacterFormat.FontName = "Calibri";footnote.MarkerCharacterFormat.FontSize = 12;footnote.MarkerCharacterFormat.Bold = true;footnote.MarkerCharacterFormat.TextColor = Color.DarkGreen;//Save the result documentdocument.SaveToFile("InsertFootnote.docx", FileFormat.Docx);document.Close();}}}

Imports Spire.DocImports Spire.Doc.DocumentsImports Spire.Doc.FieldsImports System.DrawingNamespace InsertFootnoteFriend Class ProgramPrivate Shared Sub Main(ByVal args As String())'Initialize an instance of the Document classDim document As Document = New Document()'Load a Word documentdocument.LoadFromFile("Sample.docx")'Find a specific text in the documentDim selection As TextSelection = document.FindString("Spire.Doc for .NET", False, True)'Get the found text as a single text rangeDim textRange As TextRange = selection.GetAsOneRange()'Get the owner paragraph of the text rangeDim paragraph As Paragraph = textRange.OwnerParagraph'Get the index of the text range in the paragraphDim index As Integer = paragraph.ChildObjects.IndexOf(textRange)'Add a footnote to the paragraphDim footnote As Footnote = paragraph.AppendFootnote(FootnoteType.Footnote)'Insert the footnote reference mark after the text rangeparagraph.ChildObjects.Insert(index + 1, footnote)'Set the footnote texttextRange = footnote.TextBody.AddParagraph().AppendText("Developed by E-iceblue Co., LTD.")'Set format for the footnote texttextRange.CharacterFormat.FontName = "Arial Black"textRange.CharacterFormat.FontSize = 12textRange.CharacterFormat.TextColor = Color.DarkGray'Set format for the footnote reference markfootnote.MarkerCharacterFormat.FontName = "Calibri"footnote.MarkerCharacterFormat.FontSize = 12footnote.MarkerCharacterFormat.Bold = Truefootnote.MarkerCharacterFormat.TextColor = Color.DarkGreen'Save the result documentdocument.SaveToFile("InsertFootnote.docx", FileFormat.Docx)document.Close()End SubEnd ClassEnd Namespace

在 C# 和 的 Word 中插入尾注

尾注由两部分组成 —— 尾注引用标记和相应的尾注文本。为特定文本插入尾注的步骤与上述示例非常相似:

初始化文档。使用 加载 Word 文档 Document.LoadFromFile() 方法。使用 Document.FindString() 方法,并使用以下命令将找到的文本作为单个文本范围获取 TextSelection.GetAsOneRange() 方法。通过以下方式访问文本范围的所有者段落 TextRange.OwnerParagraph 属性,并使用 Paragraph.ChildObjects.IndexOf() 方法。使用 向段落添加尾注 Paragraph.AppendFootnote(FootnoteType.Endnote) 方法。在文本范围后插入尾注引用标记 Paragraph.ChildObjects.Insert() 方法。使用 设置尾注文本 Footnote.TextBody.AddParagraph().追加文本() 方法。设置尾注文本和参考标记的字体名称、字体大小和文本颜色等格式。保存结果文档使用 Document.SaveToFile() 方法。

C#

using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;using System.Drawing;namespace InsertEndnote{internal class Program{static void Main(string[] args){//Initialize an instance of the Document classDocument document = new Document();//Load a Word documentdocument.LoadFromFile(@"Sample.docx");//Find a specific text in the documentTextSelection selection = document.FindString("Microsoft Office", false, true);//Get the found text as a single text rangeTextRange textRange = selection.GetAsOneRange();//Get the owner paragraph of the text rangeParagraph paragraph = textRange.OwnerParagraph;//Get the index of the text range in the paragraphint index = paragraph.ChildObjects.IndexOf(textRange);//Add an endnote to the paragraphFootnote endnote = paragraph.AppendFootnote(FootnoteType.Endnote);//Insert the endnote reference mark after the text rangeparagraph.ChildObjects.Insert(index + 1, endnote);//Set the endnote texttextRange = endnote.TextBody.AddParagraph().AppendText("Developed by Microsoft.");//Set format for the endnote texttextRange.CharacterFormat.FontName = "Arial Black";textRange.CharacterFormat.FontSize = 12;textRange.CharacterFormat.TextColor = Color.DarkGray;//Set format for the endnote reference markendnote.MarkerCharacterFormat.FontName = "Calibri";endnote.MarkerCharacterFormat.FontSize = 12;endnote.MarkerCharacterFormat.Bold = true;endnote.MarkerCharacterFormat.TextColor = Color.DarkGreen;//Save the result documentdocument.SaveToFile("InsertEndnote.docx", FileFormat.Docx);document.Close();}}}

Imports Spire.DocImports Spire.Doc.DocumentsImports Spire.Doc.FieldsImports System.DrawingNamespace InsertEndnoteFriend Class ProgramPrivate Shared Sub Main(ByVal args As String())'Initialize an instance of the Document classDim document As Document = New Document()'Load a Word documentdocument.LoadFromFile("Sample.docx")'Find a specific text in the documentDim selection As TextSelection = document.FindString("Microsoft Office", False, True)'Get the found text as a single text rangeDim textRange As TextRange = selection.GetAsOneRange()'Get the owner paragraph of the text rangeDim paragraph As Paragraph = textRange.OwnerParagraph'Get the index of the text range in the paragraphDim index As Integer = paragraph.ChildObjects.IndexOf(textRange)'Add a endnote to the paragraphDim endnote As Footnote = paragraph.AppendFootnote(FootnoteType.Endnote)'Insert the endnote reference mark after the text rangeparagraph.ChildObjects.Insert(index + 1, endnote)'Set the endnote texttextRange = endnote.TextBody.AddParagraph().AppendText("Developed by Microsoft.")'Set format for the endnote texttextRange.CharacterFormat.FontName = "Arial Black"textRange.CharacterFormat.FontSize = 12textRange.CharacterFormat.TextColor = Color.DarkGray'Set format for the endnote reference markendnote.MarkerCharacterFormat.FontName = "Calibri"endnote.MarkerCharacterFormat.FontSize = 12endnote.MarkerCharacterFormat.Bold = Trueendnote.MarkerCharacterFormat.TextColor = Color.DarkGreen'Save the result documentdocument.SaveToFile("InsertEndnote.docx", FileFormat.Docx)document.Close()End SubEnd ClassEnd Namespace

申请临时许可证

如果您想从生成的文档中删除评估消息,或摆脱功能限制,请 为自己申请 30 天试用许可证 。

以上便是如何使用C#或:在 Word 文档中插入脚注和尾注的方法,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。