Stuart Pattison
Stuart Pattison

Reputation: 193

iOS 8 Swift - Text appearing upside down when writing to PDF

I am working on a project that will take an existing PDF template, and is able to write text to text (currently stored as user defaults) to that template, and then show the updated PDF with annotations in a UIWebView.

I've created a function for this and so far able to display the PDF with annotations in the location I would like, however the text is appearing upside down (the PDF template is the correct orientation). I understand that when using core graphics and drawing to a PDF, the (0,0) location of the context is in the bottom left opposed to top left. However even after attempting to flip the y axis, I am still struggling to have the text appear correctly.

else {
            //Set up the webView to show the pdf file

            //Create NSURL for the pdf document
            var localUrl: NSURL = NSBundle.mainBundle().URLForResource("Original PDF", withExtension: "pdf")!

            //references the PDF's location
            var pdfDocumentRef: CGPDFDocumentRef = CGPDFDocumentCreateWithURL(localUrl as CFURLRef)

            //establishing the height and width of the PDF file
            var paperSize = CGRectMake(0.0, 0.0, 595.276, 841.89)

            //create a tempory filename for this PDF and temporary path
            var path = NSTemporaryDirectory()
            var temporaryPdfFilePath = path.stringByAppendingPathComponent("temporaryPDF.pdf")

            //creation of a new graphical context, if no file is present at specified path, then a new path is created.
            var graphicsContext = UIGraphicsBeginPDFContextToFile(temporaryPdfFilePath, paperSize, nil)

            //starts a new PDF page that can have graphical context drawn, parameters specify size and location of new PDF, and any additional page information (currently nil)
            UIGraphicsBeginPDFPageWithInfo(paperSize, nil)

            //gets the current context
            var currentContext: CGContextRef = UIGraphicsGetCurrentContext()

            //access page 1 of the PDF
            var page: CGPDFPageRef = CGPDFDocumentGetPage(pdfDocumentRef, 1)

            //changes the origin of the coordinate system. parameters of current context, x coordinate (displaces by 0), y coordinate (displaces by paperSize height)
            CGContextTranslateCTM(currentContext, 0, paperSize.height)

            //changes the scale of the user coordinate system. parameters of current context, x axis (doesn't change factor), y axis (changes scale by -1 (to flip vertically))
            CGContextScaleCTM(currentContext, 1.0, -1.0)

            //draw to page 1 of the graphics context
            CGContextDrawPDFPage(currentContext, page)

            //Add some text to be displayed
            let font = UIFont(name: "Courier", size: 14)
            let text: String = userDefaults.stringForKey("name")!
            let rectText = CGRectMake(25, 345, 100, 100)
            let textStyle = NSMutableParagraphStyle.defaultParagraphStyle().mutableCopy() as NSMutableParagraphStyle
            let textColor = UIColor.blackColor()
            let textFontAttributes = [
                NSFontAttributeName : font!,
                NSForegroundColorAttributeName: textColor,
                NSParagraphStyleAttributeName: textStyle
            ]

            text.drawInRect(rectText, withAttributes: textFontAttributes)


            //closes the PDF Graphics context
            UIGraphicsEndPDFContext()

            //show in webView
            let testUrl:NSURL = NSURL(string: temporaryPdfFilePath)!
            let testUrlRequest: NSURLRequest = NSURLRequest(URL: testUrl)
            self.webView.loadRequest(testUrlRequest)

        }

I have a feeling that I am only missing something small relating to how the graphical context is set up. I would greatly appreciate it if someone is able to point me in the right direction on this?

Thanks

Upvotes: 1

Views: 2723

Answers (2)

Sebin Roy
Sebin Roy

Reputation: 844

In Swift 3

    currentContext?.textMatrix = .identity
    currentContext?.translateBy(x: 0, y: 100)
    currentContext?.scaleBy(x: 1, y: -1)

Upvotes: 0

zisoft
zisoft

Reputation: 23078

You need to transform the text space, too. So try this to flip the coordinate system:

CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity)
CGContextTranslateCTM(currentContext, 0, paperSize.height)
CGContextScaleCTM(currentContext, 1, -1)

Upvotes: 1

Related Questions