iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >iOS实现PDF文件浏览功能
  • 358
分享到

iOS实现PDF文件浏览功能

iOSPDF文件浏览 2022-05-18 14:05:46 358人浏览 安东尼
摘要

写了一个小Demo,显示本地pdf格式文件,支持翻页、跳页、缩放。  先看一下效果图: iOS开发,显示PDF格式文件方法有很多: 最简单的应该是UIWEBView

写了一个小Demo,显示本地pdf格式文件,支持翻页、跳页、缩放。 

先看一下效果图:

iOS开发,显示PDF格式文件方法有很多:

  • 最简单的应该是UIWEBView,可以加载本地或网络PDF文件,支持上下滑动浏览、缩放。
  • 优化一点的是用系统的QLPreviewController加载,实现起来也比较方便,支持上下滑动浏览,左后滑动可多PDF文件切换,同时支持原生的分享打印,QLPreviewController支持的文档格式也比较多,如pdf、doc、docx、xls、xlsx、txt、ppt、mp4...
  • 上面两种都没有足够的自定义空间,不在这里做过多的介绍了,另外一种也就是本篇用到的ioS核心图形库:Core Graphics,绘制PDF文档。

简单说一下逻辑,根据本地路径获取到CGPDFDocumentRef,在drawRect中绘制上下文,画出PDF文件。通过UIScrollView实现缩放,添加UIGestureRecognizer实现单击、双击、左滑、右滑功能。基于CATransition实现翻页动画。

下面贴上核心代码:

承载PDF文件视图的控制器:HWPDFBrowseVC


#import <UIKit/UIKit.h>
 
@interface HWPDFBrowseVC : UIViewController
 
@property (nonatomic, copy) NSString *filePath;
@property (nonatomic, copy) NSString *fileName;
 
@end
 

 
#import "HWPDFBrowseVC.h"
#import "HWPDFBrowseView.h"
#import "HWPDFBrowseToolBar.h"
#import "HWPDFBrowseScrollView.h"
 
#define KPicMaxScale 3.0
#define KMainW [UIScreen mainScreen].bounds.size.width
#define KMainH [UIScreen mainScreen].bounds.size.height
 
@interface HWPDFBrowseVC ()<UIScrollViewDelegate, HWPDFBroeseToolBarDelegate>
 
@property (nonatomic, weak) HWPDFBrowseScrollView *scrollView;
@property (nonatomic, weak) HWPDFBrowseView *browseView;
@property (nonatomic, weak) HWPDFBrowseToolBar *toolBar;
@property (nonatomic, assign) CGFloat minZoomScale;
@property (nonatomic, assign) CGFloat lastScrContX;
 
@end
 
@implementation HWPDFBrowseVC
 
- (void)viewDidLoad {
 [super viewDidLoad];
 
 //初始化
 self.view.backgroundColor = [UIColor whiteColor];
 self.navigationItem.title = _fileName;
 
 //创建控件
 [self creatControl];
}
 
- (void)viewWillDisappear:(BOOL)animated
{
 [super viewWillDisappear:animated];
 
 //防止隐藏导航时,左滑返回导航消失
 CGRect temNavBarFrame = self.navigationController.navigationBar.frame;
 temNavBarFrame.origin.y = 20;
 self.navigationController.navigationBar.frame = temNavBarFrame;
}
 
