Преглед изворни кода

170109
debuting memory leak

Ray Zhang пре 9 година
родитељ
комит
3e7c76c5ff

BIN
RedAnt ERP Mobile/RedAnt ERP Mobile.xcworkspace/xcuserdata/Ray.xcuserdatad/UserInterfaceState.xcuserstate


+ 3 - 1
RedAnt ERP Mobile/common/Functions/PDF+Web/PDFViewController.m

@@ -28,6 +28,8 @@
 
 - (void)viewDidLoad
 {
+    
+    NSLog(@"pdf viewer loaded");
     [super viewDidLoad];
     // Do any additional setup after loading the view.
     UIRefreshControl *ref = [[UIRefreshControl alloc]init];
@@ -180,7 +182,7 @@
         if(self.autoPrint)
         [self startAirPrintWithData:self.content];
         
-        
+        NSLog(@"put file to webview");
         [self.webView loadData:self.content MIMEType:@"application/pdf" textEncodingName:nil baseURL:nil];
         
     }

+ 2 - 0
RedAnt ERP Mobile/common/PDFUtils.h

@@ -16,4 +16,6 @@
 //+ (CGRect)scaleToSize:(CGRect )from to:(CGSize)to;
 +(NSString*)SavePDF:(NSMutableDictionary*) controlTemplate source:(CGPDFDocumentRef )document window_rect:(CGRect)window_rect;
 +(NSMutableDictionary*) loadControl:(NSString*)templateName;
+
++(void) leak_test:(NSMutableDictionary*) controlTemplate source:(CGPDFDocumentRef* )document window_rect:(CGRect)window_rect;
 @end

+ 446 - 104
RedAnt ERP Mobile/common/PDFUtils.m

@@ -14,6 +14,8 @@
 
 
 @implementation PDFUtils
+
+
 +(NSString*) addSignature :(UIImage*) img to:(NSMutableDictionary*)signatureData subType:(NSString*)subtype
 {
     
@@ -94,43 +96,49 @@
     
     //   CGContextRef pdfContext;
     
-    CFStringRef path;
+//    CFStringRef path;
+//    
+//    CFURLRef url;
+//    
+//    path=CFStringCreateWithCString(NULL, filename, kCFStringEncodingUTF8);
+//    
+//    url=CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0);
     
-    CFURLRef url;
     
-    path=CFStringCreateWithCString(NULL, filename, kCFStringEncodingUTF8);
+//    CFRelease(path);
+    CGPDFDocumentRef document;
     
-    url=CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0);
     
+    CFURLRef pdfURL = (__bridge CFURLRef)[NSURL fileURLWithPath:default_path];
     
-    CFRelease(path);
-    CGPDFDocumentRef document;
-    document = CGPDFDocumentCreateWithURL (url);
-    CFRelease(url);
-    size_t totalpage = CGPDFDocumentGetNumberOfPages (document);
-    
-    if (totalpage == 0) {
-        printf("[%s] needs at least one page!\n", [@"myPDF.pdf" UTF8String] );
-        return NULL;
-    } else {
-        printf("[%ld] pages loaded in this PDF!\n", totalpage);
-        
-        
-        /*8
-         for (NSInteger pageNumber = 1; pageNumber <= count; pageNumber++)
-         {
-         CGPDFPageRef pageRef = CGPDFDocumentGetPage(document, pageNumber);
-         
-         CGPDFDictionaryRef pageDictionaryFromPage = CGPDFPageGetDictionary(pageRef);
-         
-         if (pageDictionaryFromPage == pageDictionaryFromDestArray) // Found it
-         {
-         targetPageNumber = pageNumber; break;
-         }
-         
-         
-         }*/
-    }
+//    CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL(pdfURL);
+    
+    document = CGPDFDocumentCreateWithURL (pdfURL);
+//    CFRelease(url);
+//    size_t totalpage = CGPDFDocumentGetNumberOfPages (document);
+//    
+//    if (totalpage == 0) {
+//        printf("[%s] needs at least one page!\n", [@"myPDF.pdf" UTF8String] );
+//        return NULL;
+//    } else {
+//        printf("[%ld] pages loaded in this PDF!\n", totalpage);
+//        
+//        
+//        /*8
+//         for (NSInteger pageNumber = 1; pageNumber <= count; pageNumber++)
+//         {
+//         CGPDFPageRef pageRef = CGPDFDocumentGetPage(document, pageNumber);
+//         
+//         CGPDFDictionaryRef pageDictionaryFromPage = CGPDFPageGetDictionary(pageRef);
+//         
+//         if (pageDictionaryFromPage == pageDictionaryFromDestArray) // Found it
+//         {
+//         targetPageNumber = pageNumber; break;
+//         }
+//         
+//         
+//         }*/
+//    }
     
     //    CGPDFPageRef page= CGPDFDocumentGetPage(document, 0);
     //    CGContextDrawPDFPage(<#CGContextRef  _Nullable c#>, <#CGPDFPageRef  _Nullable page#>)
