Drawing to a PDF context


Sandor Szatmari
 

Jim,

On Mar 4, 2020, at 11:56 AM, Jim <jimcfl@...> wrote:


On Mar 4, 2020, at 11:06 AM, Quincey Morris <quinceymorris@...> wrote:

On Mar 4, 2020, at 06:52 , Sandor Szatmari <admin.szatmari.net@...> wrote:

I need to replace calls to CGShowTextAtPoint() with calls to -drawAtPoint:withAttributes:

I made this change and drawing fails… :/
i.e. no text rendered, no exception, logging of errors, etc. simply no text
Well, by default, NWView coordinates are flipped vs. CGContext coordinates. I suggest you try temporarily forcing the point you’re drawing at to be the center of the context or view, and see if the text even appears. If so, you should be able to see what you need to do to transform the point at which you’re drawing (based on which direction the text is running, and whether it’s upside down).

Are you switching over to PDFKit? In that case, you’ll have to respect whatever coordinate system it uses. It looks like there’s some advice on that subject in the “Override the Draw Method and Add Your Custom Graphic” section of https://developer.apple.com/documentation/pdfkit/adding_custom_graphics_to_a_pdf.
When I wrote an app that added things to an existing PDF a couple years ago, I was able to use text and custom annotations to display elements on a document in a PDFView, but was unable to figure out how to use PDFKit to save the annotations into the document so that they would be visible with Preview or Acrobat. It appears that PDFKit is primarily oriented around on-screen display and markup of a PDF, not creating or modifying a PDF.

So to save the changes I had to redraw the existing document in a CGPDFContext and then draw the elements as images in that context as well. This turns each PDF page into an image, which wasn’t a problem in my case, but is usually not desirable.
Yea, we are printing and distributing the PDF so it’s important to retain the vector nature of the PDF. People need to be able to zoom in to read the finer details.

Thanks!

Jim Crate




Sandor Szatmari
 

Quincey,


On Mar 4, 2020, at 11:07 AM, Quincey Morris <quinceymorris@...> wrote:

On Mar 4, 2020, at 06:52 , Sandor Szatmari <admin.szatmari.net@...> wrote:

I need to replace calls to CGShowTextAtPoint() with calls to -drawAtPoint:withAttributes:

I made this change and drawing fails… :/ 
i.e. no text rendered, no exception, logging of errors, etc. simply no text

Well, by default, NWView coordinates are flipped vs. CGContext coordinates. I suggest you try temporarily forcing the point you’re drawing at to be the center of the context or view, and see if the text even appears. If so, you should be able to see what you need to do to transform the point at which you’re drawing (based on which direction the text is running, and whether it’s upside down).

I tried a couple tweaks of the drawing coordinates but I should investigate more.  

Also interesting. If I call [NSGraphics currentContext] before trying to set the context it returns nil.


Are you switching over to PDFKit? In that case, you’ll have to respect whatever coordinate system it uses.

Not switching to PDFKit.  Just updating deployment target SDK and dealing with deprecations.

Thank you!

It looks like there’s some advice on that subject in the “Override the Draw Method and Add Your Custom Graphic” section of https://developer.apple.com/documentation/pdfkit/adding_custom_graphics_to_a_pdf.


Jim
 

On Mar 4, 2020, at 11:06 AM, Quincey Morris <quinceymorris@...> wrote:

On Mar 4, 2020, at 06:52 , Sandor Szatmari <admin.szatmari.net@...> wrote:

I need to replace calls to CGShowTextAtPoint() with calls to -drawAtPoint:withAttributes:

I made this change and drawing fails… :/
i.e. no text rendered, no exception, logging of errors, etc. simply no text
Well, by default, NWView coordinates are flipped vs. CGContext coordinates. I suggest you try temporarily forcing the point you’re drawing at to be the center of the context or view, and see if the text even appears. If so, you should be able to see what you need to do to transform the point at which you’re drawing (based on which direction the text is running, and whether it’s upside down).

Are you switching over to PDFKit? In that case, you’ll have to respect whatever coordinate system it uses. It looks like there’s some advice on that subject in the “Override the Draw Method and Add Your Custom Graphic” section of https://developer.apple.com/documentation/pdfkit/adding_custom_graphics_to_a_pdf.
When I wrote an app that added things to an existing PDF a couple years ago, I was able to use text and custom annotations to display elements on a document in a PDFView, but was unable to figure out how to use PDFKit to save the annotations into the document so that they would be visible with Preview or Acrobat. It appears that PDFKit is primarily oriented around on-screen display and markup of a PDF, not creating or modifying a PDF.

So to save the changes I had to redraw the existing document in a CGPDFContext and then draw the elements as images in that context as well. This turns each PDF page into an image, which wasn’t a problem in my case, but is usually not desirable.

Jim Crate


Quincey Morris
 

On Mar 4, 2020, at 06:52 , Sandor Szatmari <admin.szatmari.net@...> wrote:

I need to replace calls to CGShowTextAtPoint() with calls to -drawAtPoint:withAttributes:

I made this change and drawing fails… :/ 
i.e. no text rendered, no exception, logging of errors, etc. simply no text

Well, by default, NWView coordinates are flipped vs. CGContext coordinates. I suggest you try temporarily forcing the point you’re drawing at to be the center of the context or view, and see if the text even appears. If so, you should be able to see what you need to do to transform the point at which you’re drawing (based on which direction the text is running, and whether it’s upside down).

Are you switching over to PDFKit? In that case, you’ll have to respect whatever coordinate system it uses. It looks like there’s some advice on that subject in the “Override the Draw Method and Add Your Custom Graphic” section of https://developer.apple.com/documentation/pdfkit/adding_custom_graphics_to_a_pdf.


Sandor Szatmari
 

I need to replace calls to CGShowTextAtPoint() with calls to -drawAtPoint:withAttributes:

I made this change and drawing fails… :/
i.e. no text rendered, no exception, logging of errors, etc. simply no text
The CG implementation of this code has been working with no leaks, crashes, etc for at least 10+ years.

Original context is created as follows:
CGContextRef ctx;
ctx = CGPDFContextCreateWithURL(…)

I tried setting the current NS graphics context like so

[NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithCGContext:ctx flipped:NO]

… but this causes the app to crash.

Any thoughts? Is this an ownership issue? Should I be letting the NSGraphics side of things know not to dispose of the context…

Thanks in advance,
Sandor