在IOS 5中将注释和Pdf保存到新的Pdf文件

时间:2013-05-06 10:49:14

标签: iphone ios objective-c ipad ios5

我正在开发一个pdf注释应用程序,我需要保存注释,所以当我试图将注释和pdf内容保存到新的pdf时,注释会被保存但是pdf的内容不是吗?这是我的功能:

-(void)save_doc_as_pdf:(ReaderMainToolbar *)toolbar BackButton:(UIButton *)button
{
    NSError *error;
    NSString *PdfPath = [document.fileURL absoluteString];
    NSString *newPath = [PdfPath stringByReplacingOccurrencesOfString:@"file://localhost" withString:@""];

    NSString *NewPdfPath = [thenewPath stringByReplacingOccurrencesOfString:@".pdf" withString:@"_1.pdf"];
    NSData *pdfData = [NSData dataWithContentsOfFile:[document.fileURL path] options:NSDataReadingUncached error:&error];
    if (pdfData == nil) {
        NSLog(@"data is nil !!!!");

    }

    if (error)
    {
        NSLog(@"%@", [error localizedDescription]);
        return;
    }
    else
    NSLog(@"Data has loaded successfully.");
    //If fails to create the new file, returns
    if (![[NSFileManager defaultManager] createFileAtPath:NewPdfPath contents:pdfData attributes:nil])
    {
        return;
    }

    NSURL *url = [NSURL fileURLWithPath:newPath];
    CGPDFDocumentRef pdf_document = CGPDFDocumentCreateWithURL ((__bridge_retained CFURLRef) url);
    size_t count = CGPDFDocumentGetNumberOfPages(pdf_document);

    if (count == 0)
    {
        NSLog(@"PDF needs at least one page");
        return;
    }

    CGRect paperSize = CGRectMake(0, 0,842, 1190);

    UIGraphicsBeginPDFContextToFile(NewPdfPath , paperSize, nil);
    // CGPDFPageRef page = CGPDFDocumentGetPage(document, 1);
    UIGraphicsBeginPDFPageWithInfo(paperSize, nil);
    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    // flip context so page is right way (landscape app)
    CGContextScaleCTM(currentContext, 1, -1);
    // Rotate the coordinate system (rotation = M_PI or -M_PI for landscape)
    //CGContextRotateCTM(currentContext, rotation/ 2);

    CGPDFPageRef page = CGPDFDocumentGetPage (pdf_document, 1); // grab page 1 of the PDF
    CGContextDrawPDFPage (currentContext, page); // draw page 1 into graphics context

    //flip context so annotations are right way
    CGContextScaleCTM(currentContext, 1, -1);
    //CGContextRotateCTM(currentContext, rotation / 2);
    //Render the layer of the annotations view in the context
    [startdraw.layer renderInContext:currentContext];
    UIGraphicsEndPDFContext();
    CGPDFDocumentRelease (pdf_document); 
}

1 个答案:

答案 0 :(得分:1)

你好我找到了一个解决方案,用于在pdf文档上保存注释,我想与你分享,所以这里是代码:

-(void)save_the_pdf:(ReaderMainToolbar *)toolbar BackButton:(UIButton *)button{
    NSMutableArray *URLsTableObject=[[NSMutableArray alloc] init];
    NSMutableArray *URLsTable=[[NSMutableArray alloc] init];
    NSUserDefaults *DataStoreUrls;
    NSString *OriginalPdfName = [document.fileName stringByReplacingOccurrencesOfString:@".pdf" withString:@""];


    NSString *OriginalPdfPath = [document.fileName stringByReplacingOccurrencesOfString:@".pdf" withString:@""];
    NSString* thenewPath = [[NSBundle mainBundle] pathForResource:OriginalPdfPath ofType:@"pdf"];
    NSString *NewPdfPath = [thenewPath stringByReplacingOccurrencesOfString:@".pdf" withString:@"_1.pdf"];

    CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((__bridge CFURLRef)[[NSBundle mainBundle] URLForResource:OriginalPdfName withExtension:@"pdf"]);

    const size_t numberOfPages = CGPDFDocumentGetNumberOfPages(pdf);

    NSMutableData* data = [NSMutableData data];
    UIGraphicsBeginPDFContextToData(data, CGRectZero, nil);

    for(size_t page = 1; page <= numberOfPages; page++)
    {


        //  Get the current page and page frame
        CGPDFPageRef pdfPage = CGPDFDocumentGetPage(pdf, page);
        const CGRect pageFrame = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox);

        UIGraphicsBeginPDFPageWithInfo(pageFrame, nil);

        //  Draw the page (flipped)
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextSaveGState(ctx);
        CGContextScaleCTM(ctx, 1, -1);
        CGContextTranslateCTM(ctx, 0, -pageFrame.size.height);
        CGContextDrawPDFPage(ctx, pdfPage);
        CGContextRestoreGState(ctx);
        if (page == currentPage) {
        [startdraw.layer renderInContext:ctx];
        }
    }

    UIGraphicsEndPDFContext();

    CGPDFDocumentRelease(pdf);
    //pdf = nil;
    if (![[NSFileManager defaultManager] createFileAtPath:NewPdfPath contents:data attributes:nil])
    {
        return;
    }

    //  Do something with the 'data'...

    DataStoreUrls=[NSUserDefaults standardUserDefaults];

    if([DataStoreUrls objectForKey:@"ALLURLS"]!=NULL){
        URLsTableObject=[DataStoreUrls objectForKey:@"ALLURLS"];
        int index=[URLsTableObject count];
        URLsTable=[[NSMutableArray alloc] initWithArray:URLsTableObject];
        [URLsTable insertObject:NewPdfPath atIndex:index];
        [DataStoreUrls setObject:URLsTable forKey:@"ALLURLS"];
    }else{
        [URLsTable addObject:NewPdfPath];
        [DataStoreUrls setObject:URLsTable forKey:@"ALLURLS"];
    }
    [DataStoreUrls synchronize];
}