@@ -167,7 +175,70 @@
     return CGRectMake(x, y, width, height);
     
 }
++(void) leak_test:(NSMutableDictionary*) controlTemplate source:(CGPDFDocumentRef* )document window_rect:(CGRect)window_rect
+{
+    
+    
+
+    //获取路径
+    
+    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);//坐标
+    
+    NSString *saveDirectory=[paths objectAtIndex:0];
+    
+    NSString *saveFileName=[NSString stringWithFormat:@"%@.pdf",[[NSUUID UUID ] UUIDString] ] ;;
+    
+    NSString *newFilePath=[saveDirectory stringByAppendingPathComponent:saveFileName];
+    
+    
+    
+    
+    const char *filename=[newFilePath UTF8String];
+    
+    
+    CFStringRef path;
+    
+    CFURLRef url;
+    
+    path=CFStringCreateWithCString(NULL, filename, kCFStringEncodingUTF8);
+    
+    url=CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0);
+    
+    CFBridgingRelease(path);
+
+    
+    
+    CGContextRef pdfContext;
+    CGRect docsize = CGRectMake(0, 0, 612, 1008);
+    
+    
+    
+    
+    CFMutableDictionaryRef myDictionary = NULL;
+    myDictionary= CFDictionaryCreateMutable(NULL,
+                                            0,
+                                            &kCFTypeDictionaryKeyCallBacks,
+                                            &kCFTypeDictionaryValueCallBacks);
+
+    
+    //如果注释掉这行就不会有问题
+//    CFDictionarySetValue(myDictionary, kCGPDFContextOwnerPassword, CFSTR("12345"));
 
+    
+    CFDictionarySetValue(myDictionary, kCGPDFContextAllowsPrinting, kCFBooleanTrue);
+    CFDictionarySetValue(myDictionary, kCGPDFContextAllowsCopying, kCFBooleanFalse);
+    
+    pdfContext=CGPDFContextCreateWithURL(url, &docsize,myDictionary);
+    
+    CFRelease(myDictionary);
+    CFBridgingRelease(url);
+    CGPDFContextClose(pdfContext);
+
+
+    
+    CGContextRelease(pdfContext);
+    
+}
 +(NSString*)SavePDF:(NSMutableDictionary*) controlTemplate source:(CGPDFDocumentRef )document window_rect:(CGRect)window_rect
 {
     
@@ -187,10 +258,10 @@
     //设置页面大小 Letter纸
     
     //CGPDFDocumentGetMediaBox(document,1);
-    CGPDFDictionaryRef pdf_dict= CGPDFDocumentGetInfo(document);
+    //   CGPDFDictionaryRef pdf_dict= CGPDFDocumentGetInfo(document);
+    
     
     
-
     
     
     //关联上下文的对象
@@ -208,49 +279,64 @@
     CFBridgingRelease(path);
     // CGPDFCONTEXTCREATE
     
-  
+    
     CGContextRef pdfContext;
-    CGRect docsize = CGRectMake(0, 0, 100, 100);
+    CGRect docsize = CGRectMake(0, 0, 612, 1008);
     
     
-//    if(name.length==0)
-//    {
-//        name=@"Helvetica";
-//        //        name=@"DejaVu Sans";
-//    }
-//    if(size==0)
-//    {
-//        size=10;
-//    }
-//    NSString* style = @"Regular";
-//    if(bold)
-//        style=@"Bold";
-//    
-//    //name=@"Courier";
-    
-    NSDictionary *PDFAttributes =
-    [NSDictionary dictionaryWithObjectsAndKeys:
-     @"usai2010", (NSString *)kCGPDFContextOwnerPassword,
-     kCFBooleanTrue, (NSString *)kCGPDFContextAllowsPrinting,
-     kCFBooleanFalse, (NSString *)kCGPDFContextAllowsCopying,
-     nil];
-    // Create a descriptor.
- CFDictionaryRef cfpdfAttributes =(__bridge CFDictionaryRef)(PDFAttributes);
-
+    //    NSDictionary *PDFAttributes =
+    //    [NSDictionary dictionaryWithObjectsAndKeys:
+    //     @"usai2010", (NSString *)kCGPDFContextOwnerPassword,
+    //     kCFBooleanTrue, (NSString *)kCGPDFContextAllowsPrinting,
+    //     kCFBooleanFalse, (NSString *)kCGPDFContextAllowsCopying,
+    //     nil];
+    //    // Create a descriptor.
+    // CFDictionaryRef cfpdfAttributes =(__bridge_retained  CFDictionaryRef)PDFAttributes;
+    
+    
+    //    CFBridgingRelease(cfpdfAttributes);
+    
+    
+    
+    
+    CFMutableDictionaryRef myDictionary = NULL;
+    myDictionary= CFDictionaryCreateMutable(NULL,
+                                            0,
+                                            &kCFTypeDictionaryKeyCallBacks,
+                                            &kCFTypeDictionaryValueCallBacks);
+    //    CFDictionarySetValue(myDictionary,
+    //                         kCGPDFContextTitle,
+    //                         CFSTR("Photo from iPrivate Album"));
+    //    CFDictionarySetValue(myDictionary,
+    //                         kCGPDFContextCreator,
+    //                         CFSTR("iPrivate Album"));
+    
+    //    //设置文档名称
+    //    CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
+    //    //设置创建者
+    //    CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
+    //设置文档尺寸
+    CFDictionarySetValue(myDictionary, kCGPDFContextOwnerPassword, CFSTR("usai2010"));
+    CFDictionarySetValue(myDictionary, kCGPDFContextAllowsPrinting, kCFBooleanTrue);
+    CFDictionarySetValue(myDictionary, kCGPDFContextAllowsCopying, kCFBooleanFalse);
+    
+    
+    
+    pdfContext=CGPDFContextCreateWithURL(url, &docsize,NULL);
+    
     
-   // CFBridgingRelease(cfpdfAttributes);
-    pdfContext=CGPDFContextCreateWithURL(url, &docsize,cfpdfAttributes);
     
     
     
     size_t page_count=CGPDFDocumentGetNumberOfPages(document);
     for(int i=1;i<=page_count;i++)
     {
-        NSMutableDictionary* page_control=controlTemplate[[NSString stringWithFormat:@"page_%d",i-1]];
-        CGPDFPageRef page= CGPDFDocumentGetPage (document , i);
-        
+        int p = (i+4)%5+1;
+        NSMutableDictionary* page_control=controlTemplate[[NSString stringWithFormat:@"page_%d",p-1]];
+        CGPDFPageRef page= CGPDFDocumentGetPage (document , p);
+        //        CGPDFPageRef page1= CGPDFDocumentGetPage (document , i);
         CGRect papersize=CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
-
+        
         
         
         CGContextBeginPage(pdfContext, &papersize);
@@ -267,10 +353,11 @@
             PDFDrawable * control_drawable = nil;
             CGRect control_rect = CGRectMake([control[@"pos_x"] floatValue], [control[@"pos_y"] floatValue], [control[@"width"] floatValue], [control[@"height"] floatValue]);
             
-
+            
             NSString* control_type = control[@"type"];
             if([control_type isEqualToString:@"Signature"]||[control_type isEqualToString:@"Image"])
             {
+                
                 NSString* value = control[@"value"];
                 if(value.length==0)
                     continue;
@@ -280,14 +367,23 @@
             }
             else if([control_type isEqualToString:@"TextView"]||[control_type isEqualToString:@"Label"]||[control_type isEqualToString:@"DatePicker"])
             {
+                
                 NSString* value = control[@"value"];
                 if(value.length==0)
                     continue;
                 NSString* size = control[@"size"];
                 if(size.length==0)
                     size=@"10";
+                NSString* textAlignment=control[@"textAlignment"];
+                if(textAlignment.length==0)
+                    textAlignment = @"Middle";
+                double margin_left = [control[@"margin_left"] doubleValue];
                 
-                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];
+            
+                
+                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];
+                
+                [control_drawable setMargin_left:margin_left];
             }
             else if([control_type isEqualToString:@"Check"])
             {
@@ -296,7 +392,7 @@
                     continue;
                 NSArray* cadedate = control[@"cadedate"];
                 float marker_size = [control[@"marker_size"] floatValue];
-    
+                
                 for(int c=0;c<value.count;c++)
                 {
                     NSArray* marker=cadedate[[value[c] intValue]];
@@ -309,7 +405,7 @@
                         if(size.length==0)
                             size=@"10";
                         
-                        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];
+                        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];
                     }
                     else
                     {
@@ -318,59 +414,305 @@
                         if(marker_drawable)
                             [pdfPage DirectDraw:pdfContext drawable:marker_drawable];
                     }
