翻译|使用教程|编辑:胡涛|2023-04-17 13:33:57.027|阅读 160 次
概述: 本文介绍如何在 Word 中插入或删除脚注-C#/VB.NET,欢迎查阅~
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
相关链接:
Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。
E-iceblue 功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式
脚注是放在页面底部的注释。在 MS Word 中,您可以使用脚注来引用参考文献、给出解释或发表评论,而不会影响正文。在本文中,您将了解如何使用Spire.Doc for .NET在 Word 文档中插入或删除脚注。
首先,您需要添加包含在 Spire.Doc for.NET 包中的 DLL 文件作为您的 .NET 项目中的引用。DLL 文件可以从此链接下载或通过NuGet安装。
PM> Install-Package Spire.Doc
Spire.Doc for .NET 提供的Paragraph.AppendFootnote (FootnoteType.Footnote) 方法允许您在指定段落后插入脚注。以下是详细步骤。
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);
}
}
}
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
使用 Spire.Doc for .NET,还可以在文档中任意位置的指定文本后插入脚注。以下是详细步骤。
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);
}
}
}
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#
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[i] 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("RemoveFootnote.docx", 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)
'Create a Document instance
Dim document As Document = New Document
'Load a sample Word document
document.LoadFromFile("AddFootnote.docx")
'Get the first section
Dim section As Section = document.Sections(0)
'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
'Save the result document
document.SaveToFile("RemoveFootnote.docx", FileFormat.Docx)
End Sub
End Class
End Namespace
以上便是如何在 Word 中插入或删除脚注-C#/VB.NET,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。
欢迎下载|体验更多E-iceblue产品
获取更多信息请咨询 ;技术交流Q群(767755948)
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ke049m.cn