【问题标题】:macOS/iOS PDFKit: create document's outline rootmacOS/iOS PDFKit:创建文档的大纲根
【发布时间】: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 缺少根大纲时添加它?

【问题讨论】:

标签: objective-c ios-pdfkit ios-pdfkit-outlines


【解决方案1】:

通过查看Apple的参考herePDFDocument类提供了设置大纲根的方法。

所以解决方法是:

root = [pdfDoc outlineRoot];

if( ! root )
{
    root = [[PDFOutline alloc] init];
    [pdfDoc setOutlineRoot: root];
}

【讨论】:

  • 请使用现代 Objective-C。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-17
  • 1970-01-01
  • 2013-07-01
  • 1970-01-01
  • 2023-01-01
  • 2021-10-28
  • 1970-01-01
相关资源
最近更新 更多