xcode – 对PDF的NSImages并将它们合并到Swift中

前端之家收集整理的这篇文章主要介绍了xcode – 对PDF的NSImages并将它们合并到Swift中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个包含一些NS Images的数组.
我主要想将NSImage转换为PDF.所以,任何人都可以告诉我如何在 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来插入,删除和重新排序页面.

猜你在找的Xcode相关文章