【问题标题】:Save more than one pdf page保存多个 pdf 页面
【发布时间】:2016-09-23 14:45:35
【问题描述】:

我正在从 url 保存一个 pdf 文件,如果我保存一个页面,那么没有问题,但是当我保存整个 pdf 应用程序时崩溃而没有任何日志

我的代码

CGPDFDocumentRef document = CGPDFDocumentCreateWithURL((CFURLRef)url);
NSString *pass = [_JSON objectForKey:@"pass"];
CGPDFDocumentUnlockWithPassword(document, [pass UTF8String]);

int i = (int) CGPDFDocumentGetNumberOfPages(document);
NSLog(@"Number Of pages: %d",i);


//Create the pdf context
for (int j = 0; j<i; j++) {
    page = CGPDFDocumentGetPage(document, j+1); //Pages are numbered starting at 1
    pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
    mutableData = CFDataCreateMutable(NULL, j);
}

//NSLog(@"w:%2.2f, h:%2.2f",pageRect.size.width, pageRect.size.height);
CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData(mutableData);
CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, &pageRect, NULL);

if (CGPDFDocumentGetNumberOfPages(document) > 0)
{
    //Draw the page onto the new context
    //page = CGPDFDocumentGetPage(document, 1); //Pages are numbered starting at 1

    CGPDFContextBeginPage(pdfContext, NULL);
    CGContextDrawPDFPage(pdfContext, page);
    CGPDFContextEndPage(pdfContext);
}
else
{
    NSLog(@"Failed to create the document");
}

CGContextRelease(pdfContext); //Release before writing data to disk.

//Write to disk
[(__bridge NSData *)mutableData writeToFile:@"/Users/user/Desktop/sample.pdf" atomically:YES];

//CleanUP
CGDataConsumerRelease(dataConsumer);
CGPDFDocumentRelease(document);
CFRelease(mutableData);

我认为问题出在循环上,但对这个模块了解不多,我是第一次尝试这个。 Here is the output log which i could never understand and there is no log in console

【问题讨论】:

    标签: objective-c pdf


    【解决方案1】:

    我自己通过摆弄我的代码找到了答案 这是我的新工作编码

    CGPDFDocumentRef document = CGPDFDocumentCreateWithURL((CFURLRef)url);
    NSString *pass = [_JSON objectForKey:@"pass"];
    CGPDFDocumentUnlockWithPassword(document, [pass UTF8String]);
    
    int i = (int) CGPDFDocumentGetNumberOfPages(document);
    NSLog(@"Number Of pages: %d",i);
    
    //Create the pdf context
    CGPDFPageRef page = CGPDFDocumentGetPage(document, 1); //Pages are numbered starting at 1
    CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
    CFMutableDataRef mutableData = CFDataCreateMutable(NULL, 0);
    
    
    CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData(mutableData);
    CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, &pageRect, NULL);
    
    if (CGPDFDocumentGetNumberOfPages(document) > 0)
    {
        //Draw the page onto the new context
        //page = CGPDFDocumentGetPage(document, 1); //Pages are numbered starting at 1
    
        for (int j = 0; j < i; j++) {
        page = CGPDFDocumentGetPage(document, j+1);
        CGPDFContextBeginPage(pdfContext, NULL);
        CGContextDrawPDFPage(pdfContext, page);
        CGPDFContextEndPage(pdfContext);
        }
    }
    else
    {
        NSLog(@"Failed to create the document");
    }
    CGContextRelease(pdfContext); //Release before writing data to disk.
    
    //Write to disk
    
    NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    path = arr[0];
    path = [path stringByAppendingPathComponent:@"sample.pdf"];
    
    [(__bridge NSData *)mutableData writeToFile:path atomically:YES];
    
    
    //CleanUP
    CGDataConsumerRelease(dataConsumer);
    CGPDFDocumentRelease(document);
    CFRelease(mutableData);
    

    我只是在循环整个事情,因为我应该只循环我正在做页面创建部分的部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      • 2012-09-06
      • 1970-01-01
      • 2022-01-01
      • 2023-03-29
      相关资源
      最近更新 更多