title: Draw on a PDF using Swift on macOS
body: <p>My goal is to write text on a PDF, like an annotation.</p>
  
  <p>I achieved it transforming the PDFPage to a NSImage, I drew on the NSImage then I saved the PDF formed by the images. </p>
  
  <pre><code>let image = NSImage(size: pageImage.size)        
  image.lockFocus()
  
  let rect: NSRect = NSRect(x: 50, y: 50, width: 60, height: 20)
  "Write it on the page!".draw(in: rect, withAttributes: someAttributes)
  
  image.unlockFocus()
  
  let out = PDFPage(image: image)
  </code></pre>
  
  <p>The problem is obviously that <code>out</code> (the new page of the output PDF) is a PDFPage of images and not a regular one. So the output PDF is very big in size and you can't copy and paste anything on it. It's just a sequence of images. </p>
  
  <p>My question is if there's a way to add simple text on a PDF page programmatically without using NSImage. Any idea?</p>
  
  <p><strong>Note:</strong> There's this class in iOS programming <code>UIGraphicsBeginPDFPageWithInfo</code> which could be very helpful in my case. But I can't find the similar class for macOS development.</p>