swift - 如何设置PDF页面标签并重绘缩略图?

标签 swift apple-pdfkit

我正在尝试在 macOS 上使用 NSImages 快速创建多页 PDF 文件。如何设置 PDFPage 标签以及如何触发预览缩略图的生成。

PDFPage 有一个标签变量: https://developer.apple.com/documentation/pdfkit/pdfpage/1504088-label

但我没有找到设置标签的方法,我尝试使用以下方法将标签设置为 1: page.setValue("1", forKey: "label") 但这没有任何作用

我没有找到触发缩略图生成的方法。

一页的代码示例:

//stroyboard pdfView
@IBOutlet weak var myPdfView: PDFView!

//Storyboad PDFThumbnailView
@IBOutlet weak var myPDFThumbnailView:PDFThumbnailView!

//create a new PDFDocument
var myPDFDocument = PDFDocument()

//image path
guard let pngPath = Bundle.main.url(forResource: "example", withExtension: "png") else { return }

//create NSImage
if let firstPageImage = NSImage(contentsOf: pngPath){

    //PDFPage
    if let page = PDFPage(image: firstPageImage) {

        //insert Page in PDFDocument
        myPDFDocument.insert(page, at: myPDFDocument.pageCount)

    }
}

//set PDFDocument
myPdfView.document = myPDFDocument

//set PDFView
myPDFThumbnailView.pdfView = myPdfView

预期结果: 带有标签“1”的 PDF 页面的缩略图出现在 PDFThumbnailView 中,并且 PDF 页面在 PDFView 中呈现。

实际结果: PDFThumbnailView 中出现带有标签“Label”的空白缩略图 PDF 页面在 PDFView 中呈现。

编辑: 我解决问题的方法是将 PDFDocument 保存到磁盘并重新加载。它不漂亮,但它正在工作......欢迎任何建议!

myPDFDocument.write(toFile: "path/to/temp/folder/mytemp.pdf")
if let document = PDFDocument(url: URL(fileURLWithPath: "path/to/temp/folder/mytemp.pdf")) 
{
    myPDFDocument = document
}

最佳答案

似乎没有设置 PDFPage 标签的方法。 label 属性是只读的。

该值实际上是显示的页码,因此通常不需要设置。

关于swift - 如何设置PDF页面标签并重绘缩略图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54708980/

相关文章:

ios - tableViewSelectionDidChange 在 Xcode 8 中不起作用

swift - 如何删除 PDFDocument 边距

ios - PDFKit 交换注释内容

ios - 使用 Swift 5 - PDFKit 在 iOS 中编辑和保存现有的 pdf 文档

ios - 调用 CLLocation 中的额外参数 'userinfo'

ios - swift 3 iTunes订阅收据验证实时模式

swift - 传递当前的 SKScene 实例 Swift

ios - 无法在 iPad 的 ios 应用程序中使用 swift 2 准备圆形 ImageView

ios - 如何使用 PDFKit 突出显示 pdf 中选定的文本?