标题

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 文档中添加和更新页眉和页脚对象的选项。为了进行测试,我们使用了名为 Business Letter 的现有 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 文件中添加页眉对象,我们需要按顺序执行以下操作。

  1. 创建一个 InsertHeaderFooterRequest 对象,我们将 HeaderFooterLink 类型作为参数传递。
  2. 使用 InsertHeaderFooter (…) 方法将 HeaderFooterRequest 对象插入到 WordsApi 对象。
  3. 创建一个包含示例文本信息的 Run 对象。
  4. 创建 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 对象后,我们可以更新其中内容的文本格式。为了满足此要求,我们需要执行以下步骤。

  1. 创建一个 Font 类的对象,其中我们指定字体名称、大小、颜色信息。
  2. 为了使文本更加美观,将 Engrave 属性标记为 True。
  3. 创建 UpdateRunFontRequest 类的对象,我们将输入文件、Font、SectionPath 和 runIndex 作为参数传递。
  4. 最后,使用 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.

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 信息

页码提供了非常有用的信息,例如当前页面和文档中的总页数。以下步骤定义了如何满足要求。

  1. 首先创建一个 PageNumber 类的对象,在其中我们定义文本的对齐方式、文本的格式、呈现 PageNumber 信息的位置,并指定是否需要在第一页呈现 PageNumber。
  2. 创建 InsertPageNumbersRequest 对象并将 PageNumber 对象作为参数传递。
  3. 最后,调用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 文档中添加页眉页脚的步骤。请注意,我们还提供适用于 JavaPHPRubyPythonGoSwiftC++Node.JsAndroid 的 Cloud SDK。有关更多详细信息,请访问 Aspose.Words Cloud

相关文章

我们还建议您访问以下内容以了解更多信息