【问题标题】:iOS 8 Swift - Text appearing upside down when writing to PDFiOS 8 Swift - 写入 PDF 时出现上下颠倒的文本
【发布时间】:2015-03-02 06:46:05
【问题描述】:

我正在处理一个项目,该项目将采用现有的 PDF 模板,并且能够将文本写入文本(当前存储为用户默认值)到该模板,然后在 UIWebView 中显示带有注释的更新 PDF。

我已经为此创建了一个函数,并且到目前为止能够在我想要的位置显示带有注释的 PDF,但是文本出现颠倒(PDF 模板是正确的方向)。我知道在使用核心图形和绘制 PDF 时,上下文的 (0,0) 位置位于左下角,与左上角相对。然而,即使在尝试翻转 y 轴之后,我仍然在努力让文本正确显示。

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)

        }

我有一种感觉,我只是缺少与图形上下文设置方式有关的一些小东西。如果有人能指出我正确的方向,我将不胜感激?

谢谢

【问题讨论】:

    标签: ios pdf swift core-graphics core-text


    【解决方案1】:

    在 Swift 3 中

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

    【讨论】:

      【解决方案2】:

      您还需要转换文本空间。所以试试这个翻转坐标系:

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

      【讨论】:

      • 我已经添加到 CGContextSetTextMatrix 但不幸的是仍然没有运气。我在哪里翻转坐标系有问题吗?我目前正在尝试将文本绘制到上下文之前(但在我访问它之后)
      • 尝试在获得图形上下文的行之后立即移动这些行。也许这就是问题所在。
      • 谢谢!这看起来已经纠正了文本的问题。然而,它也改变了我试图绘制的 PDF 模板的方向。有没有办法独立改变它们?希望在添加到捆绑包之前避免旋转和翻转每个模板。谢谢
      猜你喜欢
      • 2021-12-02
      • 2019-06-04
      • 2019-08-27
      • 2018-10-31
      • 2018-06-21
      • 2012-03-10
      • 1970-01-01
      • 2016-05-29
      • 1970-01-01
      相关资源
      最近更新 更多