创建分页的完整PDF

时间:2016-08-16 22:38:50

标签: swift macos cocoa pdf pagination

我正在尝试创建用户输入到我的OS X Cocoa应用程序(swift 3)的文本字段中的所有文本的PDF。不幸的是,我遇到了一些问题。

以下是我正在使用的代码:

do {

    // set rect to the text view bounds
    let rect: NSRect = self.textview.bounds

    // search for a suitable location to save file
    if let dir = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true).first {

        // if we find that spot, add the file name and prepare to write it to there.
        let path = NSURL(fileURLWithPath: dir).appendingPathComponent("File.pdf")

        // Write that PDF to that path.
        try self.view.dataWithPDF(inside: rect).write(to: path!)

     } else {
     // this never happens
     print("uh-uh.")
     }
} catch _ {
    // this never happens either
    print("something went wrong.")
}

这成功保存了PDF,但失败的是PDF的外观。 PDF是一个页面(如何将其分页到字母大小?),只有向用户显示的NSTextView部分才会成为PDF的一部分,而不是整个文本视图(需要发生)。

以下是生成的PDF的样子:PDF issues

我希望保存的PDF具有文本视图的完整内容,而不仅仅是其中的一部分,我需要将其分页。我怎样才能做到这一点?

编辑1:我找到了this article,但它都是Objective-C(也非常老),所以没那么有帮助。

编辑2: Willeke的评论帮助我将整个文本视图转换为PDF,但它仍然只有一页。如何对我创建的PDF进行分页?

0 个答案:

没有答案
相关问题