- (void)creatControl
{
 //导航右侧按钮
 UIButton *deleteBtn = [[UIButton alloc] initWithFrame:CGRectMake(9, 0, 40, 40)];
 deleteBtn.titleLabel.font = [UIFont systemFontOfSize:16.f];
 [deleteBtn setTitle:@"跳页" forState:UIControlStateNORMal];
 [deleteBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
 [deleteBtn addTarget:self action:@selector(navBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
 UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
 [rightView addSubview:deleteBtn];
 self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightView];
 
 //scrollView
 HWPDFBrowseScrollView *scrollView = [[HWPDFBrowseScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
 scrollView.delegate = self;
 scrollView.backgroundColor = [UIColor blackColor];
 scrollView.maximumZoomScale = KPicMaxScale;
 scrollView.showsVerticalScrollIndicator = NO;
 scrollView.showsHorizontalScrollIndicator = NO;
 [self.view addSubview:scrollView];
 _scrollView = scrollView;
 
 //pdf视图
 HWPDFBrowseView *browseView = [[HWPDFBrowseView alloc] initWithFilePath:_filePath];
 [scrollView addSubview:browseView];
 _browseView = browseView;
 
 //绘制pdf视图后缩放至屏幕完全居中显示
 CGRect frame = browseView.frame;
 frame.size.width = browseView.frame.size.width > KMainW ? KMainW : browseView.frame.size.width;
 frame.size.height = frame.size.width * (browseView.frame.size.height / browseView.frame.size.width);
 if (frame.size.height > KMainH) {
 frame.size.height = KMainH;
 frame.size.width = KMainH * (browseView.frame.size.width / browseView.frame.size.height);
 }
 
 //根据缩放调整
 _minZoomScale = frame.size.width / browseView.frame.size.width;
 scrollView.allowScrollScale = _minZoomScale;
 scrollView.minimumZoomScale = _minZoomScale;
 scrollView.zoomScale = _minZoomScale;
 
 //底部工具栏
 HWPDFBrowseToolBar *toolBar = [[HWPDFBrowseToolBar alloc] initWithFrame:CGRectMake(0, KMainH - 49, KMainW, 49) currentPage:_browseView.currentPage totalPage:_browseView.totalPages];
 toolBar.delegate = self;
 [self.view addSubview:toolBar];
 _toolBar = toolBar;
 
 //单击
 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click)];
 tap.numberOfTouchesRequired = 1;
 tap.numberOfTapsRequired = 1;
 [scrollView addGestureRecognizer:tap];
 
 //双击
 UITapGestureRecognizer *tapDouble = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleClick)];
 tapDouble.numberOfTapsRequired = 2;
 [scrollView addGestureRecognizer:tapDouble];
 [tap requireGestureRecognizerToFail:tapDouble];
 
 //右滑手势
 UISwipeGestureRecognizer *rightSwip = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(nextPage)];
 rightSwip.direction = UISwipeGestureRecognizerDirectionLeft;
 [scrollView addGestureRecognizer:rightSwip];
 
 //左滑手势
 UISwipeGestureRecognizer *leftSwip = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(forwardPage)];
 leftSwip.direction = UISwipeGestureRecognizerDirectionRight;
 [scrollView addGestureRecognizer:leftSwip];
}
 
- (void)navBtnOnClick
{
 [_toolBar showWindow];
}
 
//单击屏幕显示隐藏菜单
- (void)click
{
 CGFloat navBarY = _toolBar.frame.origin.y == KMainH - 49 ? -64 : 20;
 CGFloat toolBarY = _toolBar.frame.origin.y == KMainH - 49 ? KMainH : KMainH - 49;
 
 [UIView animateWithDuration:0.25 animations:^{
 CGRect temNavBarFrame = self.navigationController.navigationBar.frame;
 temNavBarFrame.origin.y = navBarY;
 self.navigationController.navigationBar.frame = temNavBarFrame;
 CGRect temToolBarFrame = _toolBar.frame;
 temToolBarFrame.origin.y = toolBarY;
 _toolBar.frame = temToolBarFrame;
 }];
}
 
//双击屏幕放大缩小图片
- (void)doubleClick
{
 [UIView animateWithDuration:0.25f animations:^{
 _scrollView.zoomScale = _scrollView.zoomScale == _minZoomScale ? KPicMaxScale : _minZoomScale;
 }];
}
 
//左滑事件
- (void)nextPage
{
 [_browseView nextPage];
 _toolBar.currentPage = _browseView.currentPage;
}
 
//右滑事件
- (void)forwardPage
{
 [_browseView prePage];
 _toolBar.currentPage = _browseView.currentPage;
}
 
