3

我正在开发一个 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); 
}
4

1 回答 1

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];
}
于 2013-05-07T10:50:02.417 回答