首先,我不知道人们是否只是因为复杂性才建议使用昂贵的 PDF iOS 库,或者他们通过评论说如果没有事先测试就无法做到这一点来推广它们。
您可以在 pdf 文件中添加和保存注释,甚至可以将其导出。
NSURL *OriginalPdfPath = [[NSBundle mainBundle] URLForResource:@"yourPDFname" withExtension:@"pdf"];
NSString *string=[OriginalPdfPath path];
NSError *error;
//Load original pdf data:
NSData *pdfData = [NSData dataWithContentsOfFile:string options:NSDataReadingUncached error:&error];
if (error)
{
NSLog(@"%@", [error localizedDescription]);
return;
}
else {
NSLog(@"Data has loaded successfully.");
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pathLD = [documentsDirectory stringByAppendingPathComponent:@"dirName"]; // get directory
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:pathLD])
{
[fileManager createDirectoryAtPath:pathLD withIntermediateDirectories:YES attributes:nil error:nil];
}
pathLD = [pathLD stringByAppendingPathComponent:@"yourPDFname.pdf"]; // add file path
BOOL isDir;
if (![fileManager fileExistsAtPath:pathLD isDirectory:&isDir]) {
BOOL didsave=[fileManager createFileAtPath:pathLD contents:pdfData attributes:nil];
}
NSURL *url = [NSURL fileURLWithPath:string];
CGPDFDocumentRef document = CGPDFDocumentCreateWithURL ((__bridge_retained CFURLRef) url);
size_t count = CGPDFDocumentGetNumberOfPages(document);
if (count == 0)
{
NSLog(@"PDF needs at least one page");
return;
}
// 现在使用 pdf 文件
UIGraphicsBeginPDFContextToFile(pathLD , CGRectZero, nil);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGPDFPageRef page = CGPDFDocumentGetPage (document, 1);
CGRect paperSize = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox);
UIGraphicsBeginPDFContextToFile(pathLD , CGRectZero, nil);
UIGraphicsBeginPDFPageWithInfo(paperSize, nil);
// 翻转上下文,使 PDF 页面正面朝上呈现。
CGContextTranslateCTM(currentContext, 0.0, paperSize.size.height);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextDrawPDFPage (currentContext, page);
// 这里的魔术函数:在上下文中渲染注释视图的层
[drawingView.layer renderInContext:currentContext];
UIGraphicsEndPDFContext();
CGPDFDocumentRelease (document);
这会将您在当前 pdf 上下文中呈现的所有内容保存回文件管理器
,您可以使用 WebView 上的模型视图显示 VC 对其进行测试:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self displayPdfinWebView];
}
-(void)displayPdfinWebView {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory =[paths objectAtIndex:0];
NSString *pathLD = [documentsDirectory stringByAppendingPathComponent:@"dirName"]; // get directory
pathLD = [pathLD stringByAppendingPathComponent:@"yourPDFname.pdf"]; // add file path
NSFileManager *fileManager = [NSFileManager defaultManager];
NSData *data;
NSURL* pdfUrl;
if ([fileManager fileExistsAtPath:pathLD])
{
pdfUrl = [NSURL fileURLWithPath:pathLD];
[webView loadRequest:[NSURLRequest requestWithURL:pdfUrl]];
}
}
我想您可以使用 UIDocumentInteractionController 连接到 iBooks 以进行导出。我的参考资料是ApplePDF Drawing 。