gpt4 book ai didi

Swift:如何像 iOS 13 的 Book 应用程序一样为 PDF 实现标记(注释)?

转载 作者:行者123 更新时间:2023-12-03 20:58:29 26 4
gpt4 key购买 nike

我到处搜索,但找不到答案。我能找到的最接近的是

func saveAnnotations_forever() {
//let documentAttributes = pdfDocument?.documentAttributes
//let attachmentData = pdfDocument?.dataRepresentation()
let currentPage = pdfView.currentPage
let page_index = pdfDocument?.index(for: currentPage!)
let documentURL = self.pdfDocument?.documentURL// URL to your PDF document.

// Create a `PDFDocument` object using the URL.
let pdfDocument = PDFDocument(url: documentURL!)!

// `page` is of type `PDFPage`.
let page = self.pdfDocument!.page(at: page_index!)!

// Extract the crop box of the PDF. We need this to create an appropriate graphics context.
let bounds = page.bounds(for: .mediaBox)


// Create a `UIGraphicsImageRenderer` to use for drawing an image.
let renderer = UIGraphicsImageRenderer(bounds: bounds, format: UIGraphicsImageRendererFormat.default())

// This method returns an image and takes a block in which you can perform any kind of drawing.
let image = renderer.image { (context) in
// We transform the CTM to match the PDF's coordinate system, but only long enough to draw the page.
context.cgContext.saveGState()

context.cgContext.translateBy(x: 0, y: bounds.height)
context.cgContext.concatenate(CGAffineTransform.init(scaleX: 1, y: -1))
page.draw(with: .mediaBox, to: context.cgContext)

context.cgContext.restoreGState()

以此类推,将注释和 PDF 页面一起呈现为图像。但是,我无法搜索带注释页面的文本(因为它是作为图像呈现的),并且一旦保存,我就无法删除以前的注释。 iOS 的 Book 注释效果很好-我想实现这一点,但是如何实现?

提前致谢。

最佳答案

如果您尝试将注释保存为 PDF,不妨试试以下代码:

// add annotation to the current page
let rect = NSRect(x: 30, y: 30, width: 30, height: 30)
let annotation = PDFAnnotation(bounds: rect, forType: .text, withProperties: nil)
pdfView.currentPage?.addAnnotation(annotation)

// save the PDF to file
if let doc = self.document,
let url = self.document?.documentURL {
doc.write(to: url)
}
您可以使用 PDFAnnotation documentation进一步自定义注释。

关于Swift:如何像 iOS 13 的 Book 应用程序一样为 PDF 实现标记(注释)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59619614/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com