Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So in the title you say that you want to add an annotation to a pdf, but then the example that you are trying to make work in the body of your question is simply adding text to the pdf. These are <strong>very</strong> different things....</p> <p>Here is a "Hello World" which adds text to an existing pdf (similar to your attempt):</p> <p>I have not tested this but it is based on existing code (where I was drawing using a CTLine instead of CGContextShowTextAtPoint) so it should be very close. Error checking has been removed for clarity.</p> <pre><code>/* * This function copies the first page of a source pdf into the destination pdf * and then adds a line of text to the destination pdf. * * This must be modified by putting the correct path into the NSURL lines * for sourceURL and destURL before it will work. * * This code is using ARC and has error checking removed for clarity. */ - (void)helloWorldPDF { // Open the source pdf NSURL *sourceURL = [NSURL fileURLWithPath:@"path to original pdf"]; CGPDFDocumentRef sourceDoc = CGPDFDocumentCreateWithURL((__bridge CFURLRef)sourceURL); // Create the new destination pdf &amp; set the font NSURL *destURL = [NSURL fileURLWithPath:@"path to new pdf"]; CGContextRef destPDFContext = CGPDFContextCreateWithURL((__bridge CFURLRef)destURL, NULL, NULL); CGContextSelectFont(destPDFContext, "CourierNewPS-BoldMT", 12.0, kCGEncodingFontSpecific); // Copy the first page of the source pdf into the destination pdf CGPDFPageRef pdfPage = CGPDFDocumentGetPage(sourceDoc, 1); CGRect pdfCropBoxRect = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox); CGContextBeginPage (destPDFContext, &amp;pdfCropBoxRect); CGContextDrawPDFPage(destPDFContext, pdfPage); // Close the source file CGPDFDocumentRelease(sourceDoc); // Draw the text const char *text = "second line!"; CGContextShowTextAtPoint(destPDFContext, 10.0, 30.0, text, strlen(text)); // Close the destination file CGContextRelease(destPDFContext); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload