本文记录PDF文件手绘签名并重新保存;
主要实现步骤:
- PDF文件分页加载
- 在PDF视图上手绘涂鸦签名
- 将绘制过的PDF页和签名一起截图,并转换为单页PDF
4.将签过名的单页PDF合并至原始PDF文件
5.重新生成完整的PDF文件
一.分页加载PDF文件:
该部分参考:iOS开发——PDF文件的加载及显示(网络、本地),虽然排版乱了写,但是注释是相当的详尽,在此不做赘述;
1.加载PDF的方式:
- 网络加载:
CGPDFDocumentRef pdfRefByDataByUrl(NSString *fileName) {
NSURL*url = [NSURL URLWithString:fileName];
CFURLRef refURL = (__bridge_retained CFURLRef)url;
CGPDFDocumentRef document =CGPDFDocumentCreateWithURL(refURL);
// CGDataProviderRelease(document);
CFRelease(refURL);
return document;
}
- 本地加载:
CGPDFDocumentRef pdfRefByFilePath(NSString *aFilePath) {
CFStringRef path;
CFURLRef url;
CGPDFDocumentRef document;
path = CFStringCreateWithCString(NULL, [aFilePath UTF8String], kCFStringEncodingUTF8);
url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, NO);
document = CGPDFDocumentCreateWithURL(url);
CFRelease(path);
CFRelease(url);
return document;
}
Demo中使用网络加载:
//通过pdfRefByDataByUrl函数获取PDF文件资源,
self->docRef = pdfRefByDataByUrl(@"http://teaching.csse.uwa.edu.au/units/CITS4401/practicals/James1_files/SPMP1.pdf");
2.将PDF文件分解为单页PDF数据,以便分页展示:
#pragma mark - 获取所有需要显示的PDF所有页面数据
- (void)getDataArrayValue {
//获取总页数(注意,该页数从1开始)
size_t totalPages = CGPDFDocumentGetNumberOfPages(self->docRef);
self.totalPage = (int)totalPages;
for(int i = 1; i <= totalPages; i++) {
CGPDFPageRef pageRef = CGPDFDocumentGetPage(self->docRef,i);
CGRect pageRect = CGPDFPageGetBoxRect(pageRef, kCGPDFMediaBox);
// CGPDFPageRef 转 NSValue
NSValue *pageValue = [NSValue valueWithBytes:&pageRef objCType:@encode(CGPDFPageRef)];
// 保存PDF文件所有单页的数据
[self.pages addObject:pageValue];
RiderPDFView *view = [[RiderPDFView alloc]initWithFrame:CGRectMake(0,0,pageRect.size.width,self.view.frame.size.height) documentRef: self->docRef andPageNum:i];
// 保存PDF文件所有单页视图
[self.dataArray addObject:view];
}
}
此处处理一次性将PDF所有页面数据分页加载,并保存在self.dataArray
当中,实际中可以分批次(类似与上拉加载更多的逻辑)甚至是一页一页加载,视实际需求而定;
代码段中为在数组中保存pageRef
结构体,将之转换为NSValue对象;
二. 在PDF视图上手绘涂鸦签名
签名涂鸦,主要实现参考:PPSSignatureView,可控制笔画书写厚度,更贴近钢笔字的字迹;当然,自己画也可以;
PPSSignatureView继承与GLKView(GLKit框架),这里给出一个使用实例,不做过多描述:
EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
_signatureView = [[PPSSignatureView alloc] initWithFrame:CGRectMake(10, 80, 300, 300) context:context];
_signatureView.strokeColor = [UIColor redColor];
GLKViewController *glkView = [[GLKViewController alloc] init];
glkView.view = _signatureView;
[self addChildViewController:glkView];
实现在PDF页面(currentRiderPDFView)
涂鸦,将[currentRiderPDFView addSubview:_signatureView]
即可;(currentRiderPDFView 为第一步中分页显示PDF的RiderPDFView);
三.将绘制过的PDF页和签名一起截图,并转换为单页PDF
1.将绘制过的PDF页(RiderPDFView)和签名图层(PPSSignatureView)一起截图:
因 PPSSignatureView 是加在 RiderPDFView之上,故,仅仅对 RiderPDFView 截图即可获取完整的绘制过的PDF图像,实现如下:
UIImage *signatureImage = [self snapViewToImage:currentRiderPDFView];
-(UIImage *)snapViewToImage:(UIView *)view{
CGSize size = view.bounds.size;
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
CGContextSaveGState(context);
CGContextTranslateCTM(context, window.center.x, window.center.y);
CGContextConcatCTM(context, window.transform);
CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);
if ([view respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
} else {
[view.layer renderInContext:context];
}
CGContextRestoreGState(context);
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
2.将UIIMage转换为单页PDF:
将上述获得的 signatureImage
转换为单页PDF,为下面合并至原始PDF做准备:
CGPDFPageRef currentSinglePageRef = [self imageToPDFPageRef:signatureImage];
-(CGPDFPageRef)imageToPDFPageRef:(UIImage *)image{
[PDFManager createPDFFileWithSrc:UIImageJPEGRepresentation(image, 1.0) toDestFile:@"signaturePage.pdf" withPassword:nil];
NSString *path = [PDFManager pdfDestPath:@"signaturePage.pdf"];
CGPDFDocumentRef newPDFRef = pdfRefByFilePath(path);
// size_t pageSize = CGPDFDocumentGetNumberOfPages(newPDFRef);
CGPDFPageRef pageRef = CGPDFDocumentGetPage(newPDFRef, 1);
NSLog(@"绘制的PDF页:%@",path);
return pageRef;
}
其中 PDFManager
主要实现 UIIMage转PDF并存储,网上能找到的转换实发基本就这么一个;
四.将签过名的单页PDF合并至原始PDF文件
到此就很明了了,上一步获取绘制过的单页PDF(currentSinglePageRef
),将 currentSinglePageRef
替换数组_pages
对应位置的CGPDFPageRef实现PDF数据重新拼接:
CGPDFPageRef currentSinglePageRef = [self imageToPDFPageRef:signatureImage];
// 将 CGPDFPageRef 转换成 NSValue 以便于在集合中存储
NSValue *currentPageValue = [NSValue value:& currentSinglePageRef withObjCType:@encode(CGPDFPageRef)];
// 执行替换
[self.pages replaceObjectAtIndex:self->currentPage withObject:currentPageValue];
五.重新生成完整的PDF文件:
讲过上一步的处理,此时已经获得了涂鸦绘制过的完整PDF数据(_pages
),接下来要做的就是将_pages
重新生成PDF即可:
#pragma mark - 重新生成PDF文件,返回新文件地址
-(NSString *)reSavePDF:(NSArray <NSValue *> *)pdfPages{
// 新生成PDF文件路径
NSString *fileName = [NSString stringWithFormat:@"公文%d.pdf",arc4random_uniform(100)];
NSString *pdfPathOutput = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:fileName];
CFURLRef pdfURLOutput = (CFURLRef)CFBridgingRetain([NSURL fileURLWithPath:pdfPathOutput]);
// Create the output context
CGContextRef writeContext = CGPDFContextCreateWithURL(pdfURLOutput, NULL, NULL);
CGRect mediaBox;
CGPDFPageRef tempref;
// 写入文件
for (int i = 0; i < pdfPages.count; i++) {
[[pdfPages objectAtIndex:i] getValue:&tempref];
mediaBox = CGPDFPageGetBoxRect(tempref, kCGPDFMediaBox);
CGContextBeginPage(writeContext, &mediaBox);
CGContextDrawPDFPage(writeContext, tempref);
CGContextEndPage(writeContext);
}
CFRelease(pdfURLOutput);
CGPDFContextClose(writeContext);
CGContextRelease(writeContext);
return pdfPathOutput;
}
至此,全新的PDF文件也就生成完成;
若服务端能处理的话,只上传修改过的单页是最佳策略 ....
加密 .... 上传 .... 清沙盒 .... anything you want ....