PDF 生成模板编辑器
开源可视化 PDF 生成引擎,支持自定义模板,并提供面向开发者的 API,灵活构建文档生成流程。
页眉页脚是指在文档的顶部和底部处添加标注,通常包含标题、页码、品牌标识等信息。通过给文档添加页眉和页脚,使文档更易导航和辨识,提高文档的可读性和专业性。
在 PDF 文档中只能存在一个页眉页脚,添加新的页眉页脚会覆盖旧的页眉页脚。
添加页眉页脚的步骤如下:
获取文档中的页眉页脚对象。
设置需要添加页眉页脚的页面。
设置页眉页脚颜色,字体大小等属性。
将页眉页脚更新到页面上。
以下是添加页眉页脚的示例代码:
// Open document from file path.
CPDFDocument document = new CPDFDocument(context);
document.open(pdfPath, password);
CPDFHeaderFooter headerFooter = document.getHeaderFooter();
int index = 0;
headerFooter.setText(index, "<<1,2>> page");
headerFooter.setTextColor(index, Color.RED);
headerFooter.setFontSize(index, 14);
headerFooter.setPages("0,1,2");
headerFooter.setRules(1);
headerFooter.update();// Open document from file path.
val document = CPDFDocument(context)
document.open(pdfPath, password)
val index = 0
document.headerFooter.apply {
setText(index, "<<1,2>> page")
setTextColor(index, Color.RED)
setFontSize(index, 14F)
pages = "0,1,2"
rules = 1
update()
}当文档已绑定并通过 CPDFReaderView 显示时,在对文档页面进行修改后,需要调用以下接口刷新视图,使变更立即生效:
cpdfReaderView.reloadPages();页眉页脚支持特定格式的正则表达式,当设置headerFooter.setRules(1)时生效,格式为:<<\d+,\d+>>|<<\d+>>|<<\d+,>>
i表示页码的起始值。i表示页码的起始值,f为页码位数,如果实际页码不够,会在前面自动补充0。 例如:当文本被设置为"<<1,2>> 页"时,在第一页上显示的文本为"01 页"。删除页眉页脚的步骤如下:
获取文档中的页眉页脚对象。
清除页眉页脚。
以下是删除页眉页脚的示例代码:
CPDFHeaderFooter headerFooter = document.getHeaderFooter();
headerFooter.clear();document.headerFooter.clear()当文档已绑定并通过 CPDFReaderView 显示时,在对文档页面进行修改后,需要调用以下接口刷新视图,使变更立即生效:
cpdfReaderView.reloadPages();