-1

我想在导航栏上显示 pdf 的总页数

- (void)viewDidLoad
 {
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"pages" ofType:@"pdf"];

totalPages = (int)CGPDFDocumentGetNumberOfPages(path);

PageViewController *page = [[PageViewController alloc] initWithPDFAtPath:path];

page.view.frame = self.view.bounds;

[self.view addSubview:page.view];

[self addChildViewController:page];

 [self displaycurrentIndex:1];
}

- (void) displaycurrentIndex:(NSUInteger)currentIndex {
self.navigationItem.title = [NSString stringWithFormat:
                                              @"Page %u of %u",
                                              currentIndex,
                                              totalPages];
  }

但在导航栏上显示 1 of 0。

而它应该是 pdf 中总页数的 1。

编辑:

我没有使用这样的 URL 语句

 CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("paper.pdf"), NULL, NULL);
    pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);

就我而言,我正在使用

 NSString *path = [[NSBundle mainBundle] pathForResource:@"pages" ofType:@"pdf"];

这就是为什么它没有获得 pdf 的总页数。如何使用我的案例获得 pdf 的总页数。

谢谢

4

2 回答 2

2

CGPDFDocumentGetNumberOfPages()需要一个CGPDFDocumentRefas 参数,而不是文件的路径。

以下代码应该可以在给定的位置打开 PDF 文档path并获取页数:

NSURL *pdfurl = [NSURL fileURLWithPath:path];
CGPDFDocumentRef pdfDocument = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfurl);
int totalPages = (int)CGPDFDocumentGetNumberOfPages(pdfDocument);
CGPDFDocumentRelease(pdfDocument);

(__bridge CFURLRef)仅当您使用 ARC 编译时才需要。)

于 2013-07-11T21:40:36.113 回答
1
totalPages = (int)CGPDFDocumentGetNumberOfPages(path);

嗯?您需要传入CGPDFDocumentRef,而不是文件系统路径。

文档。

于 2013-07-11T21:42:07.327 回答