半岛权威十大直营(官方)网站

文档在线协同工具ONLYOFFICE教程:如何使用ONLYOFFICE API从第二页插入自定义页码

转帖|使用教程|编辑:吉炜炜|2025-02-26 10:13:05.040|阅读 5 次

概述:在这篇教程中,我们将使用其中一种方法创建一个宏,从第二页开始在整个文档中插入自定义页码。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>


ONLYOFFICE Docs,作为一款功能强大的在线编辑器,适用于您使用的平台的文本文档、电子表格、演示文稿、表单和 PDF 阅读器。此次 ONLYOFFICE发布全新版本8.3,整个套件具有多项增强功能↓↓↓

ONLYOFFICE Docs 最新下载

ONLYOFFICE 宏是功能强大的工具,允许用户扩展编辑器的功能并对其进行自定义以满足特定需求。在 ONLYOFFICE,我们致力于通过新功能不断增强我们的产品。随着版本8.3的发布,我们引入了一系列旨在简化您的工作流程的新 API 方法。在这篇教程中,我们将使用其中一种方法创建一个宏,从第二页开始在整个文档中插入自定义页码。

关于SetStartPageNumber方法

方法是类的一部分。此新方法允许您定义文档中特定部分的起始页码。以下是其实现的示例:

let doc = Api.GetDocument(); let paragraph = doc.GetElement(0);
paragraph.AddText("This section starts with second page number");
paragraph.AddPageBreak();
paragraph.AddText("Third page");
paragraph.AddPageBreak();
paragraph.AddText("Fourth page"); let section = paragraph.GetSection();
section.SetStartPageNumber(2); let header = section.GetHeader("default", true);
paragraph = header.GetElement(0);
paragraph.AddText("Page #");
paragraph.AddPageNumber(); let footer = section.GetFooter("default", true);
paragraph = footer.GetElement(0);
paragraph.AddText("Page #");
paragraph.AddPageNumber();


在这篇博文的后面,我们将使用此方法来设置文档第二页的起始页码。

构建宏

检索文档和章节

该宏首先使用Api.GetDocument()访问活动文档。它使用doc.GetSections()[0]
检索第一部分,该部分代表第一页:

let doc = Api.GetDocument(); let firstSection = doc.GetSections()[0];

配置第一页

// First Page Configuration
firstSection.SetTitlePage(true);
// Remove first page header/footer
firstSection.RemoveHeader("title");
firstSection.RemoveFooter("title");
  • SetTitlePage(true)将第一页标记为标题页。
  • RemoveHeader(“title”)RemoveFooter(“title”)确保标题页上不出现页眉或页脚。

设置后续页面

// Subsequent Pages Configuration
let finalSection = doc.GetFinalSection();
finalSection.SetStartPageNumber(0); // Sets start page number. Default is 0 => 1st numbered page is 1
  • finalSection变量代表将应用页眉、页脚和页码的部分。
  • SetStartPageNumber(0)确保编号从第二页正确开始,使第一个编号页面为“1”。

配置标头

// Header Configuration
let header = finalSection.GetHeader("default", true);
const headerText = header.GetElement(0);
headerText.AddPageNumber();
// Choose header justification (uncomment one):
//headerText.SetJc("left"); // Left alignment 
headerText.SetJc("center"); // Center alignment 
//headerText.SetJc("right"); // Right alignment
  • GetHeader(“default”,true)检索该部分的标题。
  • 使用GetElement(0)访问标题的第一个元素
  • AddPageNumber()插入动态页码。
  • SetJc ()方法定义标题文本的对齐方式,将其定位在左侧、居中或右侧。

配置页脚

// Footer Configuration - uncomment for inserting a page number in the footer
let footer = finalSection.GetFooter("default", true);
const footerText = footer.GetElement(0);
footerText.AddPageNumber();

// Choose footer justification (uncomment one):
// footerText.SetJc("left");    // Left alignment
footerText.SetJc("center");  // Center alignment
// footerText.SetJc("right");   // Right alignment
  • 使用GetElement(0)访问页脚的第一个元素
  • AddPageNumber()在页脚插入动态页码。
  • SetJc ()方法控制页脚文本的对齐方式,与页眉类似。

注意!默认情况下,页脚配置被注释掉。只需注释或取消注释相关部分,即可在页眉、页脚或两者之间切换插入编号。

整个宏如下:

let doc = Api.GetDocument();
let firstSection = doc.GetSections()[0];

// First Page Configuration
firstSection.SetTitlePage(true);
// Remove first page header/footer
firstSection.RemoveHeader("title");
firstSection.RemoveFooter("title");

// Subsequent Pages Configuration
let finalSection = doc.GetFinalSection();
finalSection.SetStartPageNumber(0); // Sets start page number. Default is 0 => 1st numbered page is 1

// Header Configuration
let header = finalSection.GetHeader("default", true);
const headerText = header.GetElement(0);
headerText.AddPageNumber();

// Choose header justification (uncomment one):
//headerText.SetJc("left"); // Left alignment 
headerText.SetJc("center"); // Center alignment 
//headerText.SetJc("right"); // Right alignment

// Footer Configuration - uncomment for inserting a page number in the footer
// let footer = finalSection.GetFooter("default", true);
// const footerText = footer.GetElement(0);
// footerText.AddPageNumber();

// Choose footer justification (uncomment one):
// footerText.SetJc("left");    // Left alignment
// footerText.SetJc("center");  // Center alignment
// footerText.SetJc("right");   // Right alignment

让我们运行宏并看看它是如何工作的!

我们希望此宏能成为您工具包中有价值的补充,帮助您简化工作流程。在 ONLYOFFICE,我们致力于为用户提供多功能功能和灵活性,以满足他们的特定需求。

在最近的发布中,我们为 API 库引入了大量新方法。欢迎您探索这些增强功能并创建自己的宏。


————————————————————————————————————————

慧都是ONLYOFFICE在中国的官方授权代理商,提供ONLYOFFICE系列产品免费试用,咨询,正版销售等于一体的专业化服务。

下载|体验更多ONLYOFFICE产品咨询,或拨打产品热线:023-68661681


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@wqylolg.cn

文章转载自:慧都网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP