iPad App: Merge PDF files into 1 PDF document / Create PDF Document of multi-page scrollview

陌路散爱 提交于 2019-12-03 14:15:40

问题


I am writing an iPad application which uses a scrollview with page control. I need to create a PDF of all the pages as 1 PDF file. So far, I figured that I should loop through all the sub-views (pages) and create PDF files for each (using CGPDFContext). BUT I do need to combine all the files into 1 PDF document. Can you help me to do so??

OR if you have a better way to create a PDF document with multiple pages from this scrollview, that would even be better!!

Please help. I've searched everywhere and saw that Mac OS has something using PDFDocument, insertPage function. I can't find a similar method for iOS??


回答1:


to create a multi-part 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);
}

Hope this helps.



来源:https://stackoverflow.com/questions/6028322/ipad-app-merge-pdf-files-into-1-pdf-document-create-pdf-document-of-multi-pag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!