PDFUtils.m 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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 subType:(NSString*)subtype
  15. {
  16. if(true)
  17. {
  18. NSString* newfile=[self saveTempSignature:UIImagePNGRepresentation(img)];
  19. int newidx = [signatureData[subtype][@"count"] intValue];
  20. // [@"file"]= newfile;
  21. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  22. item[@"file"]=newfile;
  23. NSMutableDictionary* subjson =signatureData[subtype];
  24. if(subjson==nil)
  25. subjson = [[NSMutableDictionary alloc] init];
  26. subjson[[NSString stringWithFormat:@"item_%d",newidx]] = item;
  27. subjson[@"count"]=[NSNumber numberWithInt:newidx+1];
  28. signatureData[subtype]=subjson;
  29. return newfile;
  30. }
  31. }
  32. +(NSString *) saveTempSignature:(NSData *) image
  33. {
  34. NSString* tempDir = NSTemporaryDirectory();
  35. NSString *saveFileName=[NSString stringWithFormat:@"%@.png",[[NSUUID UUID ] UUIDString] ] ;
  36. NSString *newFilePath=[tempDir stringByAppendingPathComponent:saveFileName];
  37. bool bsuccess=[image writeToFile:newFilePath atomically:YES];
  38. if(bsuccess)
  39. {
  40. return newFilePath;
  41. }
  42. else
  43. {
  44. return nil;
  45. }
  46. }
  47. +(NSMutableDictionary*) loadControl:(NSString*)path
  48. {
  49. // NSString *path = [[NSBundle mainBundle] pathForResource:templateName ofType:nil];
  50. NSData *data = [NSData dataWithContentsOfFile:path];
  51. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  52. return ret;
  53. }
  54. +(CGSize) QueryPDFSizeFromData:(NSData *)pdfData
  55. {
  56. CGPDFDocumentRef document = [self OpenPDFFromData:pdfData];
  57. CGRect rect= [self QuerySize:document];
  58. CFRelease(document);
  59. return rect.size;
  60. }
  61. +(CGRect) QuerySize:(CGPDFDocumentRef)document
  62. {
  63. CGPDFPageRef page= CGPDFDocumentGetPage (document , 1);
  64. // CGPDFPageRef page1= CGPDFDocumentGetPage (document , i);
  65. CGRect papersize=CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
  66. return papersize;
  67. }
  68. + (CGPDFDocumentRef)OpenPDFFromData:(NSData *)pdfData {
  69. CFDataRef dataRef = (__bridge_retained CFDataRef)(pdfData);
  70. CGDataProviderRef proRef = CGDataProviderCreateWithCFData(dataRef);
  71. CGPDFDocumentRef pdfRef = CGPDFDocumentCreateWithProvider(proRef);
  72. CGDataProviderRelease(proRef);
  73. CFRelease(dataRef);
  74. return pdfRef;
  75. }
  76. +(CGPDFDocumentRef)OpenPDF:(NSString*) file
  77. {
  78. //获取路径
  79. /*
  80. NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);//坐标
  81. NSString *saveDirectory=[paths objectAtIndex:0];
  82. NSString *saveFileName=@"myPDF.pdf";
  83. NSString *newFilePath=[saveDirectory stringByAppendingPathComponent:saveFileName];
  84. */
  85. NSString *default_path = file;//[[NSBundle mainBundle] pathForResource:file ofType:nil];
  86. // const char *filename=[default_path UTF8String];
  87. NSLog(@"%@",default_path);
  88. //关联上下文的对象
  89. // CGContextRef pdfContext;
  90. // CFStringRef path;
  91. //
  92. // CFURLRef url;
  93. //
  94. // path=CFStringCreateWithCString(NULL, filename, kCFStringEncodingUTF8);
  95. //
  96. // url=CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0);
  97. // CFRelease(path);
  98. CGPDFDocumentRef document;
  99. CFURLRef pdfURL = (__bridge CFURLRef)[NSURL fileURLWithPath:default_path];
  100. // CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL(pdfURL);
  101. document = CGPDFDocumentCreateWithURL (pdfURL);
  102. // CFRelease(url);
  103. // size_t totalpage = CGPDFDocumentGetNumberOfPages (document);
  104. //
  105. // if (totalpage == 0) {
  106. // printf("[%s] needs at least one page!\n", [@"myPDF.pdf" UTF8String] );
  107. // return NULL;
  108. // } else {
  109. // printf("[%ld] pages loaded in this PDF!\n", totalpage);
  110. //
  111. //
  112. // /*8
  113. // for (NSInteger pageNumber = 1; pageNumber <= count; pageNumber++)
  114. // {
  115. // CGPDFPageRef pageRef = CGPDFDocumentGetPage(document, pageNumber);
  116. //
  117. // CGPDFDictionaryRef pageDictionaryFromPage = CGPDFPageGetDictionary(pageRef);
  118. //
  119. // if (pageDictionaryFromPage == pageDictionaryFromDestArray) // Found it
  120. // {
  121. // targetPageNumber = pageNumber; break;
  122. // }
  123. //
  124. //
  125. // }*/
  126. // }
  127. // CGPDFPageRef page= CGPDFDocumentGetPage(document, 0);
  128. // CGContextDrawPDFPage(<#CGContextRef _Nullable c#>, <#CGPDFPageRef _Nullable page#>)
  129. return document;
  130. }
  131. +(CGRect)WindowRect2PDFRect:(CGRect)rect pdf_rect:(CGRect)pdf_rect window_size:(CGSize)window_size
  132. {
  133. CGRect pdf_expand;
  134. float offset_x = 0;
  135. float offset_y = 0;
  136. float scale =1;
  137. if(pdf_rect.size.height>=pdf_rect.size.width)
  138. {
  139. pdf_expand = CGRectMake(0, 0, pdf_rect.size.height*window_size.width/window_size.height, pdf_rect.size.height);
  140. offset_x = (pdf_expand.size.width-pdf_rect.size.width)/2;
  141. scale = pdf_rect.size.height/window_size.height;
  142. }
  143. else
  144. {
  145. pdf_expand = CGRectMake(0, 0, pdf_rect.size.width, pdf_rect.size.width*window_size.height/window_size.width);
  146. offset_y = (pdf_expand.size.height-pdf_rect.size.height)/2;
  147. scale = pdf_rect.size.width/window_size.width;
  148. }
  149. float x=rect.origin.x*scale-offset_x;
  150. float y=rect.origin.y*scale-offset_y;
  151. float width = rect.size.width*scale;
  152. float height = rect.size.height*scale;
  153. return CGRectMake(x, y, width, height);
  154. }
  155. +(CGRect)PDFRect2WindowRect:(CGRect)rect pdf_rect:(CGRect)pdf_rect window_size:(CGSize)window_size
  156. {
  157. CGRect win_expand;
  158. float offset_x = 0;
  159. float offset_y = 0;
  160. float scale =1;
  161. if(pdf_rect.size.height>=pdf_rect.size.width)
  162. {
  163. win_expand = CGRectMake(window_size.width/2-(window_size.height*pdf_rect.size.width/pdf_rect.size.height)/2, 0, (window_size.height*pdf_rect.size.width/pdf_rect.size.height), window_size.height);
  164. offset_x = (win_expand.size.width-window_size.width)/2;
  165. scale = window_size.height/pdf_rect.size.height;
  166. }
  167. else
  168. {
  169. win_expand = CGRectMake(0, window_size.height/2-(window_size.width*pdf_rect.size.height/pdf_rect.size.width)/2, window_size.width, (window_size.width*pdf_rect.size.height/pdf_rect.size.width));
  170. offset_y = (win_expand.size.height-window_size.height)/2;
  171. scale = window_size.width/pdf_rect.size.width;
  172. }
  173. float x=rect.origin.x*scale-offset_x;
  174. float y=rect.origin.y*scale-offset_y;
  175. float width = rect.size.width*scale;
  176. float height = rect.size.height*scale;
  177. return CGRectMake(x, y, width, height);
  178. }
  179. +(NSString*)SavePDF:(NSMutableDictionary*) controlTemplate source:(CGPDFDocumentRef )document window_rect:(CGRect)window_rect name:(NSString*)name
  180. isnew:(bool)isNewDocument
  181. {
  182. //获取路径
  183. NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);//坐标
  184. NSString *saveDirectory=[paths objectAtIndex:0];
  185. if(name==nil)
  186. name=[[NSUUID UUID ] UUIDString];
  187. NSString *saveFileName=[NSString stringWithFormat:@"%@.pdf",name ] ;
  188. NSString *newFilePath=[saveDirectory stringByAppendingPathComponent:saveFileName];
  189. const char *filename=[newFilePath UTF8String];
  190. NSLog(@"%@",newFilePath);
  191. //设置页面大小 Letter纸
  192. //CGPDFDocumentGetMediaBox(document,1);
  193. // CGPDFDictionaryRef pdf_dict= CGPDFDocumentGetInfo(document);
  194. //关联上下文的对象
  195. CFStringRef path;
  196. CFURLRef url;
  197. path=CFStringCreateWithCString(NULL, filename, kCFStringEncodingUTF8);
  198. url=CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0);
  199. CFRelease(path);
  200. // CGPDFCONTEXTCREATE
  201. CGContextRef pdfContext;
  202. CGRect docsize = CGRectMake(0, 0, 612, 1008);
  203. // NSDictionary *PDFAttributes =
  204. // [NSDictionary dictionaryWithObjectsAndKeys:
  205. // @"usai2010", (NSString *)kCGPDFContextOwnerPassword,
  206. // kCFBooleanTrue, (NSString *)kCGPDFContextAllowsPrinting,
  207. // kCFBooleanFalse, (NSString *)kCGPDFContextAllowsCopying,
  208. // nil];
  209. // // Create a descriptor.
  210. // CFDictionaryRef cfpdfAttributes =(__bridge_retained CFDictionaryRef)PDFAttributes;
  211. // CFBridgingRelease(cfpdfAttributes);
  212. CFMutableDictionaryRef myDictionary = NULL;
  213. myDictionary= CFDictionaryCreateMutable(NULL,
  214. 0,
  215. NULL,
  216. NULL);
  217. // CFDictionarySetValue(myDictionary,
  218. // kCGPDFContextTitle,
  219. // CFSTR("Photo from iPrivate Album"));
  220. // CFDictionarySetValue(myDictionary,
  221. // kCGPDFContextCreator,
  222. // CFSTR("iPrivate Album"));
  223. // //设置文档名称
  224. // CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
  225. // //设置创建者
  226. // CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
  227. //设置文档尺寸
  228. // CFDictionarySetValue(myDictionary, kCGPDFContextOwnerPassword, CFNUMBERCREATE (40));
  229. CFDictionarySetValue(myDictionary, kCGPDFContextOwnerPassword, CFSTR("usai2010"));
  230. // CFDictionarySetValue(myDictionary, kCGPDFContextUserPassword, CFSTR("usai2010"));
  231. CFDictionarySetValue(myDictionary, kCGPDFContextAllowsPrinting, kCFBooleanTrue);
  232. CFDictionarySetValue(myDictionary, kCGPDFContextAllowsCopying, kCFBooleanFalse);
  233. //
  234. pdfContext=CGPDFContextCreateWithURL(url, &docsize,myDictionary);
  235. CFRelease(myDictionary);
  236. CFRelease(url);
  237. size_t page_count=CGPDFDocumentGetNumberOfPages(document);
  238. for(int i=1;i<=page_count;i++)
  239. {
  240. int p = (i+4)%5+1;
  241. NSMutableDictionary* page_control=controlTemplate[[NSString stringWithFormat:@"page_%d",p-1]];
  242. CGPDFPageRef page= CGPDFDocumentGetPage (document , p);
  243. // CGPDFPageRef page1= CGPDFDocumentGetPage (document , i);
  244. CGRect papersize=CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
  245. CGContextBeginPage(pdfContext, &papersize);
  246. PDFPage* pdfPage = [[PDFPage alloc] init:nil size:papersize];
  247. [pdfPage DirectDraw:pdfContext page:page];
  248. for(int j=0;j<[page_control[@"count"] intValue];j++)
  249. {
  250. NSMutableDictionary* control=page_control[[NSString stringWithFormat:@"control_%d",j]];
  251. PDFDrawable * control_drawable = nil;
  252. CGRect control_rect = CGRectMake([control[@"pos_x"] floatValue], [control[@"pos_y"] floatValue], [control[@"width"] floatValue], [control[@"height"] floatValue]);
  253. NSString* control_type = control[@"type"];
  254. if([control_type isEqualToString:@"Signature"]||[control_type isEqualToString:@"Image"])
  255. {
  256. NSString* value = control[@"value"];
  257. if(value.length==0)
  258. continue;
  259. 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"];
  260. }
  261. else if([control_type isEqualToString:@"TextView"]||[control_type isEqualToString:@"Label"]||[control_type isEqualToString:@"DatePicker"])
  262. {
  263. NSString* value = control[@"value"];
  264. if(value.length==0)
  265. continue;
  266. NSString* size = control[@"size"];
  267. if(size.length==0)
  268. size=@"10";
  269. NSString* textAlignment=control[@"textAlignment"];
  270. if(textAlignment.length==0)
  271. textAlignment = @"Left";
  272. double margin_left = [control[@"margin_left"] doubleValue];
  273. double margin_top = [control[@"margin_top"] doubleValue];
  274. CGRect text_rect = [self WindowRect2PDFRect:control_rect pdf_rect:papersize window_size:window_rect.size];
  275. control_drawable=[[TextDrawable alloc] init:text_rect source_type:@"const" source:value textAlignment:textAlignment vAlign:@"middle" size:size];
  276. [control_drawable setMargin_left:margin_left];
  277. [control_drawable setMargin_top:margin_top];
  278. if(!isNewDocument)
  279. {
  280. CGRect bg_rect= text_rect;
  281. PDFDrawable * rect_drawable = [[RectDrawable alloc] init:bg_rect color:@"0xffcc66"];
  282. if(rect_drawable)
  283. [pdfPage DirectDraw:pdfContext drawable:rect_drawable];
  284. }
  285. }
  286. else if([control_type isEqualToString:@"Check"])
  287. {
  288. NSArray* value = control[@"value"];
  289. if(value.count==0)
  290. continue;
  291. NSArray* cadedate = control[@"cadedate"];
  292. float marker_size = [control[@"marker_size"] floatValue];
  293. for(int c=0;c<value.count;c++)
  294. {
  295. NSArray* marker=cadedate[[value[c] intValue]];
  296. if(marker.count>=3)
  297. {
  298. NSString* value = marker[0][0];
  299. if(value.length==0)
  300. continue;
  301. NSString* size = control[@"size"];
  302. if(size.length==0)
  303. size=@"10";
  304. 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];
  305. }
  306. else
  307. {
  308. CGRect marker_rect= CGRectMake([marker[1][0] floatValue] ,[marker[1][1] floatValue], marker_size, marker_size);
  309. PDFDrawable * marker_drawable = [[RectDrawable alloc] init:[self WindowRect2PDFRect:marker_rect pdf_rect:papersize window_size:window_rect.size] color:@"0x000000"];
  310. if(marker_drawable)
  311. [pdfPage DirectDraw:pdfContext drawable:marker_drawable];
  312. }
  313. }
  314. // NSString* size = control[@"size"];
  315. // if(size.length==0)
  316. // size=@"10";
  317. //
  318. // 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];
  319. }
  320. if(control_drawable)
  321. [pdfPage DirectDraw:pdfContext drawable:control_drawable];
  322. }
  323. // NSMutableDictionary* tline1= [LineDrawable createlineTemplate:1 from:CGPointMake(20, 20) to:CGPointMake(500,500)];
  324. // LineDrawable* line1=nil;
  325. // line1= [[LineDrawable alloc] init:tline1];
  326. //
  327. // [pdfPage DirectDraw:pdfContext drawable:line1];
  328. // NSRange range = NSMakeRange(0,9999);
  329. // [line1 Draw:pdfContext dataSource:nil ParentRect:papersize startX:0 startY:0 flipHeight:0 range:range];
  330. CGContextEndPage(pdfContext);
  331. // CGPDFPageRelease(page);
  332. // [pdfPage Draw:pdfContext dataSource:nil];
  333. // [self drawPage:pdfContext template:page size:papersize dataSource:data];
  334. // CFBridgingRelease(page);
  335. }
  336. CGPDFContextClose(pdfContext);
  337. CGContextRelease(pdfContext);
  338. // CFRelease(cfpdfAttributes);
  339. // //开始画pdf
  340. //
  341. // // NSString *temtext=[[NSString alloc]init];
  342. // //
  343. // // const char *text=(char *)[temtext UTF8String];
  344. // //
  345. // // int width;
  346. // //
  347. // // int height;
  348. // //
  349. //
  350. // // [self newpage:pdfContext size:papersize];
  351. //
  352. //
  353. //
  354. // CGContextRelease(pdfContext);
  355. return newFilePath;
  356. }
  357. //{
  358. //
  359. // //获取路径
  360. //
  361. // NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);//坐标
  362. //
  363. // NSString *saveDirectory=[paths objectAtIndex:0];
  364. //
  365. // NSString *saveFileName=[NSString stringWithFormat:@"%@.pdf",[[NSUUID UUID ] UUIDString] ] ;;
  366. //
  367. // NSString *newFilePath=[saveDirectory stringByAppendingPathComponent:saveFileName];
  368. //
  369. // const char *filename=[newFilePath UTF8String];
  370. //
  371. // NSLog(@"%@",newFilePath);
  372. // //设置页面大小 Letter纸
  373. //
  374. // //CGPDFDocumentGetMediaBox(document,1);
  375. // // CGPDFDictionaryRef pdf_dict= CGPDFDocumentGetInfo(document);
  376. //
  377. //
  378. //
  379. //
  380. //
  381. // //关联上下文的对象
  382. //
  383. //
  384. //
  385. // CFStringRef path;
  386. //
  387. // CFURLRef url;
  388. //
  389. // path=CFStringCreateWithCString(NULL, filename, kCFStringEncodingUTF8);
  390. //
  391. // url=CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0);
  392. //
  393. // CFRelease(path);
  394. // // CGPDFCONTEXTCREATE
  395. //
  396. //
  397. // CGContextRef pdfContext;
  398. // CGRect docsize = CGRectMake(0, 0, 100, 100);
  399. //
  400. //
  401. //// if(name.length==0)
  402. //// {
  403. //// name=@"Helvetica";
  404. //// // name=@"DejaVu Sans";
  405. //// }
  406. //// if(size==0)
  407. //// {
  408. //// size=10;
  409. //// }
  410. //// NSString* style = @"Regular";
  411. //// if(bold)
  412. //// style=@"Bold";
  413. ////
  414. //// //name=@"Courier";
  415. //
  416. //// NSDictionary *PDFAttributes =
  417. //// [NSDictionary dictionaryWithObjectsAndKeys:
  418. //// @"usai2010", (NSString *)kCGPDFContextOwnerPassword,
  419. //// kCFBooleanTrue, (NSString *)kCGPDFContextAllowsPrinting,
  420. //// kCFBooleanFalse, (NSString *)kCGPDFContextAllowsCopying,
  421. //// nil];
  422. //// // Create a descriptor.
  423. //// CFDictionaryRef cfpdfAttributes =(__bridge CFDictionaryRef)(PDFAttributes);
  424. ////
  425. ////
  426. //// // CFBridgingRelease(cfpdfAttributes);
  427. //// pdfContext=CGPDFContextCreateWithURL(url, &docsize,cfpdfAttributes);
  428. //
  429. //
  430. //
  431. // CFMutableDictionaryRef myDictionary = NULL;
  432. // //文档信息字典
  433. //// myDictionary = CFDictionaryCreateMutable(NULL, 0,
  434. //// &kCFTypeDictionaryKeyCallBacks,
  435. //// &kCFTypeDictionaryValueCallBacks);
  436. ////
  437. ////// CFStringRef password = (__bridge CFStringRef)@"usai2010";
  438. ////// CFDictionarySetValue(myDictionary, kCGPDFContextOwnerPassword, password);
  439. ////
  440. //// CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
  441. ////
  442. //////
  443. //// CFDictionarySetValue(myDictionary, kCGPDFContextOwnerPassword, CFSTR("usai2010"));
  444. //////
  445. ////
  446. //// CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
  447. //// CFDictionarySetValue(myDictionary, kCGPDFContextAllowsPrinting, kCFBooleanTrue);
  448. //// CFDictionarySetValue(myDictionary, kCGPDFContextAllowsCopying, kCFBooleanFalse);
  449. //
  450. // //创建文档
  451. // pdfContext = CGPDFContextCreateWithURL (url, &docsize, NULL);
  452. //
  453. //
  454. //
  455. // size_t page_count=CGPDFDocumentGetNumberOfPages(document);
  456. // for(int i=1;i<=page_count;i++)
  457. // {
  458. // NSMutableDictionary* page_control=controlTemplate[[NSString stringWithFormat:@"page_%d",i-1]];
  459. // CGPDFPageRef page= CGPDFDocumentGetPage (document , i);
  460. //
  461. // CGRect papersize=CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
  462. //
  463. //
  464. //
  465. // CGContextBeginPage(pdfContext, &papersize);
  466. //
  467. // PDFPage* pdfPage = [[PDFPage alloc] init:nil size:papersize];
  468. //
  469. // [pdfPage DirectDraw:pdfContext page:page];
  470. //
  471. //
  472. // for(int j=0;j<[page_control[@"count"] intValue];j++)
  473. // {
  474. // NSMutableDictionary* control=page_control[[NSString stringWithFormat:@"control_%d",j]];
  475. //
  476. // PDFDrawable * control_drawable = nil;
  477. // CGRect control_rect = CGRectMake([control[@"pos_x"] floatValue], [control[@"pos_y"] floatValue], [control[@"width"] floatValue], [control[@"height"] floatValue]);
  478. //
  479. //
  480. // NSString* control_type = control[@"type"];
  481. // if([control_type isEqualToString:@"Signature"]||[control_type isEqualToString:@"Image"])
  482. // {
  483. //
  484. // NSString* value = control[@"value"];
  485. // if(value.length==0)
  486. // continue;
  487. //
  488. //
  489. // 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"];
  490. // }
  491. // else if([control_type isEqualToString:@"TextView"]||[control_type isEqualToString:@"Label"]||[control_type isEqualToString:@"DatePicker"])
  492. // {
  493. //
  494. // NSString* value = control[@"value"];
  495. // if(value.length==0)
  496. // continue;
  497. // NSString* size = control[@"size"];
  498. // if(size.length==0)
  499. // size=@"10";
  500. // NSString* textAlignment=control[@"textAlignment"];
  501. // if(textAlignment.length==0)
  502. // textAlignment = @"Center";
  503. // double margin_left = [control[@"margin_left"] doubleValue];
  504. //
  505. //
  506. // control_drawable=[[TextDrawable alloc] init:[self WindowRect2PDFRect:control_rect pdf_rect:papersize window_size:window_rect.size] source_type:@"const" source:value textAlignment:textAlignment vAlign:@"middle" size:size];
  507. //
  508. // [control_drawable setMargin_left:margin_left];
  509. // }
  510. // else if([control_type isEqualToString:@"Check"])
  511. // {
  512. // NSArray* value = control[@"value"];
  513. // if(value.count==0)
  514. // continue;
  515. // NSArray* cadedate = control[@"cadedate"];
  516. // float marker_size = [control[@"marker_size"] floatValue];
  517. //
  518. // for(int c=0;c<value.count;c++)
  519. // {
  520. // NSArray* marker=cadedate[[value[c] intValue]];
  521. // if(marker.count>=3)
  522. // {
  523. // NSString* value = marker[0][0];
  524. // if(value.length==0)
  525. // continue;
  526. // NSString* size = control[@"size"];
  527. // if(size.length==0)
  528. // size=@"10";
  529. //
  530. // 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];
  531. // }
  532. // else
  533. // {
  534. // CGRect marker_rect= CGRectMake([marker[1][0] floatValue] ,[marker[1][1] floatValue], marker_size, marker_size);
  535. // PDFDrawable * marker_drawable = [[RectDrawable alloc] init:[self WindowRect2PDFRect:marker_rect pdf_rect:papersize window_size:window_rect.size] color:@"0x000000"];
  536. // if(marker_drawable)
  537. // [pdfPage DirectDraw:pdfContext drawable:marker_drawable];
  538. // }
  539. //
  540. // }
  541. //
  542. //// NSString* size = control[@"size"];
  543. //// if(size.length==0)
  544. //// size=@"10";
  545. ////
  546. //// 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];
  547. // }
  548. // if(control_drawable)
  549. //// for(int l=0;l<100;l++)
  550. // [pdfPage DirectDraw:pdfContext drawable:control_drawable];
  551. //
  552. //
  553. //
  554. // }
  555. //// NSMutableDictionary* tline1= [LineDrawable createlineTemplate:1 from:CGPointMake(20, 20) to:CGPointMake(500,500)];
  556. //// LineDrawable* line1=nil;
  557. //// line1= [[LineDrawable alloc] init:tline1];
  558. ////
  559. //// [pdfPage DirectDraw:pdfContext drawable:line1];
  560. //// NSRange range = NSMakeRange(0,9999);
  561. //// [line1 Draw:pdfContext dataSource:nil ParentRect:papersize startX:0 startY:0 flipHeight:0 range:range];
  562. //
  563. // CGContextEndPage(pdfContext);
  564. //
  565. // // [pdfPage Draw:pdfContext dataSource:nil];
  566. //// [self drawPage:pdfContext template:page size:papersize dataSource:data];
  567. //
  568. //
  569. //// CFBridgingRelease(page);
  570. // }
  571. // CGContextRelease(pdfContext);
  572. // CFRelease(url);
  573. // // CFRelease(myDictionary);
  574. //
  575. //
  576. // // CFRelease(cfpdfAttributes);
  577. //// //开始画pdf
  578. ////
  579. //// // NSString *temtext=[[NSString alloc]init];
  580. //// //
  581. //// // const char *text=(char *)[temtext UTF8String];
  582. //// //
  583. //// // int width;
  584. //// //
  585. //// // int height;
  586. //// //
  587. ////
  588. //// // [self newpage:pdfContext size:papersize];
  589. ////
  590. ////
  591. ////
  592. //// CGContextRelease(pdfContext);
  593. //
  594. // return newFilePath;
  595. //
  596. //}
  597. @end