翻譯|使用教程|編輯:胡濤|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