页眉页脚
福昕 PDF SDK iOS 版通过 Core SDK 提供了添加页眉页脚的能力,支持在页面的六个位置(左上、中上、右上、左下、中下、右下)添加自定义文本内容,可包含页码、日期等动态占位符。
核心类
| 类 | 说明 |
|---|---|
FSHeaderFooter | 页眉页脚配置对象,包含字体、大小、颜色、边距、页面范围和内容等属性 |
FSHeaderFooterContent | 页眉页脚内容对象,定义六个位置的文本内容 |
FSHeaderFooterContentGenerator | 内容生成器,用于组合文本、日期和页码并生成最终字符串 |
FSHeaderFooter 主要属性
| 属性 | 说明 |
|---|---|
font | 字体对象 |
text_size | 文字大小 |
text_color | 文字颜色(0xRRGGBB) |
page_range | 页面范围(FSPageNumberRange) |
page_margin | 页面边距 |
start_page_number | 起始页码编号 |
content | 六个位置的文本内容(FSHeaderFooterContent) |
has_text_shrinked | 文本是否自动缩放以适应空间 |
has_fixedsize_for_print | 打印时是否使用固定尺寸 |
is_to_embed_font | 是否嵌入字体 |
is_underline | 是否添加下划线 |
示例:添加页眉页脚
objc
#import <FoxitRDK/FSPDFObjC.h>
FSPDFDoc *doc = [[FSPDFDoc alloc] initWithPath:@"path/to/Sample.pdf"];
[doc load:nil];
// 使用内容生成器创建含页码的字符串
FSHeaderFooterContentGenerator *generator = [[FSHeaderFooterContentGenerator alloc] init];
[generator addString:@"第 "];
[generator addPageNumber:FSHeaderFooterContentGeneratorPageNumberFormatFirst];
[generator addString:@" 页"];
NSString *pageNumberStr = [generator generateContent];
// 使用内容生成器创建含日期的字符串
FSHeaderFooterContentGenerator *dateGen = [[FSHeaderFooterContentGenerator alloc] init];
[dateGen addDate:FSHeaderFooterContentGeneratorDateFormatTypeFirst];
NSString *dateStr = [dateGen generateContent];
// 配置内容:六个位置
FSHeaderFooterContent *content = [[FSHeaderFooterContent alloc] init];
content.header_left_content = @"福昕文档";
content.header_center_content = @"";
content.header_right_content = dateStr;
content.footer_left_content = @"";
content.footer_center_content = pageNumberStr;
content.footer_right_content = @"机密";
// 设置页面范围
FSPageNumberRange *pageRange = [[FSPageNumberRange alloc]
initWithStart_number:0
end_number:[doc getPageCount] - 1
filter:FSRangeFilterAll];
// 设置页面边距(left, bottom, right, top)
FSRectF *margin = [[FSRectF alloc] initWithLeft1:72 bottom1:36 right1:72 top1:36];
// 构造 FSHeaderFooter
FSHeaderFooter *headerFooter = [[FSHeaderFooter alloc] init];
headerFooter.font = [[FSFont alloc] initWithStandard_id:FSFontStdIDHelvetica];
headerFooter.text_size = 10.0f;
headerFooter.text_color = 0x000000;
headerFooter.page_range = pageRange;
headerFooter.page_margin = margin;
headerFooter.start_page_number = 1;
headerFooter.content = content;
headerFooter.is_to_embed_font = NO;
headerFooter.is_underline = NO;
// 添加到文档
[doc addHeaderFooter:headerFooter];
[doc saveAs:@"path/to/output.pdf" saveFlags:FSPDFDocSaveFlagNormal];
其他操作
objc
// 检查文档是否存在页眉页脚
BOOL hasHF = [doc hasHeaderFooter];
// 获取可编辑的页眉页脚
FSHeaderFooter *existingHF = [doc getEditableHeaderFooter];
// 更新已有的页眉页脚
[doc updateHeaderFooter:existingHF];
// 移除所有页眉页脚
[doc removeAllHeaderFooters];
API 参考
FSHeaderFooter、FSHeaderFooterContent、FSHeaderFooterContentGenerator 的完整接口说明请参阅 API 手册。