使用Swift iOS重命名下载PDF文件

发布于 2025-01-30 09:30:09 字数 1039 浏览 3 评论 0原文

我成功地从API终点下载了PDF。下载PDF后,PDF的标题为:PDF Document.pdf。 如何更改PDF的标题?

我尝试使用pdfdocumentAttribute(请参阅下文)更新PDF的 ,但它不起作用。

var metadata = pdfDocument.documentAttributes!
metadata[PDFDocumentAttribute.subjectAttribute] = "subject attribute"
metadata[PDFDocumentAttribute. titleAttribute] = "title attribute"
pdfDocument.documentAttributes = metadata

注意:我不使用filemanager

我如何获取PDF: -

let task = session.dataTask(with: urlRequest) { (data, _, error) in
            DispatchQueue.main.async {
                guard let unwrappedData = data, error == nil else {
                    completion(.failure(error ?? Constants.dummyError))
                    return
                }

                guard let pdfDocument = PDFDocument(data: unwrappedData) else {
                    completion(.failure(error ?? Constants.dummyError))
                    return
                }

                completion(.success(pdfDocument))
            }
        }

I am successfully downloading a PDF from api end point. Once pdf is downloaded, the title of pdf is : PDF document.pdf .
How to change the title of PDF?

I tried to update metadata of PDF using PDFDocumentAttribute (see below), but it is not working.

var metadata = pdfDocument.documentAttributes!
metadata[PDFDocumentAttribute.subjectAttribute] = "subject attribute"
metadata[PDFDocumentAttribute. titleAttribute] = "title attribute"
pdfDocument.documentAttributes = metadata

Note: I am not using FileManager

How I am fetching PDF:-

let task = session.dataTask(with: urlRequest) { (data, _, error) in
            DispatchQueue.main.async {
                guard let unwrappedData = data, error == nil else {
                    completion(.failure(error ?? Constants.dummyError))
                    return
                }

                guard let pdfDocument = PDFDocument(data: unwrappedData) else {
                    completion(.failure(error ?? Constants.dummyError))
                    return
                }

                completion(.success(pdfDocument))
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场信仰旅途 2025-02-06 09:30:10

尝试以下操作:

pdfDocument.documentAttributes?["Title"] = "my title attribute"

pdfDocument.documentAttributes?[PDFDocumentAttribute.titleAttribute] = "my title attribute"

类似于pdfdocumentAttribute.subjectAttribute

以上将设置文档的标题,当您保存时,file名称将是任何文件名称您提供的内容。

edit-1:将pdfdocument保存到带有选择文件名称的文件中。

       DispatchQueue.main.async {
            guard let unwrappedData = data, error == nil else {
                completion(.failure(error ?? Constants.dummyError))
                return
            }
            guard let pdfDocument = PDFDocument(data: unwrappedData) else {
                completion(.failure(error ?? Constants.dummyError))
                return
            }
            
            // set the Title
            pdfDocument.documentAttributes?[PDFDocumentAttribute.titleAttribute] = "my title attribute"
            
            do {
                // save the document to the given file name ("mydoc.pdf")
                let docURL = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("mydoc.pdf")  // <-- here file name
                pdfDocument.write(to: docURL)
                print("\n docUrl: \(docURL.absoluteString)\n")
            }
            catch {
                print("Error \(error)")
            }
            completion(.success(pdfDocument))
        }

try this:

pdfDocument.documentAttributes?["Title"] = "my title attribute"

or

pdfDocument.documentAttributes?[PDFDocumentAttribute.titleAttribute] = "my title attribute"

Similarly for PDFDocumentAttribute.subjectAttribute.

The above will set the Title of your document, and when you save it, the file name will be whatever file name you give it.

EDIT-1: saving the pdfDocument to a file with a chosen file name.

       DispatchQueue.main.async {
            guard let unwrappedData = data, error == nil else {
                completion(.failure(error ?? Constants.dummyError))
                return
            }
            guard let pdfDocument = PDFDocument(data: unwrappedData) else {
                completion(.failure(error ?? Constants.dummyError))
                return
            }
            
            // set the Title
            pdfDocument.documentAttributes?[PDFDocumentAttribute.titleAttribute] = "my title attribute"
            
            do {
                // save the document to the given file name ("mydoc.pdf")
                let docURL = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("mydoc.pdf")  // <-- here file name
                pdfDocument.write(to: docURL)
                print("\n docUrl: \(docURL.absoluteString)\n")
            }
            catch {
                print("Error \(error)")
            }
            completion(.success(pdfDocument))
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文