代码之家  ›  专栏  ›  技术社区  ›  Paolo

macOS/iOS PDFKit:创建文档的大纲根目录

  •  0
  • Paolo  · 技术社区  · 8 年前

    下面的示例代码允许将大纲(或Acrobat术语中的“书签”)添加到现有的 PDF文档 pdfDoc 带标签 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 接下来的代码显然失败了。

    问题是: PDF文档

    1 回复  |  直到 8 年前
        1
  •  1
  •   Paolo    8 年前

    通过查看苹果的参考资料 here PDFDocument 类提供了设置大纲根目录的方法。

    root = [pdfDoc outlineRoot];
    
    if( ! root )
    {
        root = [[PDFOutline alloc] init];
        [pdfDoc setOutlineRoot: root];
    }
    
    推荐文章