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