PDFUtils.m 27 KB

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