swift - 如何设置 PDFPage 标签并重绘缩略图?
问题描述
我正在尝试在 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
}
解决方案
There doesn't appear to be a method for setting the label of a PDFPage
. The label
property is Read-only.
This value is actually the displayed page number, so shouldn't normally need setting.
推荐阅读
- javascript - 为什么useState只有在你第二次点击按钮时才设置数据?
- java - Apache HTTP/2 客户端 5.0 POST 请求缺少有效负载/内容
- c# - 返回轮播页面中当前页面列表项的属性
- android - classes.jar' 已经包含条目 'META-INF/module_name_debug.kotlin_module',无法覆盖
- elasticsearch - 如何根据elasticsearch中的父文档字段对嵌套聚合字段进行排序?
- python - VSCode 远程 SSH:在新终端中运行 Python 文件
- php - JSON 正文末尾出现意外数字
- android - Kotlin - 如何知道常量对象中是否存在字符串?
- python - 如何在 Bokeh (Python) 中编写自定义 JS 回调?
- .net - 接口函数应该强制输入吗?