-
+                    
                 }
                 
-//                NSString* size = control[@"size"];
-//                if(size.length==0)
-//                    size=@"10";
-//                
-//                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];
+                //                NSString* size = control[@"size"];
+                //                if(size.length==0)
+                //                    size=@"10";
+                //
+                //                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];
             }
             if(control_drawable)
-                [pdfPage DirectDraw:pdfContext drawable:control_drawable];
+//                for(int l=0;l<1000;l++)
+                    [pdfPage DirectDraw:pdfContext drawable:control_drawable];
             
             
             
         }
-//        NSMutableDictionary* tline1= [LineDrawable createlineTemplate:1 from:CGPointMake(20, 20) to:CGPointMake(500,500)];
-//        LineDrawable* line1=nil;
-//        line1= [[LineDrawable alloc] init:tline1];
-//        
-//        [pdfPage DirectDraw:pdfContext drawable:line1];
-//                NSRange range = NSMakeRange(0,9999);
-//        [line1 Draw:pdfContext dataSource:nil ParentRect:papersize startX:0 startY:0 flipHeight:0 range:range];
+        //        NSMutableDictionary* tline1= [LineDrawable createlineTemplate:1 from:CGPointMake(20, 20) to:CGPointMake(500,500)];
+        //        LineDrawable* line1=nil;
+        //        line1= [[LineDrawable alloc] init:tline1];
+        //
+        //        [pdfPage DirectDraw:pdfContext drawable:line1];
+        //                NSRange range = NSMakeRange(0,9999);
+        //        [line1 Draw:pdfContext dataSource:nil ParentRect:papersize startX:0 startY:0 flipHeight:0 range:range];
         
-         CGContextEndPage(pdfContext);
-
-       // [pdfPage Draw:pdfContext dataSource:nil];
-//        [self drawPage:pdfContext template:page size:papersize dataSource:data];
+        CGContextEndPage(pdfContext);
+        
+        
+        //       CGPDFPageRelease(page);
         
+        // [pdfPage Draw:pdfContext dataSource:nil];
+        //        [self drawPage:pdfContext template:page size:papersize dataSource:data];
         
-//        CFBridgingRelease(page);
+        
+        //        CFBridgingRelease(page);
     }
     CGContextRelease(pdfContext);
+    CFRelease(myDictionary);
     CFBridgingRelease(url);
