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