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



我正在制作一个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); 
}

你好,我找到了一个在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];
}

相关内容

最新更新