1

下面的示例代码允许向现有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缺少根大纲时添加它?

4

1 回答 1

1

通过查看 Apple 在此处 PDFDocument的参考,该类提供了一种设置大纲根的方法。

所以解决方法是:

root = [pdfDoc outlineRoot];

if( ! root )
{
    root = [[PDFOutline alloc] init];
    [pdfDoc setOutlineRoot: root];
}
于 2018-10-02T16:55:01.460 回答