翻译|使用教程|编辑:颜馨|2023-04-11 11:00:02.017|阅读 71 次
概述:本文主要介绍如何在C#或VB.NET中调整Word文档中插入脚注和尾注,欢迎查阅!
# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>
相关链接:
Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。
E-iceblue 功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式
脚注和尾注是简短的注释,可用于对文档中的某些单词或句子提供解释、注释或引用。脚注通常出现在包含其参考编号的页面底部,而尾注出现在文档或章节的末尾。如果您正在用 Word 撰写学术论文,则插入脚注或尾注可能是必不可少的。本文将演示如何用Spire.Doc for .NET.在 C# 和 VB.NET 的 Word 文档中插入脚注和尾注。
首先,您需要将 Spire.Doc for .NET 包中包含的 DLL 文件添加为 .NET 项目中的引用。DLL 文件可以从过以下方式安装 NuGet.
PM> Install-Package Spire.Doc
脚注由两部分组成 :脚注参考标记和相应的脚注文本。要插入特定文本的脚注,您需要搜索文本并获取文本所在的段落,然后在段落中添加脚注,然后在找到的文本后插入脚注引用标记并设置脚注文本。具体步骤如下:
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 class Document document = new Document(); //Load a Word document document.LoadFromFile(@"Sample.docx"); //Find a specific text in the document TextSelection selection = document.FindString("Spire.Doc for .NET", false, true); //Get the found text as a single text range TextRange textRange = selection.GetAsOneRange(); //Get the owner paragraph of the text range Paragraph paragraph = textRange.OwnerParagraph; //Get the index of the text range in the paragraph int index = paragraph.ChildObjects.IndexOf(textRange); //Add a footnote to the paragraph Footnote footnote = paragraph.AppendFootnote(FootnoteType.Footnote); //Insert the footnote reference mark after the text range paragraph.ChildObjects.Insert(index + 1, footnote); //Set the footnote text textRange = footnote.TextBody.AddParagraph().AppendText("Developed by E-iceblue Co., LTD."); //Set format for the footnote text textRange.CharacterFormat.FontName = "Arial Black"; textRange.CharacterFormat.FontSize = 12; textRange.CharacterFormat.TextColor = Color.DarkGray; //Set format for the footnote reference mark footnote.MarkerCharacterFormat.FontName = "Calibri"; footnote.MarkerCharacterFormat.FontSize = 12; footnote.MarkerCharacterFormat.Bold = true; footnote.MarkerCharacterFormat.TextColor = Color.DarkGreen; //Save the result document document.SaveToFile("InsertFootnote.docx", FileFormat.Docx2013); document.Close(); } } }
VB.NET
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Imports System.Drawing Namespace InsertFootnote Friend Class Program Private Shared Sub Main(ByVal args As String()) 'Initialize an instance of the Document class Dim document As Document = New Document() 'Load a Word document document.LoadFromFile("Sample.docx") 'Find a specific text in the document Dim selection As TextSelection = document.FindString("Spire.Doc for .NET", False, True) 'Get the found text as a single text range Dim textRange As TextRange = selection.GetAsOneRange() 'Get the owner paragraph of the text range Dim paragraph As Paragraph = textRange.OwnerParagraph 'Get the index of the text range in the paragraph Dim index As Integer = paragraph.ChildObjects.IndexOf(textRange) 'Add a footnote to the paragraph Dim footnote As Footnote = paragraph.AppendFootnote(FootnoteType.Footnote) 'Insert the footnote reference mark after the text range paragraph.ChildObjects.Insert(index + 1, footnote) 'Set the footnote text textRange = footnote.TextBody.AddParagraph().AppendText("Developed by E-iceblue Co., LTD.") 'Set format for the footnote text textRange.CharacterFormat.FontName = "Arial Black" textRange.CharacterFormat.FontSize = 12 textRange.CharacterFormat.TextColor = Color.DarkGray 'Set format for the footnote reference mark footnote.MarkerCharacterFormat.FontName = "Calibri" footnote.MarkerCharacterFormat.FontSize = 12 footnote.MarkerCharacterFormat.Bold = True footnote.MarkerCharacterFormat.TextColor = Color.DarkGreen 'Save the result document document.SaveToFile("InsertFootnote.docx", FileFormat.Docx2013) document.Close() End Sub End Class End Namespace
尾注由两部分组成 —— 尾注引用标记和相应的尾注文本。为特定文本插入尾注的步骤与上述示例非常相似:
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 class Document document = new Document(); //Load a Word document document.LoadFromFile(@"Sample.docx"); //Find a specific text in the document TextSelection selection = document.FindString("Microsoft Office", false, true); //Get the found text as a single text range TextRange textRange = selection.GetAsOneRange(); //Get the owner paragraph of the text range Paragraph paragraph = textRange.OwnerParagraph; //Get the index of the text range in the paragraph int index = paragraph.ChildObjects.IndexOf(textRange); //Add an endnote to the paragraph Footnote endnote = paragraph.AppendFootnote(FootnoteType.Endnote); //Insert the endnote reference mark after the text range paragraph.ChildObjects.Insert(index + 1, endnote); //Set the endnote text textRange = endnote.TextBody.AddParagraph().AppendText("Developed by Microsoft."); //Set format for the endnote text textRange.CharacterFormat.FontName = "Arial Black"; textRange.CharacterFormat.FontSize = 12; textRange.CharacterFormat.TextColor = Color.DarkGray; //Set format for the endnote reference mark endnote.MarkerCharacterFormat.FontName = "Calibri"; endnote.MarkerCharacterFormat.FontSize = 12; endnote.MarkerCharacterFormat.Bold = true; endnote.MarkerCharacterFormat.TextColor = Color.DarkGreen; //Save the result document document.SaveToFile("InsertEndnote.docx", FileFormat.Docx2013); document.Close(); } } }
VB.NET
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Imports System.Drawing Namespace InsertEndnote Friend Class Program Private Shared Sub Main(ByVal args As String()) 'Initialize an instance of the Document class Dim document As Document = New Document() 'Load a Word document document.LoadFromFile("Sample.docx") 'Find a specific text in the document Dim selection As TextSelection = document.FindString("Microsoft Office", False, True) 'Get the found text as a single text range Dim textRange As TextRange = selection.GetAsOneRange() 'Get the owner paragraph of the text range Dim paragraph As Paragraph = textRange.OwnerParagraph 'Get the index of the text range in the paragraph Dim index As Integer = paragraph.ChildObjects.IndexOf(textRange) 'Add a endnote to the paragraph Dim endnote As Footnote = paragraph.AppendFootnote(FootnoteType.Endnote) 'Insert the endnote reference mark after the text range paragraph.ChildObjects.Insert(index + 1, endnote) 'Set the endnote text textRange = endnote.TextBody.AddParagraph().AppendText("Developed by Microsoft.") 'Set format for the endnote text textRange.CharacterFormat.FontName = "Arial Black" textRange.CharacterFormat.FontSize = 12 textRange.CharacterFormat.TextColor = Color.DarkGray 'Set format for the endnote reference mark endnote.MarkerCharacterFormat.FontName = "Calibri" endnote.MarkerCharacterFormat.FontSize = 12 endnote.MarkerCharacterFormat.Bold = True endnote.MarkerCharacterFormat.TextColor = Color.DarkGreen 'Save the result document document.SaveToFile("InsertEndnote.docx", FileFormat.Docx2013) document.Close() End Sub End Class End Namespace
如果您想从生成的文档中删除评估消息,或摆脱功能限制,请 为自己申请 30 天试用许可证 。
以上便是如何使用C#或VB.NET:在 Word 文档中插入脚注和尾注的方法,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。
欢迎下载|体验更多E-iceblue产品
获取更多信息请咨询 ;技术交流Q群(767755948)
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn