获取总页数和当前页QuickLook

时间:2019-03-06 19:06:30

标签: ios swift quicklook qlpreviewcontroller

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

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

像“ 9之9”一样-如何获得

1 个答案:

答案 0 :(得分:5)

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,请尝试安装带有迦太基的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)