使用Swift iOS重命名下载PDF文件
我成功地从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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试以下操作:
或
类似于
pdfdocumentAttribute.subjectAttribute
。以上将设置文档的
标题
,当您保存时,file
名称将是任何文件名称
您提供的内容。edit-1:将
pdfdocument
保存到带有选择文件名称
的文件中。try this:
or
Similarly for
PDFDocumentAttribute.subjectAttribute
.The above will set the
Title
of your document, and when you save it, thefile
name will be whateverfile name
you give it.EDIT-1: saving the
pdfDocument
to a file with a chosenfile name
.