|
Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。
ss="nolink">E-iceblue 功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式
脚注是放在页面底部的注释。在 MS Word 中,您可以使用脚注来引用参考文献、给出解释或发表评论,而不会影响正文。在本文中,您将了解如何使用Spire.Doc for .NET在 Word 文档中插入或删除脚注。
安装适用于 .NET 的 Spire.Doc
首先,您需要添加包含在 Spire.Doc http://for.NET 包中的 DLL 文件作为您的 .NET 项目中的引用。DLL 文件可以从此链接下载或通过NuGet安装。
PM> Install-Package Spire.Doc在 C# 和 http://VB.NET 中的 Word 中的特定段落后插入脚注
Spire.Doc for .NET 提供的Paragraph.AppendFootnote (FootnoteType.Footnote) 方法允许您在指定段落后插入脚注。以下是详细步骤。
- 创建文档实例
- 使用Document.LoadFromFile()方法加载示例 Word 文档。
- 获取第一节,然后获取该节中的指定段落。
- 使用Paragraph.AppendFootnote(FootnoteType.Footnote)方法在段落末尾添加脚注。
- 设置脚注的文字内容、字体和颜色,然后设置脚注上标编号的格式。
- 使用Document.SaveToFile()方法保存结果文档 。
C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace AddFootnote
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new Document();
//Load a sample Word document
document.LoadFromFile("Sample.docx");
//Get the first section
Section section = document.Sections[0];
//Get a specified paragraph in the section
Paragraph paragraph = section.Paragraphs[3];
//Add a footnote at the end of the paragraph
Footnote footnote = paragraph.AppendFootnote(FootnoteType.Footnote);
//Set the text content of the footnote
TextRange text = footnote.TextBody.AddParagraph().AppendText("Algorithms can be simple or complex depending on what you want to achieve.");
//Set the text font and color
text.CharacterFormat.FontName = "Arial";
text.CharacterFormat.FontSize = 12;
text.CharacterFormat.TextColor = Color.DarkBlue;
//Set the format of the footnote superscript number
footnote.MarkerCharacterFormat.FontName = "Calibri";
footnote.MarkerCharacterFormat.FontSize = 15;
footnote.MarkerCharacterFormat.Bold = true;
footnote.MarkerCharacterFormat.TextColor = Color.DarkCyan;
//Save the result document
document.SaveToFile("AddFootnote.docx", FileFormat.Docx);
}
}
}http://VB.NET
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports System.Drawing
Namespace AddFootnote
Class Program
Private Shared Sub Main(ByVal args() As String)
'Create a Document instance
Dim document As Document = New Document
'Load a sample Word document
document.LoadFromFile("Sample.docx")
'Get the first section
Dim section As Section = document.Sections(0)
'Get a specified paragraph in the section
Dim paragraph As Paragraph = section.Paragraphs(3)
'Add a footnote at the end of the paragraph
Dim footnote As Footnote = paragraph.AppendFootnote(FootnoteType.Footnote)
'Set the text content of the footnote
Dim text As TextRange = footnote.TextBody.AddParagraph.AppendText("Algorithms can be simple or complex depending on what you want to achieve.")
'Set the text font and color
text.CharacterFormat.FontName = "Arial"
text.CharacterFormat.FontSize = 12
text.CharacterFormat.TextColor = Color.DarkBlue
'Set the format of the footnote superscript number
footnote.MarkerCharacterFormat.FontName = "Calibri"
footnote.MarkerCharacterFormat.FontSize = 15
footnote.MarkerCharacterFormat.Bold = True
footnote.MarkerCharacterFormat.TextColor = Color.DarkCyan
'Save the result document
document.SaveToFile("AddFootnote.docx", FileFormat.Docx)
End Sub
End Class
End Namespace

