Swift 如何在PDF文件中保存水印并导出到桌面macOS Mojave

Swift 如何在PDF文件中保存水印并导出到桌面macOS Mojave,swift,cocoa,pdf,Swift,Cocoa,Pdf,我有一个在macOS Mojave上运行的应用程序,它运行得很好。我可以将pdf文件拖到PDFView上,并可以在pdf文件上放置一个按钮来标记水印。我得到了苹果WWDC为iOS编写的示例代码,并将其翻译为macOS。我的问题是如何将此pdf(包括水印)保存到桌面 我的代码: override func viewDidLoad() { super.viewDidLoad() pdfView?.acceptsDraggedFiles = true } func classForP

我有一个在macOS Mojave上运行的应用程序,它运行得很好。我可以将pdf文件拖到PDFView上,并可以在pdf文件上放置一个按钮来标记水印。我得到了苹果WWDC为iOS编写的示例代码,并将其翻译为macOS。我的问题是如何将此pdf(包括水印)保存到桌面

我的代码:

override func viewDidLoad() {
    super.viewDidLoad()
    pdfView?.acceptsDraggedFiles = true
}

func classForPage() -> AnyClass {
    return WatermarkPage.self
}

@IBAction func WaterMark(_ sender: NSButton) {
    if let document = PDFDocument(url:  (pdfView?.document?.documentURL)!){

    //Center document on gray background
    pdfView?.autoScales = true
    pdfView?.backgroundColor = NSColor.lightGray

    // 1. Set delegate
    document!.delegate = self   
    pdfView?.document = document

    let filename: String = "ExportPDF.pdf"
    let path =  NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask,
                                                true)[0];
    let writePath = URL(fileURLWithPath: path).appendingPathComponent(filename).path
    //pdfView?.document?.write(toFile: writePath)
    document?.write(toFile: writePath)
    print("Pfad: \(path)")
    }
}

class WatermarkPage: PDFPage {
    // 3. Override PDFPage custom draw
    /// - Tag: OverrideDraw
    override func draw(with box: PDFDisplayBox, to context: CGContext) {


        // Draw original content
        super.draw(with: box, to: context)

        // Draw rotated overlay string
        context.saveGState()

        let pageBounds = self.bounds(for: box)
        context.translateBy(x: 0.0, y: pageBounds.size.height)
        context.scaleBy(x: 1.0, y: -1.0)
        context.rotate(by: CGFloat.pi / 5.0)

        let string: NSString = "A P P R O V E D"

        let attributes = [NSAttributedString.Key.foregroundColor: NSColor(calibratedRed: 0.8, green: 0.5, blue: 0.5, alpha: 0.5),NSAttributedString.Key.font: NSFont.boldSystemFont(ofSize: 64.0)]
        string.draw(at: CGPoint(x: 300, y: 40), withAttributes: attributes)

        context.restoreGState()
        context.saveGState()

    }
}

如果要将其保存到桌面

您必须更改写入文件的路径

例如标准桌面/用户/用户名/桌面/

let fileManager = FileManager.default
let homeURL =  FileManager.default.urls(for: NSHomeDirectory, in: .userDomainMask).first! as NSURL
let writePath = homeURL.path + "Desktop" + <Filename>

document?.write(toFile: writePath)

如果要将其保存到桌面

您必须更改写入文件的路径

例如标准桌面/用户/用户名/桌面/

let fileManager = FileManager.default
let homeURL =  FileManager.default.urls(for: NSHomeDirectory, in: .userDomainMask).first! as NSURL
let writePath = homeURL.path + "Desktop" + <Filename>

document?.write(toFile: writePath)

伙计们,对不起,这完全是我的误解。完全错误的理由。我可以给文档添加水印,然后我可以保存它或打印它,水印就会出现。我不需要单独的保存功能。老兄,有时候你坐在水管上,我的头脑好得很。非常感谢您的大力帮助。

伙计们,对不起,这完全是我的误解。完全错误的理由。我可以给文档添加水印,然后我可以保存它或打印它,水印就会出现。我不需要单独的保存功能。老兄,有时候你坐在水管上,我的头脑好得很。非常感谢您的大力帮助。

1。使用NSDesktopDirectory而不是NSHomeDirectory。2.不要使用路径,请使用URL和appendingPathComponent:.1。使用NSDesktopDirectory而不是NSHomeDirectory。2.不要使用路径,请使用URL和appendingPathComponent:。