2
我有一個包含一些NSImages的數組。 我主要想將NSImage轉換爲PDF。那麼,誰能告訴我如何在Swift中做到這一點。PDF格式的NSImages並將它們合併到Swift中
如果可能,你們可以告訴我如何將PDF合併成一個並輸出它? 非常感謝。
我有一個包含一些NSImages的數組。 我主要想將NSImage轉換爲PDF。那麼,誰能告訴我如何在Swift中做到這一點。PDF格式的NSImages並將它們合併到Swift中
如果可能,你們可以告訴我如何將PDF合併成一個並輸出它? 非常感謝。
下面是從圖像創建PDF文檔的一些粗略步驟,這應該讓你開始。
使用PDFKit(導入Quartz)框架。
// Create an empty PDF document
let pdfDocument = PDFDocument()
// Load or create your NSImage
let image = NSImage(....)
// Create a PDF page instance from your image
let pdfPage = PDFPage(image: image!)
// Insert the PDF page into your document
pdfDocument.insert(pdfPage!, at: 0)
// Get the raw data of your PDF document
let data = pdfDocument.dataRepresentation()
// The url to save the data to
let url = URL(fileURLWithPath: "/Path/To/Your/PDF")
// Save the data to the url
try! data!.write(to: url)
您可能需要修改頁面的邊界和圖像大小來獲取所需的確切PDF頁面大小。
您可以使用其他PDFDocument/PDFPage API來插入,刪除和重新排序頁面。