2

Hi Friends I am Trying to create annotation of PDF page as something like bellow image in my app and this annotation is visible to all other PDF reader

For example:-

I create PDF document in My app and Add Annotation then i send this Document to My Friend Mail. Then my Friend Download this PDF and Open in to Preview App of Mac then this created Annotation of PDF that should be display.`

I doing RND of This stuff and Also Read iOS provide Doc about Quartz 2D. I successful read and create PDF using this Quartz 2D but now not getting any way to doing Annotation stuff.

I also study those kind of SO questions:-

Create PDF Annotations in iOS

Create PDF Annotations with Quartz (iOS)

pdf annotations in objective-c

I also done Annotation withing the App Reading PDF and store Point of x and y potion of Layer drage in to particular PDF page. Now issue is that what about when i send this PDF document to other user who open this Document in others PDF reader?

I also Discuss this SO chat Group. one of Stack-overflow User suggest me to Using CGLayer for doing this task But i am much Confused how to achieve this stuff and how to implement this.

Please Help me and Guide Me on this stuff.

enter image description here

Community
  • 1
  • 1
kirti Chavda
  • 3,029
  • 2
  • 17
  • 29
  • Which of the referenced methods did you try to add an annotation? Did you check if the annotation is visible in other readers? – Martin R Aug 17 '13 at 08:06
  • we try to custom annotation such that store Point of x and y position of button in to particular PDF page it is no visible to other reader – kirti Chavda Aug 17 '13 at 08:10
  • How do you create the annotation? Showing your code would help. – Martin R Aug 17 '13 at 08:13
  • that the prob sir within the app i drag button an any potion and store the x n y position with page Number in DB but now my main issue is that how to add anotion into pdf page that read from all PDF reader..? – kirti Chavda Aug 17 '13 at 08:17
  • My point is: Show us what **you tried**. - Annotations are a general PDF feature, so whether they are visible or not should only depend on the features of the reader, not how you created them. - If you don't show how you create the annotation then it is difficult to help. – Martin R Aug 17 '13 at 08:24

2 Answers2

0

The answer is simply no, you cannot create standard PDF annotations using the iOS APIs. The CGPDF* API is read-only and it can be used only to read PDF file and the CoreGraphics API with PDF context can only create simple PDF pages without annotations, form fields, bookmarks, etc.
If you create new PDF files you can switch to libHaru which supports annotations but if want to add annotations to existing PDF files you're out of luck (libHaru cannot load PDF files). There is also IFXPDFFactory but it is under development and I do not know if it supports annotations yet.

Mihai Iancu
  • 1,818
  • 2
  • 11
  • 10
0

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 .

Vacca
  • 1,219
  • 16
  • 26