
A quick view of MS Word document transformation.
頁首是每頁的頂部邊距,頁腳是每頁的底部邊距。頁首和頁尾可用於包含您想要在文件的每一頁上顯示的訊息,例如您的姓名、日期、文件標題、頁碼或文件免責聲明。使用 MS Word 時,它提供根據預訂佈局添加頁首和頁尾的功能,或允許您新增自訂頁首和頁尾。大多數內建標題都包含一些佔位符文本,您可以保留佔位符文本,也可以將其替換為您自己的資料饋送。此外,在更新頁首或頁尾部分中的資訊時,文件主體內的內容將變灰,這表示這些物件與頁面其餘內容完全隔離,並且您對這些物件執行的操作特定於這些區域。在本文中,我們將學習如何使用 REST API 在 Word 文件中新增或更新頁首頁腳的步驟。
文字處理API
Aspose.Words Cloud API 提供從雲端儲存載入 MS Word 檔案的功能,同時它允許使用者直接在請求正文中傳遞輸入文件,API 在回應物件中傳回更新後的檔案。它提供讀取、新增、更新或刪除特定或所有頁首和頁尾物件的功能。此外,您也可以指定為第一頁和奇數/偶數頁呈現不同的頁首/頁尾。
此外,該 API 還允許您設定頁首/頁尾文字的格式訊息,例如字體和段落屬性。您也可以利用槓桿將動態頁碼插入頁首/頁尾部分。最後但並非最不重要的一點是,您可以考慮使用表格物件使頁眉/頁腳的一部分文字對齊到左邊緣,另一部分文字對齊到右邊緣。
條款範圍
我們的 Aspose.Words Cloud SDK for .NET 可讓您快速輕鬆地使用 Aspose.Words Cloud REST API,因為它處理了有關發出請求和處理回應的許多低階細節,並讓您專注於編寫特定於您的業務需求的程式碼。在本文中,我們將在 Visual Studio for Mac 中使用 Aspose.Words Cloud SDK for .NET。這篇文章定義了在 Word 文件中插入頁首頁腳的步驟,其中將定義自訂格式。然後更新現有頁腳內容的格式並在右下角位置插入 PageNumber 資訊。所有這些操作都將在雲端進行。
安裝

為了開始,首先,您需要在系統上安裝 Visual Studio。建立範例解決方案並新增 Aspose.Words Cloud SDK for .NET 的 NuGet 參考。因此,為了新增引用,請右鍵單擊解決方案並選擇管理 NuGet 套件…選單選項。在搜尋文字欄位中輸入 Aspose.Words-Cloud。選擇該選項並點擊新增包按鈕。


