【发布时间】:2019-03-07 20:03:04
【问题描述】:
以下示例代码允许将大纲(或 Acrobat 术语中的“书签”)添加到现有 PDFDocument pdfDoc 并带有标签 Page n 指向页码 n,其中 n 是传递的参数 pageNum。
void insertOutline( PDFDocument *pdfDoc, NSUInteger pageNum )
{
PDFOutline *otl,
*root;
NSString *label = [NSString stringWithFormat:@"Page %lu", (unsigned long)pageNum + 1];
PDFDestination *destination;
PDFAction *action;
NSPoint point = {FLT_MAX, FLT_MAX};
PDFPage *page;
// Build the outline
page = [pdfDoc pageAtIndex: pageNum];
destination = [[PDFDestination alloc] initWithPage:page atPoint:point];
action = [[PDFActionGoTo alloc] initWithDestination: destination];
root = [pdfDoc outlineRoot];
otl = [[PDFOutline alloc] init];
[otl setLabel: label];
[otl setAction: action];
// Insert the outline
[root insertChild: otl atIndex: pageNum];
// Release resources
[otl release];
[action release];
[destination release];
}
创建的大纲将作为子级添加到文档大纲层次结构树顶部的根大纲。
尝试向尚不包含任何大纲的 PDF 添加大纲时出现问题。
在这种情况下,root = [pdfDoc outlineRoot]; 将导致 root 设置为 NULL 并且后面的代码显然会失败。
如果我用 Acrobat Pro 打开源文档并手动添加一个大纲/书签,那么代码就可以工作。
问题是:如何在 PDFDocument 缺少根大纲时添加它?
【问题讨论】:
-
@Willeke 尽管解决方案相同,但我的问题更具体,因此不同 - 我认为问题和答案都可能对其他用户有用
标签: objective-c ios-pdfkit ios-pdfkit-outlines