ios - 获取总页数和当前页面 QuickLook

标签 ios swift quicklook qlpreviewcontroller

我正在使用“QLPreviewController”来显示文档文件 (pdf/doc)。 有什么方法可以获取总页数和当前页码吗?

或查看 pdf/doc 并获取页数的任何其他方式。 enter image description here

像“9 of 9”- 如何得到它

最佳答案

QuickLook 仅适用于外观伴侣。您可以从 CGPDFDocument 获取元数据

if let url = Bundle.main.url(forResource: "yourFileNameHere", withExtension: "pdf"),
        let pdf = CGPDFDocument(url as CFURL) {
        let count = pdf.numberOfPages
        print(count)
    }

对于 docx,尝试使用 carthage 安装 ZipZap:github "pixelglow/ZipZap""8.1"

    NSURL *url = [[NSBundle mainBundle] URLForResource:@"demo" withExtension:@"docx"];
    ZZArchive* archive = [ZZArchive archiveWithURL:url error:nil];
    NSString *pageCount;
    for (ZZArchiveEntry* entry in archive.entries) {
    if([entry.fileName isEqualToString:@"docProps/app.xml"]) {
        NSString *responseString = [[NSString alloc] initWithData: 
        [entry newDataWithError:nil] encoding:NSUTF8StringEncoding];
        //Get the value between tag <Pages> and </Pages>
        NSRange tag1 = [responseString rangeOfString:@"<Pages>"];
        NSRange tag2 = [responseString rangeOfString:@"</Pages>"];
        pageCount = [responseString substringWithRange: 
        NSMakeRange((tag1.location + tag1.length), (tag2.location - tag1.length - tag1.location))];
    }
}

这里是 docx 元数据的来源:https://www.forensicswiki.org/wiki/Word_Document_(DOCX)

关于ios - 获取总页数和当前页面 QuickLook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55030468/

相关文章:

ios - 在 Windows 中从命令提示符运行 xctestrun

ios - keyboardWillHide 通知故障

ios - 当我缩小表格 View 并滚动时,表格 View 无法正确呈现

ios - 在 xcode 11 中打开项目时 UINavigationBar 重叠状态栏

swift - 在 Swift 中从 .sks 场景文件加载 SKNode

ios - 如何在适用于 iOS 的 Google Material Design 上禁用水平滚动到选项卡

ios - 如何修复错误 : Binary operator '==' cannot be applied to operands of type 'NSExpression.ExpressionType' and '_'

ios - 无法发出 URL 的文件扩展名

objective-c - 将 QLPreviewController 添加为 subview 不会加载 PDF

objective-c - 如何为 QuickLook 插件开发/创建 GUI?