PDFUtils.m 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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 "RAPDFPage.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. RAPDFPage* pdfPage = [[RAPDFPage 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:@"TouchLabel"]||[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&& [control[@"dirty"] boolValue])
  279. {
  280. CGRect bg_rect= text_rect;
  281. PDFDrawable * rect_drawable = [[RectDrawable alloc] init:bg_rect color:@"0xffff00"];
  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. //NSArray* item = rowData[i];
  297. NSArray* checkmarker = marker[1];
  298. if(checkmarker.count==0/*marker.count>=3*/)
  299. {
  300. NSString* value = marker[0][0];
  301. if(value.length==0)
  302. continue;
  303. NSString* size = control[@"size"];
  304. if(size.length==0)
  305. size=@"10";
  306. 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];
  307. }
  308. else
  309. {
  310. CGRect marker_rect= CGRectMake([marker[1][0] floatValue] ,[marker[1][1] floatValue], marker_size, marker_size);
  311. PDFDrawable * marker_drawable = [[RectDrawable alloc] init:[self WindowRect2PDFRect:marker_rect pdf_rect:papersize window_size:window_rect.size] color:@"0x000000"];
  312. if(marker_drawable)
  313. [pdfPage DirectDraw:pdfContext drawable:marker_drawable];
  314. }
  315. }
  316. if(!isNewDocument&& [control[@"dirty"] boolValue])
  317. {
  318. CGRect text_rect = [self WindowRect2PDFRect:control_rect pdf_rect:papersize window_size:window_rect.size];
  319. CGRect bg_rect= text_rect;
  320. PDFDrawable * rect_drawable = nil;
  321. if([control_type isEqualToString:@"Check"])
  322. {
  323. rect_drawable = [[RectDrawable alloc] init:bg_rect color:@"0xffff00" alpha:0.2];
  324. }
  325. else
  326. {
  327. rect_drawable = [[RectDrawable alloc] init:bg_rect color:@"0xffff00"];
  328. }
  329. if(rect_drawable)
  330. [pdfPage DirectDraw:pdfContext drawable:rect_drawable];
  331. }
  332. // NSString* size = control[@"size"];
  333. // if(size.length==0)
  334. // size=@"10";
  335. //
  336. // 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];
  337. }
  338. if(control_drawable)
  339. [pdfPage DirectDraw:pdfContext drawable:control_drawable];
  340. }
  341. // NSMutableDictionary* tline1= [LineDrawable createlineTemplate:1 from:CGPointMake(20, 20) to:CGPointMake(500,500)];
  342. // LineDrawable* line1=nil;
  343. // line1= [[LineDrawable alloc] init:tline1];
  344. //
  345. // [pdfPage DirectDraw:pdfContext drawable:line1];
  346. // NSRange range = NSMakeRange(0,9999);
  347. // [line1 Draw:pdfContext dataSource:nil ParentRect:papersize startX:0 startY:0 flipHeight:0 range:range];
  348. CGContextEndPage(pdfContext);
  349. // CGPDFPageRelease(page);
  350. // [pdfPage Draw:pdfContext dataSource:nil];
  351. // [self drawPage:pdfContext template:page size:papersize dataSource:data];
  352. // CFBridgingRelease(page);
  353. }
  354. CGPDFContextClose(pdfContext);
  355. CGContextRelease(pdfContext);
  356. // CFRelease(cfpdfAttributes);
  357. // //开始画pdf
  358. //
  359. // // NSString *temtext=[[NSString alloc]init];
  360. // //
  361. // // const char *text=(char *)[temtext UTF8String];
  362. // //
  363. // // int width;
  364. // //
  365. // // int height;
  366. // //
  367. //
  368. // // [self newpage:pdfContext size:papersize];
  369. //
  370. //
  371. //
  372. // CGContextRelease(pdfContext);
  373. return newFilePath;
  374. }
  375. //{
  376. //
  377. // //获取路径
  378. //
  379. // NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);//坐标
  380. //
  381. // NSString *saveDirectory=[paths objectAtIndex:0];
  382. //
  383. // NSString *saveFileName=[NSString stringWithFormat:@"%@.pdf",[[NSUUID UUID ] UUIDString] ] ;;
  384. //
  385. // NSString *newFilePath=[saveDirectory stringByAppendingPathComponent:saveFileName];
  386. //
  387. // const char *filename=[newFilePath UTF8String];
  388. //
  389. // NSLog(@"%@",newFilePath);
  390. // //设置页面大小 Letter纸
  391. //
  392. // //CGPDFDocumentGetMediaBox(document,1);
  393. // // CGPDFDictionaryRef pdf_dict= CGPDFDocumentGetInfo(document);
  394. //
  395. //
  396. //
  397. //
  398. //
  399. // //关联上下文的对象
  400. //
  401. //
  402. //
  403. // CFStringRef path;
  404. //
  405. // CFURLRef url;
  406. //
  407. // path=CFStringCreateWithCString(NULL, filename, kCFStringEncodingUTF8);
  408. //
  409. // url=CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0);
  410. //
  411. // CFRelease(path);
  412. // // CGPDFCONTEXTCREATE
  413. //
  414. //
  415. // CGContextRef pdfContext;
  416. // CGRect docsize = CGRectMake(0, 0, 100, 100);
  417. //
  418. //
  419. //// if(name.length==0)
  420. //// {
  421. //// name=@"Helvetica";
  422. //// // name=@"DejaVu Sans";
  423. //// }
  424. //// if(size==0)
  425. //// {
  426. //// size=10;
  427. //// }
  428. //// NSString* style = @"Regular";
  429. //// if(bold)
  430. //// style=@"Bold";
  431. ////
  432. //// //name=@"Courier";
  433. //
  434. //// NSDictionary *PDFAttributes =
  435. //// [NSDictionary dictionaryWithObjectsAndKeys:
  436. //// @"usai2010", (NSString *)kCGPDFContextOwnerPassword,
  437. //// kCFBooleanTrue, (NSString *)kCGPDFContextAllowsPrinting,
  438. //// kCFBooleanFalse, (NSString *)kCGPDFContextAllowsCopying,
  439. //// nil];
  440. //// // Create a descriptor.
  441. //// CFDictionaryRef cfpdfAttributes =(__bridge CFDictionaryRef)(PDFAttributes);
  442. ////
  443. ////
  444. //// // CFBridgingRelease(cfpdfAttributes);
  445. //// pdfContext=CGPDFContextCreateWithURL(url, &docsize,cfpdfAttributes);
  446. //
  447. //
  448. //
  449. // CFMutableDictionaryRef myDictionary = NULL;
  450. // //文档信息字典
  451. //// myDictionary = CFDictionaryCreateMutable(NULL, 0,
  452. //// &kCFTypeDictionaryKeyCallBacks,
  453. //// &kCFTypeDictionaryValueCallBacks);
  454. ////
  455. ////// CFStringRef password = (__bridge CFStringRef)@"usai2010";
  456. ////// CFDictionarySetValue(myDictionary, kCGPDFContextOwnerPassword, password);
  457. ////
  458. //// CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
  459. ////
  460. //////
  461. //// CFDictionarySetValue(myDictionary, kCGPDFContextOwnerPassword, CFSTR("usai2010"));
  462. //////
  463. ////
  464. //// CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
  465. //// CFDictionarySetValue(myDictionary, kCGPDFContextAllowsPrinting, kCFBooleanTrue);
  466. //// CFDictionarySetValue(myDictionary, kCGPDFContextAllowsCopying, kCFBooleanFalse);
  467. //
  468. // //创建文档
  469. // pdfContext = CGPDFContextCreateWithURL (url, &docsize, NULL);
  470. //
  471. //
  472. //
  473. // size_t page_count=CGPDFDocumentGetNumberOfPages(document);
  474. // for(int i=1;i<=page_count;i++)
  475. // {
  476. // NSMutableDictionary* page_control=controlTemplate[[NSString stringWithFormat:@"page_%d",i-1]];
  477. // CGPDFPageRef page= CGPDFDocumentGetPage (document , i);
  478. //
  479. // CGRect papersize=CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
  480. //
  481. //
  482. //
  483. // CGContextBeginPage(pdfContext, &papersize);
  484. //
  485. // RAPDFPage* pdfPage = [[RAPDFPage alloc] init:nil size:papersize];
  486. //
  487. // [pdfPage DirectDraw:pdfContext page:page];
  488. //
  489. //
  490. // for(int j=0;j<[page_control[@"count"] intValue];j++)
  491. // {
  492. // NSMutableDictionary* control=page_control[[NSString stringWithFormat:@"control_%d",j]];
  493. //
  494. // PDFDrawable * control_drawable = nil;
  495. // CGRect control_rect = CGRectMake([control[@"pos_x"] floatValue], [control[@"pos_y"] floatValue], [control[@"width"] floatValue], [control[@"height"] floatValue]);
  496. //
  497. //
  498. // NSString* control_type = control[@"type"];
  499. // if([control_type isEqualToString:@"Signature"]||[control_type isEqualToString:@"Image"])
  500. // {
  501. //
  502. // NSString* value = control[@"value"];
  503. // if(value.length==0)
  504. // continue;
  505. //
  506. //
  507. // 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"];
  508. // }
  509. // else if([control_type isEqualToString:@"TextView"]||[control_type isEqualToString:@"Label"]||[control_type isEqualToString:@"DatePicker"])
  510. // {
  511. //
  512. // NSString* value = control[@"value"];
  513. // if(value.length==0)
  514. // continue;
  515. // NSString* size = control[@"size"];
  516. // if(size.length==0)
  517. // size=@"10";
  518. // NSString* textAlignment=control[@"textAlignment"];
  519. // if(textAlignment.length==0)
  520. // textAlignment = @"Center";
  521. // double margin_left = [control[@"margin_left"] doubleValue];
  522. //
  523. //
  524. // 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];
  525. //
  526. // [control_drawable setMargin_left:margin_left];
  527. // }
  528. // else if([control_type isEqualToString:@"Check"])
  529. // {
  530. // NSArray* value = control[@"value"];
  531. // if(value.count==0)
  532. // continue;
  533. // NSArray* cadedate = control[@"cadedate"];
  534. // float marker_size = [control[@"marker_size"] floatValue];
  535. //
  536. // for(int c=0;c<value.count;c++)
  537. // {
  538. // NSArray* marker=cadedate[[value[c] intValue]];
  539. // if(marker.count>=3)
  540. // {
  541. // NSString* value = marker[0][0];
  542. // if(value.length==0)
  543. // continue;
  544. // NSString* size = control[@"size"];
  545. // if(size.length==0)
  546. // size=@"10";
  547. //
  548. // 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];
  549. // }
  550. // else
  551. // {
  552. // CGRect marker_rect= CGRectMake([marker[1][0] floatValue] ,[marker[1][1] floatValue], marker_size, marker_size);
  553. // PDFDrawable * marker_drawable = [[RectDrawable alloc] init:[self WindowRect2PDFRect:marker_rect pdf_rect:papersize window_size:window_rect.size] color:@"0x000000"];
  554. // if(marker_drawable)
  555. // [pdfPage DirectDraw:pdfContext drawable:marker_drawable];
  556. // }
  557. //
  558. // }
  559. //
  560. //// NSString* size = control[@"size"];
  561. //// if(size.length==0)
  562. //// size=@"10";
  563. ////
  564. //// 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];
  565. // }
  566. // if(control_drawable)
  567. //// for(int l=0;l<100;l++)
  568. // [pdfPage DirectDraw:pdfContext drawable:control_drawable];
  569. //
  570. //
  571. //
  572. // }
  573. //// NSMutableDictionary* tline1= [LineDrawable createlineTemplate:1 from:CGPointMake(20, 20) to:CGPointMake(500,500)];
  574. //// LineDrawable* line1=nil;
  575. //// line1= [[LineDrawable alloc] init:tline1];
  576. ////
  577. //// [pdfPage DirectDraw:pdfContext drawable:line1];
  578. //// NSRange range = NSMakeRange(0,9999);
  579. //// [line1 Draw:pdfContext dataSource:nil ParentRect:papersize startX:0 startY:0 flipHeight:0 range:range];
  580. //
  581. // CGContextEndPage(pdfContext);
  582. //
  583. // // [pdfPage Draw:pdfContext dataSource:nil];
  584. //// [self drawPage:pdfContext template:page size:papersize dataSource:data];
  585. //
  586. //
  587. //// CFBridgingRelease(page);
  588. // }
  589. // CGContextRelease(pdfContext);
  590. // CFRelease(url);
  591. // // CFRelease(myDictionary);
  592. //
  593. //
  594. // // CFRelease(cfpdfAttributes);
  595. //// //开始画pdf
  596. ////
  597. //// // NSString *temtext=[[NSString alloc]init];
  598. //// //
  599. //// // const char *text=(char *)[temtext UTF8String];
  600. //// //
  601. //// // int width;
  602. //// //
  603. //// // int height;
  604. //// //
  605. ////
  606. //// // [self newpage:pdfContext size:papersize];
  607. ////
  608. ////
  609. ////
  610. //// CGContextRelease(pdfContext);
  611. //
  612. // return newFilePath;
  613. //
  614. //}
  615. @end