-  //  CFRelease(cfpdfAttributes);
-//    //开始画pdf
+    
+    //  CFRelease(cfpdfAttributes);
+    //    //开始画pdf
+    //    
+    //    //    NSString *temtext=[[NSString alloc]init];
+    //    //
+    //    //    const char *text=(char *)[temtext UTF8String];
+    //    //
+    //    //    int  width;
+    //    //
+    //    //    int height;
+    //    //
+    //    
+    //    //    [self newpage:pdfContext size:papersize];
+    //    
+    //    
+    //    
+    //    CGContextRelease(pdfContext);
+    
+    return newFilePath;
+    
+}
+//{
+//    
+//    //获取路径
+//    
+//    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);//坐标
+//    
+//    NSString *saveDirectory=[paths objectAtIndex:0];
+//    
+//    NSString *saveFileName=[NSString stringWithFormat:@"%@.pdf",[[NSUUID UUID ] UUIDString] ] ;;
+//    
+//    NSString *newFilePath=[saveDirectory stringByAppendingPathComponent:saveFileName];
+//    
+//    const char *filename=[newFilePath UTF8String];
+//    
+//    NSLog(@"%@",newFilePath);
+//    //设置页面大小 Letter纸
+//    
+//    //CGPDFDocumentGetMediaBox(document,1);
+//  //  CGPDFDictionaryRef pdf_dict= CGPDFDocumentGetInfo(document);
+//    
+//    
+//
+//    
+//    
+//    //关联上下文的对象
+//    
+//    
+//    
+//    CFStringRef path;
+//    
+//    CFURLRef url;
+//    
+//    path=CFStringCreateWithCString(NULL, filename, kCFStringEncodingUTF8);
 //    
-//    //    NSString *temtext=[[NSString alloc]init];
-//    //
-//    //    const char *text=(char *)[temtext UTF8String];
-//    //
-//    //    int  width;
-//    //
-//    //    int height;
-//    //
+//    url=CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0);
 //    
-//    //    [self newpage:pdfContext size:papersize];
+//    CFRelease(path);
+//    // CGPDFCONTEXTCREATE
 //    
+//  
+//    CGContextRef pdfContext;
+//    CGRect docsize = CGRectMake(0, 0, 100, 100);
 //    
 //    
