在macOS上使用Swift绘制PDF

Mat*_*att 1 pdf macos swift

我的目标是在PDF上编写文本,就像注释一样.

我实现了将PDFPage转换为NSImage,我绘制了NSImage,然后保存了由图像形成的PDF.

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)
Run Code Online (Sandbox Code Playgroud)

问题显然是out(输出PDF的新页面)是图像的PDF页面而不是常规页面.因此输出PDF的大小非常大,您无法在其上复制和粘贴任何内容.这只是一系列图像.

我的问题是,如果有一种方法可以在不使用NSImage的情况下以编程方式在PDF页面上添加简单文本.任何的想法?

注意:在iOS编程中有这个类,UIGraphicsBeginPDFPageWithInfo在我的情况下可能非常有用.但我找不到类似的macOS开发类.

rob*_*off 7

您可以在macOS上创建PDF图形上下文并将其绘制PDFPage到其中.然后,您可以使用Core Graphics或AppKit图形将更多对象绘制到上下文中.

这是我通过打印您的问题创建的测试PDF: 输入PDF

这是将该页面绘制到PDF上下文中,然后在其上绘制更多文本的结果:

输出PDF

这是我编写的将第一个PDF转换为第二个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()
Run Code Online (Sandbox Code Playgroud)

  • 使用带有“CGDataConsumer”参数的“CGContext”初始值设定项。在“closePDF”之后,您可以从数据创建“PDFDocument”的新实例,而无需遍历文件。 (2认同)