下面的示例代码允许向现有PDFDocument pdfDoc
添加大纲(或 Acrobat 术语中的“书签”) ,标签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缺少根大纲时添加它?