在macOS上使用Swift绘制PDF [英] Draw on a PDF using Swift on macOS
问题描述
我的目标是在PDF上写文本,例如注释.
My goal is to write text on a PDF, like an annotation.
我实现了将PDFPage转换为NSImage的过程,我在NSImage上绘制了图像,然后保存了由图像形成的PDF.
I achieved it transforming the PDFPage to a NSImage, I drew on the NSImage then I saved the PDF formed by the images.
let image = NSImage(size: pageImage.size)
image.lockFocus()
let rect: NSRect = NSRect(x: 50, y: 50, width: 60, height: 20)
"Write it on the page!".draw(in: rect, withAttributes: someAttributes)
image.unlockFocus()
let out = PDFPage(image: image)
问题显然是out
(输出PDF的新页面)是图像的PDFPage,而不是常规页面.因此,输出的PDF很大,您不能在其上复制和粘贴任何内容.这只是一系列图像.
The problem is obviously that out
(the new page of the output PDF) is a PDFPage of images and not a regular one. So the output PDF is very big in size and you can't copy and paste anything on it. It's just a sequence of images.
我的问题是,是否有一种方法可以在不使用NSImage的情况下以编程方式在PDF页面上添加简单文本.有什么主意吗?
My question is if there's a way to add simple text on a PDF page programmatically without using NSImage. Any idea?
注意::iOS编程UIGraphicsBeginPDFPageWithInfo
中有一个此类,对我来说可能非常有帮助.但是我找不到用于macOS开发的类似类.
Note: There's this class in iOS programming UIGraphicsBeginPDFPageWithInfo
which could be very helpful in my case. But I can't find the similar class for macOS development.
推荐答案
您可以在macOS上创建PDF图形上下文,并在其中绘制PDFPage
.然后,您可以使用Core Graphics或AppKit图形将更多对象绘制到上下文中.
You can create a PDF graphics context on macOS and draw a PDFPage
into it. Then you can draw more objects into the context using either Core Graphics or AppKit graphics.
这是我通过打印您的问题创建的测试PDF:
Here's a test PDF I created by printing your question:
这是将页面绘制到PDF上下文中,然后在页面顶部绘制更多文本的结果:
And here's the result from drawing that page into a PDF context, then drawing more text on top of it:
这是我编写的将第一个PDF转换为第二个PDF的代码:
Here's the code I wrote to transform the first PDF into the second PDF:
import Cocoa
import Quartz
let inUrl: URL = URL(fileURLWithPath: "/Users/mayoff/Desktop/test.pdf")
let outUrl: CFURL = URL(fileURLWithPath: "/Users/mayoff/Desktop/testout.pdf") as CFURL
let doc: PDFDocument = PDFDocument(url: inUrl)!
let page: PDFPage = doc.page(at: 0)!
var mediaBox: CGRect = page.bounds(for: .mediaBox)
let gc = CGContext(outUrl, mediaBox: &mediaBox, nil)!
let nsgc = NSGraphicsContext(cgContext: gc, flipped: false)
NSGraphicsContext.current = nsgc
gc.beginPDFPage(nil); do {
page.draw(with: .mediaBox, to: gc)
let style = NSMutableParagraphStyle()
style.alignment = .center
let richText = NSAttributedString(string: "Hello, world!", attributes: [
NSFontAttributeName: NSFont.systemFont(ofSize: 64),
NSForegroundColorAttributeName: NSColor.red,
NSParagraphStyleAttributeName: style
])
let richTextBounds = richText.size()
let point = CGPoint(x: mediaBox.midX - richTextBounds.width / 2, y: mediaBox.midY - richTextBounds.height / 2)
gc.saveGState(); do {
gc.translateBy(x: point.x, y: point.y)
gc.rotate(by: .pi / 5)
richText.draw(at: .zero)
}; gc.restoreGState()
}; gc.endPDFPage()
NSGraphicsContext.current = nil
gc.closePDF()
这篇关于在macOS上使用Swift绘制PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!