+////    if(name.length==0)
+////    {
+////        name=@"Helvetica";
+////        //        name=@"DejaVu Sans";
+////    }
+////    if(size==0)
+////    {
+////        size=10;
+////    }
+////    NSString* style = @"Regular";
+////    if(bold)
+////        style=@"Bold";
+////    
+////    //name=@"Courier";
+//    
+////    NSDictionary *PDFAttributes =
+////    [NSDictionary dictionaryWithObjectsAndKeys:
+////     @"usai2010", (NSString *)kCGPDFContextOwnerPassword,
+////     kCFBooleanTrue, (NSString *)kCGPDFContextAllowsPrinting,
+////     kCFBooleanFalse, (NSString *)kCGPDFContextAllowsCopying,
+////     nil];
+////    // Create a descriptor.
+//// CFDictionaryRef cfpdfAttributes =(__bridge CFDictionaryRef)(PDFAttributes);
+////
+////    
+////   // CFBridgingRelease(cfpdfAttributes);
+////    pdfContext=CGPDFContextCreateWithURL(url, &docsize,cfpdfAttributes);
+//    
+//    
+//    
+//    CFMutableDictionaryRef myDictionary = NULL;
+//    //文档信息字典
+////    myDictionary = CFDictionaryCreateMutable(NULL, 0,
+////                                             &kCFTypeDictionaryKeyCallBacks,
+////                                             &kCFTypeDictionaryValueCallBacks);
+////    
+//////    CFStringRef password = (__bridge CFStringRef)@"usai2010";
+//////    CFDictionarySetValue(myDictionary, kCGPDFContextOwnerPassword, password);
+////    
+////    CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
+////    
+//////
+////    CFDictionarySetValue(myDictionary, kCGPDFContextOwnerPassword, CFSTR("usai2010"));
+//////
+////    
+////    CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
+////    CFDictionarySetValue(myDictionary, kCGPDFContextAllowsPrinting, kCFBooleanTrue);
+////     CFDictionarySetValue(myDictionary, kCGPDFContextAllowsCopying, kCFBooleanFalse);
+//
+//    //创建文档
+//    pdfContext = CGPDFContextCreateWithURL (url, &docsize, NULL);
+//    
+//    
+//    
+//    size_t page_count=CGPDFDocumentGetNumberOfPages(document);
+//    for(int i=1;i<=page_count;i++)
+//    {
+//        NSMutableDictionary* page_control=controlTemplate[[NSString stringWithFormat:@"page_%d",i-1]];
+//        CGPDFPageRef page= CGPDFDocumentGetPage (document , i);
+//        
+//        CGRect papersize=CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
+//
+//        
+//        
+//        CGContextBeginPage(pdfContext, &papersize);
+//        
+//        PDFPage* pdfPage = [[PDFPage alloc] init:nil size:papersize];
+//        
+//        [pdfPage DirectDraw:pdfContext page:page];
+//        
+//        
+//        for(int j=0;j<[page_control[@"count"] intValue];j++)
+//        {
+//            NSMutableDictionary* control=page_control[[NSString stringWithFormat:@"control_%d",j]];
+//            
+//            PDFDrawable * control_drawable = nil;
+//            CGRect control_rect = CGRectMake([control[@"pos_x"] floatValue], [control[@"pos_y"] floatValue], [control[@"width"] floatValue], [control[@"height"] floatValue]);
+//            
+//
+//            NSString* control_type = control[@"type"];
+//            if([control_type isEqualToString:@"Signature"]||[control_type isEqualToString:@"Image"])
+//            {
+//                
+//                NSString* value = control[@"value"];
+//                if(value.length==0)
+//                    continue;
+//                
+//                
+//                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"];
+//            }
+//            else if([control_type isEqualToString:@"TextView"]||[control_type isEqualToString:@"Label"]||[control_type isEqualToString:@"DatePicker"])
+//            {
+//                
+//                NSString* value = control[@"value"];
+//                if(value.length==0)
+//                    continue;
+//                NSString* size = control[@"size"];
+//                if(size.length==0)
+//                    size=@"10";
+//                NSString* textAlignment=control[@"textAlignment"];
+//                if(textAlignment.length==0)
+//                    textAlignment = @"Center";
+//                double margin_left = [control[@"margin_left"] doubleValue];
+//                
+//                
+//                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];
+//                
+//                [control_drawable setMargin_left:margin_left];
+//            }
+//            else if([control_type isEqualToString:@"Check"])
+//            {
+//                NSArray* value = control[@"value"];
+//                if(value.count==0)
+//                    continue;
+//                NSArray* cadedate = control[@"cadedate"];
+//                float marker_size = [control[@"marker_size"] floatValue];
+//    
+//                for(int c=0;c<value.count;c++)
+//                {
+//                    NSArray* marker=cadedate[[value[c] intValue]];
+//                    if(marker.count>=3)
+//                    {
+//                        NSString* value = marker[0][0];
+//                        if(value.length==0)
+//                            continue;
+//                        NSString* size = control[@"size"];
+//                        if(size.length==0)
+//                            size=@"10";
+//                        
+//                        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];
+//                    }
+//                    else
+//                    {
+//                        CGRect marker_rect= CGRectMake([marker[1][0] floatValue] ,[marker[1][1] floatValue], marker_size, marker_size);
+//                        PDFDrawable * marker_drawable = [[RectDrawable alloc] init:[self WindowRect2PDFRect:marker_rect pdf_rect:papersize window_size:window_rect.size] color:@"0x000000"];
+//                        if(marker_drawable)
+//                            [pdfPage DirectDraw:pdfContext drawable:marker_drawable];
+//                    }
+//
+//                }
+//                
+////                NSString* size = control[@"size"];
+////                if(size.length==0)
+////                    size=@"10";
+////                
+////                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];
+//            }
+//            if(control_drawable)
+////                for(int l=0;l<100;l++)
+//                [pdfPage DirectDraw:pdfContext drawable:control_drawable];
+//            
+//            
+//            
+//        }
+////        NSMutableDictionary* tline1= [LineDrawable createlineTemplate:1 from:CGPointMake(20, 20) to:CGPointMake(500,500)];
+////        LineDrawable* line1=nil;
+////        line1= [[LineDrawable alloc] init:tline1];
+////        
+////        [pdfPage DirectDraw:pdfContext drawable:line1];
+////                NSRange range = NSMakeRange(0,9999);
+////        [line1 Draw:pdfContext dataSource:nil ParentRect:papersize startX:0 startY:0 flipHeight:0 range:range];
+//        
+//         CGContextEndPage(pdfContext);
+//
+//       // [pdfPage Draw:pdfContext dataSource:nil];
+////        [self drawPage:pdfContext template:page size:papersize dataSource:data];
+//        
+//        
+////        CFBridgingRelease(page);
+//    }
 //    CGContextRelease(pdfContext);
