翻譯|使用教程|編輯:胡濤|2023-03-31 10:26:47.143|閱讀 200 次
概述:本文將介紹設置頁面邊框是否包圍頁眉/頁腳的方法。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Spire.Doc for .NET是一款專門對 Word 文檔進行操作的 .NET 類庫。在于幫助開發(fā)人員無需安裝 Microsoft Word情況下,輕松快捷高效地創(chuàng)建、編輯、轉換和打印 Microsoft Word 文檔。擁有近10年專業(yè)開發(fā)經驗Spire系列辦公文檔開發(fā)工具,專注于創(chuàng)建、編輯、轉換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團隊研發(fā),不依賴第三方軟件,不受其他國家的技術或法律法規(guī)限制,同時適配國產操作系統(tǒng)如中科方德、中標麒麟等,兼容國產文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
在 MS Word 頁面邊框選項中,有復選框可以選擇頁面邊框是否包圍頁眉/頁腳。此功能非常重要,可用于管理頁面邊框和頁眉/頁腳之間的位置關系。Spire.Doc 支持設置兩個常用功能:頁面邊框和頁眉/頁腳,也支持管理它們的空間關系。本文將介紹設置頁面邊框是否包圍頁眉/頁腳的方法。
注意:開始之前,請下載最新版本的Spire.Doc,并在bin文件夾中添加.dll作為Visual Studio的引用。
第 1 步:創(chuàng)建一個 Word 文檔并添加一個部分。
Document document = new Document(); Section section = document.AddSection();
第 2 步:使用 Spire.Doc 庫向文檔添加示例頁面邊框。
section.PageSetup.Borders.BorderType = BorderStyle.Wave; section.PageSetup.Borders.Color = Color.Green; section.PageSetup.Borders.Left.Space = 20; section.PageSetup.Borders.Right.Space = 20;
第 3 步:使用 Spire.Doc 庫將樣本頁眉和頁腳添加到文檔中。
//add a header and set its format
Paragraph paragraph1 = section.HeadersFooters.Header.AddParagraph();
paragraph1.Format.HorizontalAlignment = HorizontalAlignment.Right;
TextRange headerText = paragraph1.AppendText("Header isn't included in page border");
headerText.CharacterFormat.FontName = "Calibri";
headerText.CharacterFormat.FontSize = 20;
headerText.CharacterFormat.Bold = true;
//add a footer and set its format
Paragraph paragraph2 = section.HeadersFooters.Footer.AddParagraph();
paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Left;
TextRange footerText = paragraph2.AppendText("Footer is included in page border");
footerText.CharacterFormat.FontName = "Calibri";
footerText.CharacterFormat.FontSize = 20;
footerText.CharacterFormat.Bold = true;
第 4 步:設置頁眉不包含在頁面邊框中,頁腳包含在內。
section.PageSetup.PageBorderIncludeHeader = false; section.PageSetup.HeaderDistance = 40; section.PageSetup.PageBorderIncludeFooter = true; section.PageSetup.FooterDistance = 40;
第 5 步:保存文檔并啟動以查看效果。
System.Diagnostics.Process.Start("result.docx");
效果:
	 
 
document.SaveToFile("result.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("result.docx");
完整代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace Mirror_Margin
{
class Program
{
static void Main(string[] args)
{
Document document = new Document();
Section section = document.AddSection();
section.PageSetup.Borders.BorderType = BorderStyle.Wave;
section.PageSetup.Borders.Color = Color.Green;
section.PageSetup.Borders.Left.Space = 20;
section.PageSetup.Borders.Right.Space = 20;
Paragraph paragraph1 = section.HeadersFooters.Header.AddParagraph();
paragraph1.Format.HorizontalAlignment = HorizontalAlignment.Right;
TextRange headerText = paragraph1.AppendText("Header isn't included in page border");
headerText.CharacterFormat.FontName = "Calibri";
headerText.CharacterFormat.FontSize = 20;
headerText.CharacterFormat.Bold = true;
Paragraph paragraph2 = section.HeadersFooters.Footer.AddParagraph();
paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Left;
TextRange footerText = paragraph2.AppendText("Footer is included in page border");
footerText.CharacterFormat.FontName = "Calibri";
footerText.CharacterFormat.FontSize = 20;
footerText.CharacterFormat.Bold = true;
section.PageSetup.PageBorderIncludeHeader = false;
section.PageSetup.HeaderDistance = 40;
section.PageSetup.PageBorderIncludeFooter = true;
section.PageSetup.FooterDistance = 40;
document.SaveToFile("result.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("result.docx");
}
}
}
以上便如何設置頁面邊框是否包圍頁眉/頁腳,如果您有其他問題也可以繼續(xù)瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
獲取更多信息請咨詢 ;技術交流Q群(767755948)
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn