我正在开发一个项目,该项目将采用现有的 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)
}
我觉得我只是遗漏了一些与如何设置图形上下文有关的小东西。如果有人能够为此指出正确的方向,我将不胜感激?
谢谢
最佳答案
您还需要转换文本空间。所以试试这个来翻转坐标系:
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity)
CGContextTranslateCTM(currentContext, 0, paperSize.height)
CGContextScaleCTM(currentContext, 1, -1)
关于iOS 8 Swift - 写入 PDF 时文本倒置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27767521/