-    
-    return newFilePath;
-    
-}
+//    CFRelease(url);
+//   // CFRelease(myDictionary);
+//
+//    
+//  //  CFRelease(cfpdfAttributes);
+////    //开始画pdf
+////    
+////    //    NSString *temtext=[[NSString alloc]init];
+////    //
+////    //    const char *text=(char *)[temtext UTF8String];
+////    //
+////    //    int  width;
+////    //
+////    //    int height;
+////    //
+////    
+////    //    [self newpage:pdfContext size:papersize];
+////    
+////    
+////    
+////    CGContextRelease(pdfContext);
+//    
+//    return newFilePath;
+//    
+//}
 
 @end

+ 3 - 0
RedAnt ERP Mobile/common/TextUtils.h

@@ -15,4 +15,7 @@
 +(NSTextCheckingResult*) expression_findfistMatch:(NSString*)content regex:(NSString*) pattern;
 + (CGRect)rectVAlign:(CGRect )parent rect:(CGRect)rect vAlign:(NSString*)vAlign;
 + (long)countOccurencesOfString:(NSString*)string find:(NSString*)searchString;
+
++(NSString*) encodePhoneNumber:(NSString*) phone;
++(NSString*) RegularExpReplace:(NSString*) content from:(NSString*) pattern to:(NSString*) to_pattern;
 @end

+ 125 - 0
RedAnt ERP Mobile/common/TextUtils.m

@@ -16,6 +16,131 @@
     return strCount / [searchString length];
 }
 