現在解決方案中出現了 Aspose.Words.Cloud.Sdk 引用。與內部部署(aspose.com)API 相比,使用雲端 API 有一些好處。原因是,您不需要手動追蹤和更新 API 版本,也不需要支付整個許可證費用。
Aspose.Cloud 儀表板
只需在 Aspose.Cloud 儀表板 上建立帳戶,或透過您現有的 Google 或 GitHub 帳戶註冊,即可開始使用我們的雲端 API。雲端 API 的另一個令人驚嘆的功能是您可以在任何平台上使用任何受支援的語言來使用它們。
如上所述,本文強調了在 MS Word 文件中新增和更新頁首和頁尾物件的選項。為了進行測試,我們使用了名為「商業信函」的現有 MS Word 文件範本。該文件需要上傳到 Aspose 雲端存儲,所有操作和文件處理都將透過雲端執行。一旦所有處理完成,我們也可以從雲端儲存下載產生的文件副本。
using Aspose.Words.Cloud.Sdk;
using Aspose.Words.Cloud.Sdk.Model;
using Aspose.Words.Cloud.Sdk.Model.Requests;
現在,為了使用 Aspose.Cloud API,您需要在程式碼中使用 App Key 和 App SID 資訊。這些鍵由字母數字組成。這些是與您的訂閱帳戶相關的唯一代碼。為了獲取這些詳細信息,請單擊 Aspose.Cloud 儀表板 上的“我的應用程式”選項卡。一旦我們有了金鑰,我們就需要初始化 Aspose.Words API 物件。
string MyAppKey = "f6axxxxxxxxxxxxxxxxxx"; // Get AppKey and AppSID from https://dashboard.aspose.cloud/
string MyAppSid = "478e4827-xxxxx-xxxx-xxxx-xxxxxxx"; // Get AppKey and AppSID from https://dashboard.aspose.cloud/
// create an object of WordsApi while passing AppKey and AppSid information
WordsApi wordsApi = new WordsApi(MyAppKey, MyAppSid);
插入 Header 對象
為了在 MS Word 文件中新增頁首對象,我們需要按順序執行以下操作。
- 建立一個 InsertHeaderFooterRequest 對象,我們將 HeaderFooterLink 類型作為參數傳遞。
- 使用 InsertHeaderFooter (…) 方法將 HeaderFooterRequest 物件插入 WordsApi 物件。
- 建立一個包含範例文字資訊的 Run 物件。
- 建立 InsertRunRequest 對象,我們在其中傳遞 HeaderFooter 物件中段落(SectionPath)的引用,其中需要新增 Run 文字。
然後呼叫WordsApi的InsertRun(…)方法,將RunRequest加入HeaderFooter物件。
HeaderFooter.TypeEnum 可以是下列任一值。


string MyAppKey = "xxxxxxxx"; // Get AppKey and AppSID from https://dashboard.aspose.cloud/
string MyAppSid = "xxxxxxxxx"; // Get AppKey and AppSID from https://dashboard.aspose.cloud/
// 建立WordsAPI對象,並傳遞AppKey和AppSid訊息
WordsApi wordsApi = new WordsApi(MyAppKey, MyAppSid);
// 將範例文件上傳至雲端存儲
wordsApi.UploadFile(new UploadFileRequest(new System.IO.FileStream("/Users/nayyershahbaz/Documents/BusinessLetter.docx",
FileMode.Open, FileAccess.Read), "BusinessLetter.docx"));
// 在第一頁插入頁首對象
var putHeaderFooterRequest = new Aspose.Words.Cloud.Sdk.Model.Requests.InsertHeaderFooterRequest("BusinessLetter.docx", "HeaderFirst", null,null);
var actual = wordsApi.InsertHeaderFooter(putHeaderFooterRequest);
// 建立包含範例文字的 Run 對象
var run = new Run { Text = "Aspose.Words Cloud SDK for .NET " };
// 將運行文字新增至 Word 文件第一節中第一個 HeaderFooter 物件的第一段
var runRequest = new Aspose.Words.Cloud.Sdk.Model.Requests.InsertRunRequest("BusinessLetter.docx", "sections/0/headersfooters/1/paragraphs/0", run);
// 將 RunRequest 插入到 Word 文檔
var actual2 = wordsApi.InsertRun(runRequest);

Image showing preview of recently added Header
更新新插入標題的文字格式
一旦新增了 Header 對象,我們就可以更新其中內容的文字格式。為了滿足這項要求,我們需要執行以下步驟。
- 建立一個 Font 類別的對象,其中我們指定字體名稱、大小、顏色資訊。
- 為了使文本更加美觀,將 Engrave 屬性標記為 True。
- 建立 UpdateRunFontRequest 類別的對象,我們將輸入檔案、Font、SectionPath 和 runIndex 作為參數傳遞。
- 最後,使用 UpdateRunFont(..) 方法並將 UpdateRunFontRequest 物件新增至 WordsApi 實例。
string MyAppKey = "xxxxxxxx"; // Get AppKey and AppSID from https://dashboard.aspose.cloud/
string MyAppSid = "xxxxxxxxx"; // Get AppKey and AppSID from https://dashboard.aspose.cloud/
// 建立WordsAPI對象,並傳遞AppKey和AppSid訊息
WordsApi wordsApi = new WordsApi(MyAppKey, MyAppSid);
// 將範例文件上傳至雲端存儲
wordsApi.UploadFile(new UploadFileRequest(new System.IO.FileStream("/Users/nayyershahbaz/Documents/BusinessLetter.docx",
FileMode.Open, FileAccess.Read), "BusinessLetter.docx"));
// 下一步是設定 Header 物件的格式
var runIndex = 0;
var fontDto = new Font { Bold = true, Name = "Verdana", Size = 16, Color = new XmlColor { Web = "#e0a50d" } };
// 將文字格式設定為雕刻
fontDto.Engrave = true;
// 將格式應用於 HeaderFooter 物件的第一段
var documentParagraphRunFontRequest = new Aspose.Words.Cloud.Sdk.Model.Requests.UpdateRunFontRequest("BusinessLetter.docx",
fontDto, "sections/0/headersfooters/1/paragraphs/0", runIndex);
var actual4 = wordsApi.UpdateRunFont(documentParagraphRunFontRequest);

