PDFUtils.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. //
  2. // PDFUtils.m
  3. // AntsContract
  4. //
  5. // Created by Ray on 12/20/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "PDFUtils.h"
  9. #import "PDFPage.h"
  10. #import "ImageDrawable.h"
  11. #import "TextDrawable.h"
  12. #import "RectDrawable.h"
  13. @implementation PDFUtils
  14. +(NSString*) addSignature :(UIImage*) img to:(NSMutableDictionary*)signatureData
  15. {
  16. if(true)
  17. {
  18. NSString* newfile=[self saveTempSignature:UIImagePNGRepresentation(img)];
  19. int newidx = [signatureData[@"count"] intValue];
  20. // [@"file"]= newfile;
  21. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  22. item[@"file"]=newfile;
  23. signatureData[[NSString stringWithFormat:@"item_%d",newidx]] = item;
  24. signatureData[@"count"]=[NSNumber numberWithInt:newidx+1];
  25. return newfile;
  26. }
  27. }
  28. +(NSString *) saveTempSignature:(NSData *) image
  29. {
  30. NSString* tempDir = NSTemporaryDirectory();
  31. NSString *saveFileName=[NSString stringWithFormat:@"%@.png",[[NSUUID UUID ] UUIDString] ] ;
  32. NSString *newFilePath=[tempDir stringByAppendingPathComponent:saveFileName];
  33. bool bsuccess=[image writeToFile:newFilePath atomically:YES];
  34. if(bsuccess)
  35. {
  36. return newFilePath;
  37. }
  38. else
  39. {
  40. return nil;
  41. }
  42. }
  43. +(NSMutableDictionary*) loadControl:(NSString*)templateName
  44. {
  45. NSString *path = [[NSBundle mainBundle] pathForResource:templateName ofType:nil];
  46. NSData *data = [NSData dataWithContentsOfFile:path];
  47. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  48. return ret;
  49. }
  50. +(CGPDFDocumentRef)OpenPDF:(NSString*) file
  51. {
  52. //获取路径
  53. /*
  54. NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);//坐标
  55. NSString *saveDirectory=[paths objectAtIndex:0];
  56. NSString *saveFileName=@"myPDF.pdf";
  57. NSString *newFilePath=[saveDirectory stringByAppendingPathComponent:saveFileName];
  58. */
  59. NSString *default_path = [[NSBundle mainBundle] pathForResource:file ofType:nil];
  60. const char *filename=[default_path UTF8String];
  61. NSLog(@"%@",default_path);
  62. //关联上下文的对象
  63. // CGContextRef pdfContext;
  64. CFStringRef path;
  65. CFURLRef url;
  66. path=CFStringCreateWithCString(NULL, filename, kCFStringEncodingUTF8);
  67. url=CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0);
  68. CFRelease(path);
  69. CGPDFDocumentRef document;
  70. document = CGPDFDocumentCreateWithURL (url);
  71. CFRelease(url);
  72. size_t totalpage = CGPDFDocumentGetNumberOfPages (document);
  73. if (totalpage == 0) {
  74. printf("[%s] needs at least one page!\n", [@"myPDF.pdf" UTF8String] );
  75. return NULL;
  76. } else {
  77. printf("[%ld] pages loaded in this PDF!\n", totalpage);
  78. /*8
  79. for (NSInteger pageNumber = 1; pageNumber <= count; pageNumber++)
  80. {
  81. CGPDFPageRef pageRef = CGPDFDocumentGetPage(document, pageNumber);
  82. CGPDFDictionaryRef pageDictionaryFromPage = CGPDFPageGetDictionary(pageRef);
  83. if (pageDictionaryFromPage == pageDictionaryFromDestArray) // Found it
  84. {
  85. targetPageNumber = pageNumber; break;
  86. }
  87. }*/
  88. }
  89. // CGPDFPageRef page= CGPDFDocumentGetPage(document, 0);
  90. // CGContextDrawPDFPage(<#CGContextRef _Nullable c#>, <#CGPDFPageRef _Nullable page#>)
  91. return document;
  92. }
  93. +(CGRect)WindowRect2PDFRect:(CGRect)rect pdf_rect:(CGRect)pdf_rect window_size:(CGSize)window_size
  94. {
  95. CGRect pdf_expand;
  96. float offset_x = 0;
  97. float offset_y = 0;
  98. float scale =1;
  99. if(pdf_rect.size.height>=pdf_rect.size.width)
  100. {
  101. pdf_expand = CGRectMake(0, 0, pdf_rect.size.height*window_size.width/window_size.height, pdf_rect.size.height);
  102. offset_x = (pdf_expand.size.width-pdf_rect.size.width)/2;
  103. scale = pdf_rect.size.height/window_size.height;
  104. }
  105. else
  106. {
  107. pdf_expand = CGRectMake(0, 0, pdf_rect.size.width, pdf_rect.size.width*window_size.height/window_size.width);
  108. offset_y = (pdf_expand.size.height-pdf_rect.size.height)/2;
  109. scale = pdf_rect.size.width/window_size.width;
  110. }
  111. float x=rect.origin.x*scale-offset_x;
  112. float y=rect.origin.y*scale-offset_y;
  113. float width = rect.size.width*scale;
  114. float height = rect.size.height*scale;
  115. return CGRectMake(x, y, width, height);
  116. }
  117. +(NSString*)SavePDF:(NSMutableDictionary*) controlTemplate source:(CGPDFDocumentRef )document window_rect:(CGRect)window_rect
  118. {
  119. //获取路径
  120. NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);//坐标
  121. NSString *saveDirectory=[paths objectAtIndex:0];
  122. NSString *saveFileName=[NSString stringWithFormat:@"%@.pdf",[[NSUUID UUID ] UUIDString] ] ;;
  123. NSString *newFilePath=[saveDirectory stringByAppendingPathComponent:saveFileName];
  124. const char *filename=[newFilePath UTF8String];
  125. NSLog(@"%@",newFilePath);
  126. //设置页面大小 Letter纸
  127. //CGPDFDocumentGetMediaBox(document,1);
  128. CGPDFDictionaryRef pdf_dict= CGPDFDocumentGetInfo(document);
  129. //关联上下文的对象
  130. CFStringRef path;
  131. CFURLRef url;
  132. path=CFStringCreateWithCString(NULL, filename, kCFStringEncodingUTF8);
  133. url=CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0);
  134. CFBridgingRelease(path);
  135. // CGPDFCONTEXTCREATE
  136. CGContextRef pdfContext;
  137. CGRect docsize = CGRectMake(0, 0, 100, 100);
  138. pdfContext=CGPDFContextCreateWithURL(url, &docsize, nil);
  139. size_t page_count=CGPDFDocumentGetNumberOfPages(document);
  140. for(int i=1;i<=page_count;i++)
  141. {
  142. NSMutableDictionary* page_control=controlTemplate[[NSString stringWithFormat:@"page_%d",i-1]];
  143. CGPDFPageRef page= CGPDFDocumentGetPage (document , i);
  144. CGRect papersize=CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
  145. CGContextBeginPage(pdfContext, &papersize);
  146. PDFPage* pdfPage = [[PDFPage alloc] init:nil size:papersize];
  147. [pdfPage DirectDraw:pdfContext page:page];
  148. for(int j=0;j<[page_control[@"count"] intValue];j++)
  149. {
  150. NSMutableDictionary* control=page_control[[NSString stringWithFormat:@"control_%d",j]];
  151. PDFDrawable * control_drawable = nil;
  152. CGRect control_rect = CGRectMake([control[@"pos_x"] floatValue], [control[@"pos_y"] floatValue], [control[@"width"] floatValue], [control[@"height"] floatValue]);
  153. NSString* control_type = control[@"type"];
  154. if([control_type isEqualToString:@"Signature"])
  155. {
  156. NSString* value = control[@"value"];
  157. if(value.length==0)
  158. continue;
  159. control_drawable=[[ImageDrawable alloc] init:[self WindowRect2PDFRect:control_rect pdf_rect:papersize window_size:window_rect.size] source_type:@"local" source:value hAlign:@"center" vAlign:@"middle"];
  160. }
  161. else if([control_type isEqualToString:@"TextView"])
  162. {
  163. NSString* value = control[@"value"];
  164. if(value.length==0)
  165. continue;
  166. NSString* size = control[@"size"];
  167. if(size.length==0)
  168. size=@"10";
  169. control_drawable=[[TextDrawable alloc] init:[self WindowRect2PDFRect:control_rect pdf_rect:papersize window_size:window_rect.size] source_type:@"const" source:value textAlignment:@"center" vAlign:@"middle" size:size];
  170. }
  171. else if([control_type isEqualToString:@"Check"])
  172. {
  173. NSArray* value = control[@"value"];
  174. if(value.count==0)
  175. continue;
  176. NSArray* cadedate = control[@"cadedate"];
  177. float marker_size = [control[@"marker_size"] floatValue];
  178. for(int c=0;c<value.count;c++)
  179. {
  180. NSArray* marker=cadedate[[value[c] intValue]];
  181. CGRect marker_rect= CGRectMake([marker[1][0] floatValue] ,[marker[1][1] floatValue], marker_size, marker_size);
  182. PDFDrawable * marker_drawable = [[RectDrawable alloc] init:[self WindowRect2PDFRect:marker_rect pdf_rect:papersize window_size:window_rect.size] color:@"0x000000"];
  183. if(marker_drawable)
  184. [pdfPage DirectDraw:pdfContext drawable:marker_drawable];
  185. }
  186. // NSString* size = control[@"size"];
  187. // if(size.length==0)
  188. // size=@"10";
  189. //
  190. // control_drawable=[[TextDrawable alloc] init:[self WindowRect2PDFRect:control_rect pdf_rect:papersize window_size:window_rect.size] source_type:@"const" source:value textAlignment:@"center" vAlign:@"middle" size:size];
  191. }
  192. if(control_drawable)
  193. [pdfPage DirectDraw:pdfContext drawable:control_drawable];
  194. }
  195. // NSMutableDictionary* tline1= [LineDrawable createlineTemplate:1 from:CGPointMake(20, 20) to:CGPointMake(500,500)];
  196. // LineDrawable* line1=nil;
  197. // line1= [[LineDrawable alloc] init:tline1];
  198. //
  199. // [pdfPage DirectDraw:pdfContext drawable:line1];
  200. // NSRange range = NSMakeRange(0,9999);
  201. // [line1 Draw:pdfContext dataSource:nil ParentRect:papersize startX:0 startY:0 flipHeight:0 range:range];
  202. CGContextEndPage(pdfContext);
  203. // [pdfPage Draw:pdfContext dataSource:nil];
  204. // [self drawPage:pdfContext template:page size:papersize dataSource:data];
  205. // CFBridgingRelease(page);
  206. }
  207. CGContextRelease(pdfContext);
  208. CFBridgingRelease(url);
  209. // //开始画pdf
  210. //
  211. // // NSString *temtext=[[NSString alloc]init];
  212. // //
  213. // // const char *text=(char *)[temtext UTF8String];
  214. // //
  215. // // int width;
  216. // //
  217. // // int height;
  218. // //
  219. //
  220. // // [self newpage:pdfContext size:papersize];
  221. //
  222. //
  223. //
  224. // CGContextRelease(pdfContext);
  225. return newFilePath;
  226. }
  227. @end