#pragma mark - UICouseBrowseToolBarDelegate
- (void)browseToolBar:(HWPDFBrowseToolBar *)browseToolBar didClickFinishButtonWithPage:(NSString *)page
{
 _browseView.currentPage = [page integerValue];
 [_browseView reloadView];
 [_toolBar dismissKeyboard];
 _scrollView.zoomScale = _minZoomScale;
}
 
- (void)browseToolBar:(HWPDFBrowseToolBar *)browseToolBar didPageButtonWithAction:(BOOL)nextPage
{
 if (nextPage) {
 [self nextPage];
 }else {
 [self forwardPage];
 }
 _scrollView.zoomScale = _minZoomScale;
}
 
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
 CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width) ? (scrollView.bounds.size.width - scrollView.contentSize.width) * 0.5 : 0.0;
 CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height) ? (scrollView.bounds.size.height - scrollView.contentSize.height) * 0.5 : 0.0;
 _browseView.center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX, scrollView.contentSize.height * 0.5 + offsetY - 64);
}
 
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
 return _browseView;
}
 
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
 if (scrollView.isZooming) return;
 
 //允许翻页的偏移量
 CGFloat movePadding = 70.f;
 
 //仿苹果原生相册,图片放大后,滑动前在边界时在可以翻页,这里加了±10的偏移量
 if (scrollView.contentOffset.x < - movePadding && _lastScrContX < 10) {
 _scrollView.zoomScale = _minZoomScale;
 [self forwardPage];
 }
 
 if (scrollView.contentSize.width - scrollView.contentOffset.x < KMainW - movePadding && scrollView.contentSize.width != 0 && fabsf(_lastScrContX + KMainW - scrollView.contentSize.width) < 10) {
 _scrollView.zoomScale = _minZoomScale;
 [self nextPage];
 }
}
 
//滑动自然停止时调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
 _lastScrContX = scrollView.contentOffset.x;
}
 
//滑动手动停止时调用
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
 _lastScrContX = scrollView.contentOffset.x;
}
 
- (void)didReceiveMemoryWarning {
 [super didReceiveMemoryWarning];
 // Dispose of any resources that can be recreated.
}
 
@end

PDF文件视图:HWPDFBrowseView


#import <UIKit/UIKit.h>
 
@interface HWPDFBrowseView : UIView{
 CGPDFDocumentRef pdfDocumentRef;
}
 
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, assign) NSInteger totalPages;
 
- (id)initWithFilePath:(NSString *)filePath;
- (void)reloadView;
- (void)prePage;
- (void)nextPage;
 
@end
 

 
#import "HWPDFBrowseView.h"
 
@implementation HWPDFBrowseView
 
- (id)initWithFilePath:(NSString *)filePath
{
 pdfDocumentRef = [self createPDFFromExistFile:filePath];
 
 self = [super initWithFrame:CGPDFPageGetBoxRect(CGPDFDocumentGetPage(pdfDocumentRef, 1), kCGPDFMediaBox)];
 
 return self;
}
 
- (CGPDFDocumentRef)createPDFFromExistFile:(NSString *)aFilePath
{
 CFStringRef path = CFStrinGCreateWithCString(NULL, [aFilePath UTF8String], kCFStringEncodingUTF8);
 CFURLRef urlRef = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, NO);
 CFRelease(path);
 CGPDFDocumentRef document = CGPDFDocumentCreateWithURL(urlRef);
 CFRelease(urlRef);
 _totalPages = CGPDFDocumentGetNumberOfPages(document);
 _currentPage = 1;
 if (_totalPages == 0) return NULL;
 
 return document;
}
 
- (void)reloadView
{
 [self setNeedsDisplay];
}
 
- (void)drawRect:(CGRect)rect
{
 CGContextRef context = UIGraphicsGetCurrentContext();
 [[UIColor whiteColor] set];
 CGContextFillRect(context, rect);
 CGContextTranslateCTM(context, 0.0, rect.size.height);
 CGContextScaleCTM(context, 1.0, -1.0);
 CGPDFPageRef page = CGPDFDocumentGetPage(pdfDocumentRef, _currentPage);
 CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, rect, 0, true);
 CGContextConcatCTM(context, pdfTransform);
 CGContextDrawPDFPage(context, page);
}
 
