First I don't know is it only because of complexity that people suggest the costly PDF iOS libraries out there or they are promoting them by commenting that this and that cannot be done without prior testing.
You can add and save annotations in the pdf file and even export it.
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;
}
// Now work with the pdf File
UIGraphicsBeginPDFContextToFile(pathLD , CGRectZero, nil);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGPDFPageRef page = CGPDFDocumentGetPage (document, 1);
CGRect paperSize = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox);
UIGraphicsBeginPDFContextToFile(pathLD , CGRectZero, nil);
UIGraphicsBeginPDFPageWithInfo(paperSize, nil);
// Flip the context so that the PDF page is rendered right side up.
CGContextTranslateCTM(currentContext, 0.0, paperSize.size.height);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextDrawPDFPage (currentContext, page);
// Magic function here : Render the layer of the annotations view in the context
[drawingView.layer renderInContext:currentContext];
UIGraphicsEndPDFContext();
CGPDFDocumentRelease (document);
This saves all that you render in current pdf context back to the File Manager
you can test that with a model View display VC on a WebView:
- (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]];
}
}
I guess you can connect to iBooks with UIDocumentInteractionController for exporting.
My references is ApplePDF Drawing .