Image showing preview of Header after text formatting has been applied.
更新現有 Footer 物件的格式
此 API 也提供更新現有 Header / Footer 物件內內容格式的功能。為了滿足這個要求,我們可以使用上面指定的程式碼行,但唯一的區別是將 headerfooter 索引更改為 2。
// Apply formatting to first paragraph of HeaderFooter object
var FooterDocumentParagraphRunFontRequest = new Aspose.Words.Cloud.Sdk.Model.Requests.UpdateRunFontRequest("BusinessLetter.docx",
FooterFontDto, "sections/0/headersfooters/2/paragraphs/0", FooterrunIndex);

Image displaying the change in Footer text formatting.
在頁尾部分新增 PageNumber 訊息
頁碼提供非常有用的信息,例如當前頁面和文件的總頁數。以下步驟定義如何滿足要求。
- 首先建立一個 PageNumber 類別的對象,在其中我們定義文字的對齊方式、文字的格式、呈現 PageNumber 資訊的位置,並指定是否需要在第一頁呈現 PageNumber。
- 建立 InsertPageNumbersRequest 物件並將 PageNumber 物件作為參數傳遞。
- 最後,呼叫InsertPageNumbers(..)方法將InsertPageNumbersRequest加入到WordsApi實例。
string MyAppKey = "xxxxxxxx"; // Get AppKey and AppSID from https://dashboard.aspose.cloud/
string MyAppSid = "xxxxxxxxx"; // Get AppKey and AppSID from https://dashboard.aspose.cloud/
// 建立WordsAPI對象,並傳遞AppKey和AppSid訊息
WordsApi wordsApi = new WordsApi(MyAppKey, MyAppSid);
// 將範例文件上傳至雲端存儲
wordsApi.UploadFile(new UploadFileRequest(new System.IO.FileStream("/Users/nayyershahbaz/Documents/BusinessLetter.docx",
FileMode.Open, FileAccess.Read), "BusinessLetter.docx"));
// 該 API 還提供了在 HeaderFooter 物件中添加頁碼詳細資訊的功能
// 在頁面右下角添加頁碼信息
var body = new PageNumber { Alignment = "right", Format = "{PAGE} of {NUMPAGES}", IsTop = false, SetPageNumberOnFirstPage = true };
var insertPageNumbersRequest = new Aspose.Words.Cloud.Sdk.Model.Requests.InsertPageNumbersRequest("BusinessLetter.docx", body);
// 在 Word 文件中插入 PageNumber 訊息
var actual6 = wordsApi.InsertPageNumbers(insertPageNumbersRequest);

Preview Page number in the footer section.
結論
在本文中,我們學習如何使用 Aspose.Words Cloud SDK for .NET 在 Word 文件中新增頁首頁尾的步驟。請注意,我們也為 Java、PHP、Ruby、Python、Go、Swift、C++、Node.Js、Android 提供雲 SDK。欲了解更多詳細信息,請訪問Aspose.Words Cloud。
相關文章
我們還建議您訪問以下內容以了解更多信息