我有一个 PDF 文档,我想将其所有页面旋转 90 度。我找到了解决方案here但我无法让它发挥作用。结果 PDF 为空。任何帮助都会很棒。
这是我的代码:
guard let getCGPDFPage = pdfReference!.page(at: index) else {
NSLog("Error occurred in creating page")
return
}
page = getCGPDFPage
mediaBox = page.getBoxRect(.mediaBox)
// Get rotation angle and convert from degrees to radians:
let angle = CGFloat(page.rotationAngle) * CGFloat.pi / 180
// Apply rotation transform to media box:
var rotatedBox = mediaBox.applying(CGAffineTransform(rotationAngle: angle))
let m = page.getDrawingTransform(.mediaBox, rect: rotatedBox, rotate: 90, preserveAspectRatio: true)
writeContext?.concatenate(m)
writeContext?.clip(to: page.getBoxRect(.mediaBox))
writeContext?.beginPage(mediaBox: &rotatedBox)
writeContext?.drawPDFPage(page)
writeContext?.endPage()
最佳答案
有一种比使用 CoreGraphics 转换每个页面的上下文更简单的方法来旋转 PDF 文件的页面。
使用 PDFKit 的 PDFPage
对象,该对象包含页面旋转的属性。您可以将其设置为 90、180、270 或 0 度。
https://developer.apple.com/documentation/pdfkit/pdfpage/1503443-rotation
请注意,CGPDFPage
和 CGPDFDocument
与 PDFPage
和 PDFDocument
不同。
关于swift - 如何在 swift macOS 中旋转 PDF 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48769188/