在 C# 和 http://VB.NET 中的 Word 中的特定文本后插入脚注
使用 Spire.Doc for .NET,还可以在文档中任意位置的指定文本后插入脚注。以下是详细步骤。
- 创建一个文档实例。
- 使用Document.LoadFromFile()方法加载 Word 文档。
- 使用Document.FindString()方法查找指定文本。
- 使用TextSelection.GetAsOneRange()方法获取指定文本的文本范围。
- 使用TextRange.OwnerParagraph属性获取文本范围所在的段落。
- 使用Paragraph.ChildObjects.IndexOf()方法获取段落中文本范围的位置索引。
- 使用Paragraph.AppendFootnote(FootnoteType.Footnote)方法添加脚注,然后使用Paragraph.ChildObjects.Insert()方法在指定文本后插入脚注
- 设置脚注的文字内容、字体和颜色,然后设置脚注上标编号的格式。
- 使用Document.SaveToFile()方法保存结果文档。
C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace InsertFootnote
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new Document();
//Load a sample Word document
document.LoadFromFile("Sample.docx");
//Find a specified text string
TextSelection selection = document.FindString("big O notation", false, true);
//Get the text range of the specified text
TextRange textRange = selection.GetAsOneRange();
//Get the paragraph where the text range is located
Paragraph paragraph = textRange.OwnerParagraph;
//Get the position index of the text range in the paragraph
int index = paragraph.ChildObjects.IndexOf(textRange);
//Add a footnote
Footnote footnote = paragraph.AppendFootnote(FootnoteType.Footnote);
//Insert the footnote after the specified paragraph
paragraph.ChildObjects.Insert(index + 1, footnote);
//Set the text content of the footnote
TextRange text = footnote.TextBody.AddParagraph().AppendText("It gives the worst-case complexity of an algorithm.");
//Set the text font and color
text.CharacterFormat.FontName = "Arial";
text.CharacterFormat.FontSize = 12;
text.CharacterFormat.TextColor = Color.DarkBlue;
//Set the format of the footnote superscript number
footnote.MarkerCharacterFormat.FontName = "Calibri";
footnote.MarkerCharacterFormat.FontSize = 15;
footnote.MarkerCharacterFormat.Bold = true;
footnote.MarkerCharacterFormat.TextColor = Color.DarkGreen;
//Save the result document
document.SaveToFile("InsertFootnote.docx", FileFormat.Docx);
}
}
}http://VB.NET
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports System.Drawing
Namespace InsertFootnote
Class Program
Private Shared Sub Main(ByVal args() As String)
'Create a Document instance
Dim document As Document = New Document
'Load a sample Word document
document.LoadFromFile("Sample.docx")
'Find a specified text string
Dim selection As TextSelection = document.FindString("big O notation", False, True)
'Get the text range of the specified text
Dim textRange As TextRange = selection.GetAsOneRange
'Get the paragraph where the text range is located
Dim paragraph As Paragraph = textRange.OwnerParagraph
'Get the position index of the text range in the paragraph
Dim index As Integer = paragraph.ChildObjects.IndexOf(textRange)
'Add a footnote
Dim footnote As Footnote = paragraph.AppendFootnote(FootnoteType.Footnote)
'Insert the footnote after the specified paragraph
paragraph.ChildObjects.Insert((index + 1), footnote)
'Set the text content of the footnote
Dim text As TextRange = footnote.TextBody.AddParagraph.AppendText("It gives the worst-case complexity of an algorithm.")
'Set the text font and color
text.CharacterFormat.FontName = "Arial"
text.CharacterFormat.FontSize = 12
text.CharacterFormat.TextColor = Color.DarkBlue
'Set the format of the footnote superscript number
footnote.MarkerCharacterFormat.FontName = "Calibri"
footnote.MarkerCharacterFormat.FontSize = 15
footnote.MarkerCharacterFormat.Bold = True
footnote.MarkerCharacterFormat.TextColor = Color.DarkGreen
'Save the result document
document.SaveToFile("InsertFootnote.docx", FileFormat.Docx)
End Sub
End Class
End Namespace

在 C# 和 http://VB.NET 中删除 Word 文档中的脚注
手动搜索和删除文档中现有的脚注需要花费时间和精力。以下是以编程方式一次删除所有脚注的步骤。
- 创建一个文档实例。
- 使用Document.LoadFromFile()方法加载 Word 文档。
- 使用Document.Sections属性获取指定部分。
- 遍历该部分中的每个段落以找到脚注。
- 使用Paragraph.ChildObjects.RemoveAt()方法删除脚注。
- 使用Document.SaveToFile()方法保存结果文档。
C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace RemoveFootnote
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new Document();
//Load a sample Word document
document.LoadFromFile("AddFootnote.docx");
//Get the first section
Section section = document.Sections[0];
//Traverse through each paragraph in the section to find the footnote
foreach (Paragraph para in section.Paragraphs)
{
int index = -1;
for (int i = 0, cnt = para.ChildObjects.Count; i < cnt; i++)
{
ParagraphBase pBase = para.ChildObjects as ParagraphBase;
if (pBase is Footnote)
{
index = i;
break;
}
}
if (index > -1)
//Remove the footnote
para.ChildObjects.RemoveAt(index);
}
//Save the result document
document.SaveToFile(&#34;RemoveFootnote.docx&#34;, FileFormat.Docx);
}
}
}
VB.NET
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Namespace RemoveFootnote
Class Program
Private Shared Sub Main(ByVal args() As String)
&#39;Create a Document instance
Dim document As Document = New Document
&#39;Load a sample Word document
document.LoadFromFile(&#34;AddFootnote.docx&#34;)
&#39;Get the first section
Dim section As Section = document.Sections(0)
&#39;Traverse through each paragraph in the section to find the footnote
For Each para As Paragraph In section.Paragraphs
Dim index As Integer = -1
Dim cnt As Integer = para.ChildObjects.Count
Do While (i < cnt)
Dim pBase As ParagraphBase = CType(para.ChildObjects(i), ParagraphBase)
Dim i As Integer = 0
If (TypeOf pBase Is Footnote) Then
index = i
Exit For
End If
i = (i + 1)
Loop
If (index > -1) Then
para.ChildObjects.RemoveAt(index)
End If
Next
&#39;Save the result document
document.SaveToFile(&#34;RemoveFootnote.docx&#34;, FileFormat.Docx)
End Sub
End Class
End Namespace

以上便是如何在 Word 中插入或删除脚注-C#/http://VB.NET,如果您有其他问题也可以继续浏览本系列文章,获取相关教程 |
|