2015-01-04 39 views
1

我正在研究一个项目,该项目将采用现有的PDF模板,并且能够将文本写入文本(当前存储为用户默认值)到该模板,然后在UIWebView中显示带有注释的更新的PDF。iOS 8 Swift - 写入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) 

     } 

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

谢谢

回答

1

您还需要转换文本空间。因此,尝试这种翻转坐标系:

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

我在CGContextSetTextMatrix添加,但仍没有运气很遗憾。我在哪里翻转坐标系是否存在问题?我目前正在尝试将文本绘制到上下文之前(但在访问它之后) –

+0

尝试在获取图形上下文的行后立即移动这些行。也许这就是问题。 – zisoft

+0

谢谢!这看起来已经纠正了与文本有关的问题。但是它也改变了我试图绘制的PDF模板的方向。有没有办法独立改变它们?希望避免在添加到包之前旋转和翻转每个模板。谢谢 –

0

在斯威夫特3

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