I'm trying to create a multipage PDF file from NSImages in swift on macOS. How do i set the PDFPage label and how do i trigger the generation of the preview thumbnails.
PDFPage has a variable for the label: https://developer.apple.com/documentation/pdfkit/pdfpage/1504088-label
but i didn't find a way to set the label i tried to set the label to 1 with: page.setValue("1", forKey: "label")
but this did nothing
I didn't find a way to trigger the generation of thumbnails.
code example for one page:
//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
Expected result: a thumbnail of the PDF page with the label "1" appears in PDFThumbnailView and the PDF pages are rendered in PDFView.
Actual result: a blank thumbnail with the label "Label" appears in PDFThumbnailView and the PDF pages are rendered in PDFView.
EDIT: My workaround to solve my problems is to save the PDFDocument to disk and reload it. It's not pretty but it's working... any advice is welcome!
myPDFDocument.write(toFile: "path/to/temp/folder/mytemp.pdf")
if let document = PDFDocument(url: URL(fileURLWithPath: "path/to/temp/folder/mytemp.pdf"))
{
myPDFDocument = document
}