iPad App:将PDF文件合并为1个PDF文档/创建多页滚动视图的PDF文档

时间:2011-05-17 08:41:53

标签: objective-c pdf-generation scrollview multipage

我正在编写一个iPad应用程序,它使用带有页面控制的scrollview。 我需要将所有页面的PDF创建为1个PDF文件。 到目前为止,我认为我应该遍历所有子视图(页面)并为每个子视图创建PDF文件(使用CGPDFContext)。但我确实需要将所有文件合并为1个PDF文档。你能帮我这么做吗?

或者,如果您有更好的方法来创建包含此滚动视图中多个页面的PDF文档,那就更好了!

请帮忙。我到处搜索,看到Mac OS有一些使用PDFDocument,insertPage函数。我找不到类似于iOS的方法??

1 个答案:

答案 0 :(得分:2)

创建多部分PDF:

-(CGContextRef) createPDFContext:(CGRect)inMediaBox path:(NSString *) path
{
    CGContextRef myOutContext = NULL;
    NSURL * url;

    url = [NSURL fileURLWithPath:path];
    if (url != NULL) {
        myOutContext = CGPDFContextCreateWithURL (url,// 2
                                                  &inMediaBox,
                                                  NULL);
    }
    return myOutContext;// 4
}

-(void)createPdfFromScrollview:(UIScrollView *)scrollview
{

    CGContextRef pdfContext = [self createPDFContext:CGRectMake(0, 0, WIDTH, HEIGHT) path:outputFilePath];

    for(UIView * view in scrollview.subviews)
    {
        CGContextBeginPage (pdfContext,nil);
        CGAffineTransform transform = CGAffineTransformIdentity;
        transform = CGAffineTransformMakeTranslation(0, HEIGHT);
        transform = CGAffineTransformScale(transform, 1.0, -1.0);
        CGContextConcatCTM(pdfContext, transform);            
        //Draw view into PDF
        [view.layer renderInContext:pdfContext];

        CGContextEndPage (pdfContext);         
    }

    CGContextRelease (pdfContext);
}

希望这有帮助。