方法一:利用webview
-
-(void)loadDocument:(NSString *)documentName inView:(UIWebView *)webView
-
{
-
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil];
-
NSURL *url = [NSURL fileURLWithPath:path];
-
NSURLRequest *request = [NSURLRequest requestWithURL:url];
-
[webView loadRequest:request];
-
}
好处是:实现起来简单
但是:仅能浏览,拿不到任何回调,而且固定竖版拖动,想实现翻页动效果就会出错
方法二:利用CGContextDrawPDFPage
-
-
CGPDFDocumentRef GetPDFDocumentRef(NSString *filename)
-
{
-
CFStringRef path;
-
CFURLRef url;
-
CGPDFDocumentRef document;
-
size_t count;
-
-
path = CFStringCreateWithCString (NULL, [filename UTF8String], kCFStringEncodingUTF8);
-
url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);
-
-
CFRelease (path);
-
document = CGPDFDocumentCreateWithURL (url);
-
CFRelease(url);
-
count = CGPDFDocumentGetNumberOfPages (document);
-
if (count == 0) {
-
printf("[%s] needs at least one page!\n", [filename UTF8String]);
-
return NULL;
-
} else {
-
printf("[%ld] pages loaded in this PDF!\n", count);
-
}
-
return document;
-
}
-
-
void DisplayPDFPage (CGContextRef myContext, size_t pageNumber, NSString *filename)
-
{
-
CGPDFDocumentRef document;
-
CGPDFPageRef page;
-
-
document = GetPDFDocumentRef (filename);
-
page = CGPDFDocumentGetPage (document, pageNumber);
-
CGContextDrawPDFPage (myContext, page);
-
CGPDFDocumentRelease (document);
-
}
-
CGPDFDocumentRef GetPDFDocumentRef(NSString *filename)
-
{
-
CFStringRef path;
-
CFURLRef url;
-
CGPDFDocumentRef document;
-
size_t count;
-
-
path = CFStringCreateWithCString (NULL, [filename UTF8String], kCFStringEncodingUTF8);
-
url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);
-
-
CFRelease (path);
-
document = CGPDFDocumentCreateWithURL (url);
-
CFRelease(url);
-
count = CGPDFDocumentGetNumberOfPages (document);
-
if (count == 0) {
-
printf("[%s] needs at least one page!\n", [filename UTF8String]);
-
return NULL;
-
} else {
-
printf("[%ld] pages loaded in this PDF!\n", count);
-
}
-
return document;
-
}
-
-
void DisplayPDFPage (CGContextRef myContext, size_t pageNumber, NSString *filename)
-
{
-
CGPDFDocumentRef document;
-
CGPDFPageRef page;
-
-
document = GetPDFDocumentRef (filename);
-
page = CGPDFDocumentGetPage (document, pageNumber);
-
CGContextDrawPDFPage (myContext, page);
-
CGPDFDocumentRelease (document);
-
}
这样显示出来的pdf单页是倒立的,Quartz坐标系和UIView坐标系不一样所致,调整坐标系,使pdf正立:
-
CGContextRef context = UIGraphicsGetCurrentContext();
-
CGContextTranslateCTM(context, 80, self.frame.size.height-60);
-
CGContextScaleCTM(context, 1, -1);
配合UIPageViewController实现翻页浏览
-
- (PDFViewController *)viewControllerAtIndex:(NSUInteger)index
-
{
-
-
if (([self.pagePDF count] == 0 )|| (index > [self.pagePDF count]) ) {
-
return nil;
-
}
-
-
-
PDFViewController *dataViewController = [[PDFViewController alloc]initWithNibName:@"PDFViewController" bundle:nil];
-
-
dataViewController.pdfview = [[PDFView alloc]initWithFrame:self.view.frame atPage:index];
-
[dataViewController.view addSubview:dataViewController.pdfview];
-
NSLog(@"index = %d",index);
-
return dataViewController;
-
}
-
-
- (NSUInteger) indexOfViewController:(PDFViewController *)viewController
-
{
-
return [self.pagePDF indexOfObject:viewController.pdfview];
-
}
-
-
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
-
{
-
NSUInteger index = [self indexOfViewController:(PDFViewController *)viewController];
-
if ((index == 0 ) || (index == NSNotFound)){
-
return nil;
-
}
-
-
index--;
-
return [self viewControllerAtIndex:index];
-
}
-
-
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
-
{
-
NSUInteger index = [self indexOfViewController:(PDFViewController *)viewController];
-
if (index == NSNotFound)
-
{
-
return nil;
-
}
-
-
index++;
-
-
if (index == [self.pagePDF count]){
-
return nil;
-
}
-
-
return [self viewControllerAtIndex:index];
-
}
-
CGPDFDocumentRef GetPDFDocumentRef(NSString *filename)
-
{
-
CFStringRef path;
-
CFURLRef url;
-
CGPDFDocumentRef document;
-
size_t count;
-
-
path = CFStringCreateWithCString (NULL, [filename UTF8String], kCFStringEncodingUTF8);
-
url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);
-
-
CFRelease (path);
-
document = CGPDFDocumentCreateWithURL (url);
-
CFRelease(url);
-
count = CGPDFDocumentGetNumberOfPages (document);
-
if (count == 0) {
-
printf("[%s] needs at least one page!\n", [filename UTF8String]);
-
return NULL;
-
} else {
-
printf("[%ld] pages loaded in this PDF!\n", count);
-
}
-
return document;
-
}
-
-
void DisplayPDFPage (CGContextRef myContext, size_t pageNumber, NSString *filename)
-
{
-
CGPDFDocumentRef document;
-
CGPDFPageRef page;
-
-
document = GetPDFDocumentRef (filename);
-
page = CGPDFDocumentGetPage (document, pageNumber);
-
CGContextDrawPDFPage (myContext, page);
-
CGPDFDocumentRelease (document);
-
}
PDF的显示和浏览
原文:http://blog.csdn.net/hnjyzqq/article/details/42993175