++ (NSString *)formatNumber:(NSString *)mobileNumber
+{
+    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
+    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
+    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
+    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
+    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];
+    
+    NSLog(@"%@", mobileNumber);
+    
+    int length = (int)[mobileNumber length];
+    if(length > 10)
+    {
+        mobileNumber = [mobileNumber substringFromIndex: length-10];
+        NSLog(@"%@", mobileNumber);
+        
+    }
+    
+    return mobileNumber;
+}
+
++ (int)getLength:(NSString *)mobileNumber
+{
+    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
+    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
+    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
+    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
+    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];
+    
+    int length = (int)[mobileNumber length];
+    
+    return length;
+}
+
++(NSString*) encodePhoneNumber:(NSString*) phone
+{
+    
+    
+    
+    
+    
+    NSLocale* locale = [NSLocale currentLocale];
+    if( [[locale localeIdentifier] compare:@"en_US"]!=NSOrderedSame )
+        return phone;
+    
+    if( [phone length]==0 )
+        return phone;
+    
+
+    if( [phone rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet] ].location==NSNotFound )
+    {
+        const char* string = [phone UTF8String];
+        int length = [phone lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
+        if( [[NSString stringWithCString:string length:1] intValue]==1 )
+        {
+            if( length > 11 )
+                return phone;
+            NSMutableData* array = [[NSMutableData alloc] init] ;
+            // 1 (234) 567-8901
+            for( int i=0; i < length; i++ )
+            {
+                if( i==1 )
+                {
+                    [array appendBytes:" (" length:2];
+                }
+                if( i == 4 )
+                {
+                    [array appendBytes:") " length:2];
+                }
+                if( i==7 )
+                {
+                    [array appendBytes:" " length:1];
+                }
+                [array appendBytes:string++ length:1];
+            }
+            return [NSString stringWithUTF8String:(const char*)[array bytes]];
+        }
+        else
+        {
+            // (012) 345-6789
+            if( length > 10 )
+                return phone;
+            NSMutableData* array = [[NSMutableData alloc] init] ;
+            int i=0;
+            if( length <=7 )
+            {
+                for( i=0; i < length; i++ )
+                {
+                    if( i==3 )
+                    {
+                        [array appendBytes:"-" length:1];
+                    }
+                    [array appendBytes:string++ length:1];
+                }
+            }
+            else
+            {
+                for( i=0; i < length; i++ )
+                {
+                    if( i==0 )
+                        [array appendBytes:"(" length:1];
+                    if( i==3 )
+                        [array appendBytes:") " length:2];
+                    if( i==6 )
+                        [array appendBytes:"-" length:1];
+                    [array appendBytes:string++ length:1];
+                }
+            }
+            [array appendBytes:"\0" length:1];
+            return [NSString stringWithUTF8String:(const char*)[array bytes]];
+        }
+    }
+    return phone;
+}
++(NSString*) RegularExpReplace:(NSString*) content from:(NSString*) pattern to:(NSString*) to_pattern
+{
+    if(content==nil)
+        return nil;
+    
+    
+    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:nil error:nil];
+    NSString* sss = [regex stringByReplacingMatchesInString:content options:0 range:NSMakeRange(0, content.length) withTemplate:to_pattern];
+    
+    return sss;
+}
 +(NSArray*) expression_varable:(NSString*)content regex:(NSString*) pattern
 {
     

+ 1 - 1
RedAnt ERP Mobile/common/pdfCreator/PDFDrawable.h

@@ -51,5 +51,5 @@
 
 
 -(void) DrawBound:(CGContextRef) context dataSource:(NSMutableDictionary*)data ParentRect:(CGRect)p_rect startX:(double) x startY:(double) y flipHeight:(double)flip_height range:(NSRange)between_header_and_footer;
-
+-(void) setMargin:(double)leftMargin right:(double)rightMargin top:(double)topMargin bottom:(double)bottomMargin;
 @end

+ 7 - 0
RedAnt ERP Mobile/common/pdfCreator/PDFDrawable.m

@@ -38,6 +38,13 @@
     return self;
     
 }
+-(void) setMargin:(double)leftMargin right:(double)rightMargin top:(double)topMargin bottom:(double)bottomMargin
+{
+    self.margin_left=leftMargin;
+    self.margin_right=rightMargin;
+    self.margin_top=topMargin;
+    self.margin_bottom=bottomMargin;
+}
 -(void) setDrawableTemplate:(NSMutableDictionary *)drawableTemplate
 {
     _drawableTemplate = drawableTemplate;

+ 4 - 3
RedAnt ERP Mobile/common/pdfCreator/TextDrawable.m

@@ -86,7 +86,8 @@
         self.textalignment= kCTTextAlignmentLeft;
     }
     
-    
+//    self.lineSpace = 0;
+//    self.paragraphSpace = 0;
     
     
     NSString* colorvalue=drawableTemplate[@"color"];
@@ -614,7 +615,7 @@
     //
     //    }
     //    else
-    {
+    
         string=[[NSMutableAttributedString alloc] initWithString:self.text];
         //创建字体以及字体大小
         //        CTFontRef ctfont = [self CreateFont:font size:fontsize bold:bold];//CTFontCreateWithName(CFSTR("Helvetica"), fontsize, NULL);
@@ -689,7 +690,7 @@
                        value:CFBridgingRelease(paragraphStyle)
                        range:NSMakeRange(0, [string length])];
         
-    }
+