//上一页
- (void)prePage
{
 if(_currentPage < 2) {
 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"已经第一页了!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil ];
 [alert show];
 return;
 }
 
 --_currentPage;
 [self reloadView];
 [self transitionWithType:@"pageUnCurl" WithSubtype:kCATransitionFromRight ForView:self];
}
 
//下一页
- (void)nextPage
{
 if(_currentPage >= _totalPages) {
 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"已经最后一页了!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil ];
 [alert show];
 return;
 }
 
 ++_currentPage;
 [self reloadView];
 [self transitionWithType:@"pageCurl" WithSubtype:kCATransitionFromRight ForView:self];
}
 
//设置翻页动画效果
- (void)transitionWithType:(NSString *)type WithSubtype:(NSString *)subtype ForView:(UIView *)view
{
 CATransition *animation = [CATransition animation];
 animation.duration = 0.7f;
 animation.type = type;
 if (subtype) animation.subtype = subtype;
 animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;
 [view.layer addAnimation:animation forKey:@"animation"];
}
 
@end

Demo 下载链接

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: iOS实现PDF文件浏览功能

本文链接: https://www.lsjlt.com/news/30331.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • iOS实现PDF文件浏览功能
    写了一个小Demo,显示本地PDF格式文件,支持翻页、跳页、缩放。  先看一下效果图: iOS开发,显示PDF格式文件方法有很多: 最简单的应该是UIWebView...
    99+
    2022-05-18
    iOS PDF 文件浏览
  • Vue使用PDF.js实现浏览pdf文件功能
    目录写在前面开始下载放入项目中制作组件使用写在前面 很头大 , 本来网络实际地址的 pdf 文件直接放在 iframe 的 src 中就可以浏览 pdf 文件的 , 但是对于虚拟地址...
    99+
    2023-05-16
    Vue PDF.js浏览pdf文件 Vue PDF.js pdf文件 Vue PDF.js
  • iOS开发实现图片浏览功能
    本文实例为大家分享了iOS实现图片浏览功能的具体代码,供大家参考,具体内容如下 这是整体的效果图: 其中main.stroyboard中的控件有2个button,2个label,一...
    99+
    2022-11-13
  • 怎么使用vue-pdf插件实现pdf文档预览功能
    这篇文章主要介绍了怎么使用vue-pdf插件实现pdf文档预览功能的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇怎么使用vue-pdf插件实现pdf文档预览功能文章都会有所收获,下面我们一起来看看吧。vue-p...
    99+
    2023-07-05
  • vue3如何实现PDF文件在线预览功能
    目录概述正文创建 vue3 项目添加 PDF 预览插件总结概述 之前我们用 Reactjs + React-PDF 实现了 React 版的 PDF 文件预览,今天我们用 Vue3 ...
    99+
    2022-11-13
  • golang实现浏览器导出excel文件功能
    目录1.依赖包2.示例3.分析3.1先根据需求查询需要的list对象3.2新建文件,设置文件名,跟列名3.3设置标题单元格3.4设置内容单元格3.5流媒体返回web1.依赖包 imp...
    99+
    2022-11-13
  • Vue实现docx/xlsx/pdf等类型文件预览功能
    目录使用安装使用示例docx文档的预览excel文档预览pdf文档预览都2023年了,怎么文件预览还这么难! 发现了问题之后,就想着能不能实现一个简单的VUE组件库,能够解决以上问题...
    99+
    2023-02-13
    Vue docx文件预览 Vue xlsx文件预览 Vue pdf文件预览 Vue文件预览
  • Vue实现docx、pdf格式文件在线预览功能
    目录介绍docx安装使用PDF安装引入和使用pdf的放大和缩小多格式的文件渲染函数映射不支持的文件提示处理总结介绍 在业务中,如果遇到文档管理类的功能,会出现需要在线预览的业务需求,...
    99+
    2022-11-13
  • Vue-pdf实现在线预览PDF文件
    前言 在大多数项目中都会遇到在线预览PDF文件,项目使用的是element ui,使用vue-pdf实现。 安装依赖 npm install --save vue-pdf 相关...
    99+
    2022-11-12
  • iOS实现文件下载功能
    本文实例为大家分享了iOS实现文件下载的具体代码,供大家参考,具体内容如下 说明: 1).获取网络文件大小; 2).开启循环,计算每段position开始与结束位置,通过Range头...
    99+
    2022-11-13
  • Vue实现预览文件(Word/Excel/PDF)功能的示例代码
    目录安装 引入组件、注册 使用之前也有写过两篇预览pdf的,但好像还没写过预览word和excel的,但是这次的预览pdf和之前的三个又不一样!使用pdfobje...
    99+
    2023-03-20
    Vue预览文件 Vue预览Word Vue预览Excel Vue预览PDF
  • Vue实现在线预览pdf文件功能(利用pdf.js/iframe/embed)
    前言 最近在做一个精品课程,需要在线预览课件ppt,我们的思路是将ppt转换为pdf在线预览,所以问题就是如何实现在线预览pdf了。 在实现的过程中,为了更好地显示效果,我采用了多...
    99+
    2022-11-12
  • vue如何实现在线预览PDF文档功能
    这篇文章主要介绍了vue如何实现在线预览PDF文档功能的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇vue如何实现在线预览PDF文档功能文章都会有所收获,下面我们一起来看看吧。下面通过一个实例来介绍在Vue.j...
    99+
    2023-07-05
  • HTML5拖拽文件到浏览器并实现文件上传下载功能
    本篇内容主要讲解“HTML5拖拽文件到浏览器并实现文件上传下载功能”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“HTML5拖拽文件到浏览器并实现文件上传下载功能...
    99+
    2022-10-19
  • Android编程实现文件浏览功能的方法【类似于FileDialog的功能】
    本文实例讲述了Android编程实现文件浏览功能的方法。分享给大家供大家参考,具体如下: 最近正在弄上传文件,当时想怎么能实现fileDialog的功能呢,打开文件,浏览文件,...
    99+
    2022-06-06
    方法 Android
  • Vue3+Vue-PDF实现PDF文件在线预览实战
    目录概述项目实战创建 vue3 项目添加 PDF 预览插件概述 今天我们用 Vue3 + Vue-PDF 实现 Vue 版...
    99+
    2022-11-13
  • 使用Vue3+PDF.js实现PDF预览功能
    目录1 前言2 PDF 预览测试2.1 下载 PDF.js2.2 window.open 直接打开2.3 弹框形式打开3 修改配置项3.1 修改主题色为暗色系3.2 修改默认语言为简...
    99+
    2022-12-24
    vue使用pdf.js vue3.0 pdf vue pdf 预览
  • vue-pdf实现文件在线预览
    本文实例为大家分享了vue-pdf实现文件在线预览的具体代码,供大家参考,具体内容如下 提示:记录一下vue-pdf使用方法,避免忘记,便于后面使用 前言 提示:以下是本篇文章正文内...
    99+
    2022-11-12
  • Vue怎么使用vue-pdf实现PDF文件预览
    这篇文章主要介绍了Vue怎么使用vue-pdf实现PDF文件预览的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Vue怎么使用vue-pdf实现PDF文件预览文章都会有所收获,下面我们一起来看看吧。先看下效果&n...
    99+
    2023-07-05
  • Vue3+Vue-PDF怎么实现PDF文件在线预览
    创建 vue3 项目我们先创建一个的 Vue3 项目, 在终端中输入命令pnpm create vite vue-pdf-preview选择 vue-ts 回车,cd 进入项目根目录,执行 pnpm install, 等待项目依赖包安装完成...
    99+
    2023-05-14
    PDF Vue3
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作