PDFAnnotation draws text upside down

Hi,


I have the following class handling custom textual pdf annotations in my app:


class PDFSheetTextAnnotation: PDFAnnotation {
    
    override func draw(with box: PDFDisplayBox, in context: CGContext) {

        let text = value(forAnnotationKey: .contents) as? NSString
        UIGraphicsPushContext(context)
        context.saveGState()

        var attributes = [
            NSAttributedStringKey.font: font as Any,
            NSAttributedStringKey.foregroundColor: fontColor as Any,
            ] as [NSAttributedStringKey : Any]
        if value(forAnnotationKey: .color) != nil {
            attributes[NSAttributedStringKey.backgroundColor] = color
        }
        text?.draw(in: bounds, withAttributes: attributes)

        context.restoreGState()
        UIGraphicsPopContext()

    }
}


Unfortunately text is drawn upside down. I tried by flipping the context with:


        context.textMatrix = .identity
        context.scaleBy(x: 1.0, y: -1.0)
        context.translateBy(x: 0, y: bounds.height)


but this seems to push my text out of the view. Looking at the original context transformation via the ctm property gives me the following result:


(lldb) po context.ctm
▿ CGAffineTransform
  - a : 2.33217189314749
  - b : 0.0
  - c : 0.0
  - d : 2.33217189314749
  - tx : -128.766743135888
  - ty : -1661.09549454122


I just can't figure out how to transform the context to get rid of the upside down flipping... any hints?


Thank you so much in advance and happy WWDC!

Arno

Replies

I faced the same issue and I struggle to flip the context, found this resource https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaDrawingGuide/Transforms/Transforms.html

https://developer.apple.com/documentation/pdfkit/custom_graphics


This will help me understand about co-ordiante systems after that I add these two line to your code and worked as expected.

context.translateBy(x: 0.0, y: bounds.height)
context.scaleBy(x: 1.0, y: -1.0)


following is the Custom PDFAnnotation class which I am using.

class TextAnnotation: PDFAnnotation {
   
    var text:String!
   
    init(with text: String!, forBounds bounds: CGRect, withProperties properties: [AnyHashable : Any]?) {
        super.init(bounds: bounds, forType: PDFAnnotationSubtype.text, withProperties: properties)
        self.text = text
    }
   
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   
    override func draw(with box: PDFDisplayBox, in context: CGContext) {
       
        UIGraphicsPushContext(context)
        context.saveGState()
        // context flipping
        context.translateBy(x: 0.0, y: bounds.height)
        context.scaleBy(x: 1.0, y: -1.0)
        // text drawing
        attributedText().draw(in: bounds)
        context.restoreGState()
        UIGraphicsPopContext()
       
    }
    func attributedText() -> NSAttributedString {
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = .center
       
        let attributes = [
            NSAttributedStringKey.paragraphStyle: paragraphStyle,
            NSAttributedStringKey.font: UIFont.systemFont(ofSize: 32.0),
            NSAttributedStringKey.foregroundColor: UIColor.red
        ]
        return  NSAttributedString(string: text, attributes: attributes)
       
    }
   
}


Thanks

I have the similar issue, but after I add the code

CGContextTranslateCTM(context, 0.0, self.bounds.size.height);

CGContextScaleCTM(context, 1.0, -1.0);


The text will disappear, do you know why? Thanks!