翻譯|使用教程|編輯:胡濤|2023-03-20 10:46:12.860|閱讀 166 次
概述:本指南將演示使用 C# 和 VB.NET 通過 Spire.Doc for .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 頁眉和頁腳呈現Word 文檔的附加信息,可以是文本、圖像或頁碼。本指南著重介紹如何在 C# 和 VB.NET 中為 Word 文檔插入圖像頁眉和頁腳。
頁眉/頁腳在 Word 文檔中起著重要的作用,它使用文本、圖像或頁碼來展示有關該文檔的一些附加信息。這些信息可以是公司名稱、徽標、作者姓名、文檔標題等。本指南將演示使用 C# 和 VB.NET 通過 Spire.Doc for .NET 在 Word 中插入圖像頁眉/頁腳的詳細過程。以下屏幕截圖顯示了編程后的 Word 圖像頁眉/頁腳結果。
Spire.Doc for .NET 提供了一個HeaderFooter。類使開發人員能夠生成新的頁眉或頁腳。首先,初始化一個 HeaderFooter 類的頁眉實例,然后調用AddParagraph()方法為這個頁眉/頁腳實例添加段落主體。接下來,調用Paragraph.AppendPicture(Image image)方法為頁眉/頁腳段落附加圖片。如果您還想為段落添加文本,請調用 Paragraph.AppendText(string text) 方法。此外,您還可以設置頁眉/頁腳段落、附加圖像和文本的格式,以獲得更好的布局。代碼如下:
[C#]
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace ImageHeaderFooter
{
class Program
{
static void Main(string[] args)
{
//Load Document
Document document = new Document();
document.LoadFromFile(@"E:\Work\Documents\Spire.Doc for .NET.docx");
//Initialize a Header Instance
HeaderFooter header = document.Sections[0].HeadersFooters.Header;
//Add Header Paragraph and Format
Paragraph paragraph = header.AddParagraph();
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Right;
//Append Picture for Header Paragraph and Format
DocPicture headerimage = paragraph.AppendPicture(Image.FromFile(@"E:\Logo\doclog.png"));
headerimage.VerticalAlignment = ShapeVerticalAlignment.Bottom;
//Initialize a Footer Instance
HeaderFooter footer = document.Sections[0].HeadersFooters.Footer;
//Add Footer Paragraph and Format
Paragraph paragraph2 = footer.AddParagraph();
paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Left;
//Append Picture and Text for Footer Paragraph
DocPicture footerimage = paragraph2.AppendPicture(Image.FromFile(@"E:\Logo\logo.jpeg"));
TextRange TR = paragraph2.AppendText("Copyright © 2013 e-iceblue. All Rights Reserved.");
TR.CharacterFormat.FontName = "Arial";
TR.CharacterFormat.FontSize = 10;
TR.CharacterFormat.TextColor = Color.Black;
//Save and Launch
document.SaveToFile("ImageHeaderFooter.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("ImageHeaderFooter.docx");
}
}
}
[VB.NET]
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Namespace ImageHeaderFooter
Friend Class Program
Shared Sub Main(ByVal args() As String)
'Load Document
Dim document As New Document()
document.LoadFromFile("E:\Work\Documents\Spire.Doc for .NET.docx")
'Initialize a Header Instance
Dim header As HeaderFooter = document.Sections(0).HeadersFooters.Header
'Add Header Paragraph and Format
Dim paragraph As Paragraph = header.AddParagraph()
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Right
'Append Picture for Header Paragraph and Format
Dim headerimage As DocPicture = paragraph.AppendPicture(Image.FromFile("E:\Logo\doclog.png"))
headerimage.VerticalAlignment = ShapeVerticalAlignment.Bottom
'Initialize a Footer Instance
Dim footer As HeaderFooter = document.Sections(0).HeadersFooters.Footer
'Add Footer Paragraph and Format
Dim paragraph2 As Paragraph = footer.AddParagraph()
paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Left
'Append Picture and Text for Footer Paragraph
Dim footerimage As DocPicture = paragraph2.AppendPicture(Image.FromFile("E:\Logo\logo.jpeg"))
Dim TR As TextRange = paragraph2.AppendText("Copyright © 2013 e-iceblue. All Rights Reserved.")
TR.CharacterFormat.FontName = "Arial"
TR.CharacterFormat.FontSize = 10
TR.CharacterFormat.TextColor = Color.Black
'Save and Launch
document.SaveToFile("ImageHeaderFooter.docx", FileFormat.Docx)
System.Diagnostics.Process.Start("ImageHeaderFooter.docx")
End Sub
End Class
End Namespace
以上便是用Word 插入圖像頁眉和頁腳,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
獲取更多信息請咨詢 ;技術交流Q群(767755948)
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn