2015-01-04 63 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)