|
|
@@ -0,0 +1,221 @@
|
|
|
+//
|
|
|
+// PDFUtils.m
|
|
|
+// AntsContract
|
|
|
+//
|
|
|
+// Created by Ray on 12/20/16.
|
|
|
+// Copyright © 2016 United Software Applications, Inc. All rights reserved.
|
|
|
+//
|
|
|
+
|
|
|
+#import "PDFUtils.h"
|
|
|
+#import "PDFPage.h"
|
|
|
+#import "LineDrawable.h"
|
|
|
+
|
|
|
+@implementation PDFUtils
|
|
|
++(void) addSignature :(UIImage*) img to:(NSMutableDictionary*)signatureData
|
|
|
+{
|
|
|
+
|
|
|
+
|
|
|
+ if(true)
|
|
|
+ {
|
|
|
+ NSString* newfile=[self saveTempSignature:UIImagePNGRepresentation(img)];
|
|
|
+
|
|
|
+ int newidx = [signatureData[@"count"] intValue];
|
|
|
+
|
|
|
+ // [@"file"]= newfile;
|
|
|
+
|
|
|
+ NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
|
|
|
+ item[@"file"]=newfile;
|
|
|
+ signatureData[[NSString stringWithFormat:@"item_%d",newidx]] = item;
|
|
|
+ signatureData[@"count"]=[NSNumber numberWithInt:newidx+1];
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
++(NSString *) saveTempSignature:(NSData *) image
|
|
|
+{
|
|
|
+ NSString* tempDir = NSTemporaryDirectory();
|
|
|
+
|
|
|
+ NSString *saveFileName=[NSString stringWithFormat:@"%@.png",[[NSUUID UUID ] UUIDString] ] ;
|
|
|
+
|
|
|
+ NSString *newFilePath=[tempDir stringByAppendingPathComponent:saveFileName];
|
|
|
+
|
|
|
+
|
|
|
+ bool bsuccess=[image writeToFile:newFilePath atomically:YES];
|
|
|
+ if(bsuccess)
|
|
|
+ {
|
|
|
+
|
|
|
+ return newFilePath;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return nil;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
++(CGPDFDocumentRef)OpenPDF:(NSString*) file
|
|
|
+{
|
|
|
+ //获取路径
|
|
|
+ /*
|
|
|
+ NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);//坐标
|
|
|
+
|
|
|
+ NSString *saveDirectory=[paths objectAtIndex:0];
|
|
|
+
|
|
|
+ NSString *saveFileName=@"myPDF.pdf";
|
|
|
+
|
|
|
+ NSString *newFilePath=[saveDirectory stringByAppendingPathComponent:saveFileName];
|
|
|
+ */
|
|
|
+
|
|
|
+ NSString *default_path = [[NSBundle mainBundle] pathForResource:file ofType:nil];
|
|
|
+ const char *filename=[default_path UTF8String];
|
|
|
+
|
|
|
+ NSLog(@"%@",default_path);
|
|
|
+
|
|
|
+ //关联上下文的对象
|
|
|
+
|
|
|
+ // CGContextRef pdfContext;
|
|
|
+
|
|
|
+ CFStringRef path;
|
|
|
+
|
|
|
+ CFURLRef url;
|
|
|
+
|
|
|
+ path=CFStringCreateWithCString(NULL, filename, kCFStringEncodingUTF8);
|
|
|
+
|
|
|
+ url=CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0);
|
|
|
+
|
|
|
+ CGPDFDocumentRef document;
|
|
|
+ document = CGPDFDocumentCreateWithURL (url);
|
|
|
+ CFRelease(url);
|
|
|
+ size_t totalpage = CGPDFDocumentGetNumberOfPages (document);
|
|
|
+
|
|
|
+ if (totalpage == 0) {
|
|
|
+ printf("[%s] needs at least one page!\n", [@"myPDF.pdf" UTF8String] );
|
|
|
+ return NULL;
|
|
|
+ } else {
|
|
|
+ printf("[%ld] pages loaded in this PDF!\n", totalpage);
|
|
|
+
|
|
|
+
|
|
|
+ /*8
|
|
|
+ for (NSInteger pageNumber = 1; pageNumber <= count; pageNumber++)
|
|
|
+ {
|
|
|
+ CGPDFPageRef pageRef = CGPDFDocumentGetPage(document, pageNumber);
|
|
|
+
|
|
|
+ CGPDFDictionaryRef pageDictionaryFromPage = CGPDFPageGetDictionary(pageRef);
|
|
|
+
|
|
|
+ if (pageDictionaryFromPage == pageDictionaryFromDestArray) // Found it
|
|
|
+ {
|
|
|
+ targetPageNumber = pageNumber; break;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }*/
|
|
|
+ }
|
|
|
+
|
|
|
+ // CGPDFPageRef page= CGPDFDocumentGetPage(document, 0);
|
|
|
+ // CGContextDrawPDFPage(<#CGContextRef _Nullable c#>, <#CGPDFPageRef _Nullable page#>)
|
|
|
+
|
|
|
+ return document;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
++(void)SavePDF:(NSMutableDictionary*) controlTemplate source:(CGPDFDocumentRef )document
|
|
|
+{
|
|
|
+
|
|
|
+ //获取路径
|
|
|
+
|
|
|
+ NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);//坐标
|
|
|
+
|
|
|
+ NSString *saveDirectory=[paths objectAtIndex:0];
|
|
|
+
|
|
|
+ NSString *saveFileName=[NSString stringWithFormat:@"%@.pdf",[[NSUUID UUID ] UUIDString] ] ;;
|
|
|
+
|
|
|
+ NSString *newFilePath=[saveDirectory stringByAppendingPathComponent:saveFileName];
|
|
|
+
|
|
|
+ const char *filename=[newFilePath UTF8String];
|
|
|
+
|
|
|
+ NSLog(@"%@",newFilePath);
|
|
|
+ //设置页面大小 Letter纸
|
|
|
+
|
|
|
+ //CGPDFDocumentGetMediaBox(document,1);
|
|
|
+ CGPDFDictionaryRef pdf_dict= CGPDFDocumentGetInfo(document);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ //关联上下文的对象
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ CFStringRef path;
|
|
|
+
|
|
|
+ CFURLRef url;
|
|
|
+
|
|
|
+ path=CFStringCreateWithCString(NULL, filename, kCFStringEncodingUTF8);
|
|
|
+
|
|
|
+ url=CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0);
|
|
|
+
|
|
|
+ CFBridgingRelease(path);
|
|
|
+ // CGPDFCONTEXTCREATE
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ size_t page_count=CGPDFDocumentGetNumberOfPages(document);
|
|
|
+ for(int i=1;i<=page_count;i++)
|
|
|
+ {
|
|
|
+ // NSMutableDictionary* page=pages[[ NSString stringWithFormat:@"page_%d",i ] ];
|
|
|
+ CGPDFPageRef page= CGPDFDocumentGetPage (document , 1);
|
|
|
+
|
|
|
+ CGRect papersize=CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
|
|
|
+ CGContextRef pdfContext;
|
|
|
+ pdfContext=CGPDFContextCreateWithURL(url, &papersize, nil);
|
|
|
+
|
|
|
+
|
|
|
+ CGContextBeginPage(pdfContext, &papersize);
|
|
|
+
|
|
|
+ PDFPage* pdfPage = [[PDFPage alloc] init:nil size:papersize];
|
|
|
+
|
|
|
+ [pdfPage DirectDraw:pdfContext page:page];
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ NSMutableDictionary* tline1= [LineDrawable createlineTemplate:1 from:CGPointMake(20, 20) to:CGPointMake(500,500)];
|
|
|
+ LineDrawable* line1=nil;
|
|
|
+ line1= [[LineDrawable alloc] init:tline1];
|
|
|
+ NSRange range = NSMakeRange(0,9999);
|
|
|
+ [line1 Draw:pdfContext dataSource:nil ParentRect:papersize startX:0 startY:0 flipHeight:0 range:range];
|
|
|
+
|
|
|
+ CGContextEndPage(pdfContext);
|
|
|
+ CGContextRelease(pdfContext);
|
|
|
+ // [pdfPage Draw:pdfContext dataSource:nil];
|
|
|
+// [self drawPage:pdfContext template:page size:papersize dataSource:data];
|
|
|
+ }
|
|
|
+
|
|
|
+ CFBridgingRelease(url);
|
|
|
+// //开始画pdf
|
|
|
+//
|
|
|
+// // NSString *temtext=[[NSString alloc]init];
|
|
|
+// //
|
|
|
+// // const char *text=(char *)[temtext UTF8String];
|
|
|
+// //
|
|
|
+// // int width;
|
|
|
+// //
|
|
|
+// // int height;
|
|
|
+// //
|
|
|
+//
|
|
|
+// // [self newpage:pdfContext size:papersize];
|
|
|
+//
|
|
|
+//
|
|
|
+//
|
|
|
+// CGContextRelease(pdfContext);
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+@end
|