在 Swift 5 Xcode 中使用 CoreGraphics 为 PDF 自动化 beginPage()

问题描述

在我的应用程序中,我想将大的、段落长度的字符串打印到 PDF 上(就像您在 Microsoft Word 中写了一篇大论文,然后将其打印为 PDF 一样)。我正在努力弄清楚如何让它知道何时调用 beginPage() 并流到新页面(在页面下方的一定距离处)?例如,想象一下打开一个 Word 文档并键入直到第一页完全填满,然后注意它如何在必要时自动转到新页面。我想 Xcode 需要知道在哪里打印(pageBottom + 底部一定数量的像素,然后它需要剪切仍然需要打印的字符串的其余部分,创建一个页面,知道从哪里开始打印,然后在第二页上继续打印它停止的地方。

本质上:

//1
let paragraphFont = UIFont.systemFont(ofSize: 12.0,weight: .regular)
//2
let paragraphAttributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: paragraphFont]
//3
let attributedParagraph = NSAttributedString(
        string: "\(paragraphToBePrinted)",attributes: paragraphAttributes
    )
//4
let paragraphStringSize = attributedParagraph.size()
//5
let paragraphStringRect = CGRect(
        x: (pageRect.width - pageRect.width) + 20,y: paragraphTop,width: paragraphStringSize.width,height: paragraphStringSize.height
    )
//6
attributedParagraph.draw... if while printing the paragraph you come to a certain point near the bottom 
of the page (pageBottom + 50),then create a new page,and continue printing at startPoint (pagetop - 50).

有什么想法吗?我正在使用 CoreGraphics 创建 PDF 并使用 PDFKit 进行保存。

我从这里学到了这段代码https://www.raywenderlich.com/4023941-creating-a-pdf-in-swift-with-pdfkit

谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)