把iPad上面的签名添加到PDF文档上

1,项目中我使用了PDFNet_iOS的framework实现了对PDF的编辑,还有添加了Wacom_Signature_SDK_iOS_v1.0.12的SDK实现了使用wacom的签名笔连接功能。

     

// Must Initial the PTPDFNet、
        [PTPDFNet Initialize: 0];
        PTPDFDoc* doc = [[PTPDFDoc alloc] initWithFilepath:PDF模板路径];
        [doc InitSecurityHandler];    
// Update Annotations  dataArray:为你要插入到PDF的数据
        [self updateAnnotationsWithDoc:doc dataArray:dataArray];
// Flatten Annotations
        [doc FlattenAnnotations:false];
// Save File
        [doc SaveToFile:PDF生成路径 flags: e_ptlinearized];

updateAnnotationsWithDoc方法的实现,该方法主要是遍历PDF文档里面所有subject为IMG_开头的标记,只要是IMG_开头的都会把IMG_去掉得到的字符串,然后到dataArray里面找该字符串对应的数据(PS:我使用的是base64传过来的数据),在把该数据转码成img然后替换掉PDF上面的这个标记

145211_ajSQ_1043582.png

- (void)updateAnnotationsWithDoc:(PTPDFDoc*)doc dataArray:(NSArray*)dataArray {
    
    int pageNum = 1;
    
    // Get the Answer Dictionary
    NSMutableDictionary* raqAnsDict = dataArray;
    NSString* isResultOverridden = [raqAnsDict objectForKey:@"isResultOverridden"];
    //get all answer number
    NSMutableArray *mutablearray = [[NSMutableArray alloc] init];
    mutablearray = [self getAllAnswerNO:raqAnsDict];
    
    PTPageIterator* itr;
    for (itr = [doc GetPageIterator: 1]; [itr HasNext]; [itr Next]) {
        PTPage* page = [itr Current];
        int num_annots = [page GetNumAnnots];
        int i = 0;
        for (i = 0; i < num_annots; ++i) {
            PTAnnot* annot = [page GetAnnot: i];
            if (![annot IsValid]) continue;
            
            switch ([annot GetType]) {
                case e_ptFreeText:
                {
                    PTFreeText* txtAnnot = [[PTFreeText alloc] initWithAnn: annot];
                    NSString* txtAnnotSubject = [self getSubjectName: [txtAnnot GetSubject]];
                    
                    if ([txtAnnotSubject hasPrefix:@"IMG_"]) {
                        // Image
                        NSString* ansString = [raqAnsDict objectForKey:[txtAnnotSubject stringByReplacingOccurrencesOfString:@"IMG_" withString:@""]];
                        
                        if (ansString != (id)[NSNull null] && ansString.length != 0) {
                            NSLog(@"ansString is not empty!!!");
                            
                            NSString* imgStrNew = [ansString substringWithRange: NSMakeRange([ansString rangeOfString: @","].location+1, ansString.length-[ansString rangeOfString: @","].location-1)];
                            
                            NSData *data = [[NSData alloc] initWithData:[NSData dataFromBase64String:imgStrNew]];
                            UIImage *ansObj = [UIImage imageWithData:data];
                            
                            if (ansObj != (id)[NSNull null] && [ansObj isKindOfClass:[UIImage class]]) {
                                UIImage* imgSrc = (UIImage*)ansObj;
                                // Create temp image path
                                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                                NSString *imgFilePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:tmpImageFileName];
                                
                                NSLog(@"imgFilePath %@: ", imgFilePath);
                                
                                // Save & Load image: OK!
                                [UIImagePNGRepresentation(imgSrc) writeToFile:imgFilePath atomically:YES];
                                PTImage* img = [PTImage Create: [doc GetSDFDoc] filename:imgFilePath];
                                
                                // Builder
                                PTElementBuilder* builder = [[PTElementBuilder alloc] init];
                                PTElementWriter* writer = [[PTElementWriter alloc] init];
                                [writer WriterBeginWithPage:page placement:e_ptoverlay page_coord_sys:YES compress:YES];
                                
                                PTPDFRect* boxRect = [annot GetRect];
                                double boxWidth = [boxRect GetX2]-[boxRect GetX1];
                                double boxHeight = [boxRect GetY2]-[boxRect GetY1];
                                double imgWidth = [img GetImageWidth];
                                double imgHeight = [img GetImageHeight];
                                double imgHScale = 0;
                                double imgVScale = 0;
                                if ( (imgWidth/imgHeight) < (boxWidth/boxHeight)) {
                                    // Use height
                                    imgHScale = (boxHeight/imgHeight)*imgWidth;
                                    imgVScale = boxHeight;
                                } else {
                                    // Use height
                                    imgHScale = boxWidth;
                                    imgVScale = (boxWidth/imgWidth)*imgHeight;
                                }
                                
                                NSLog(@"Create Create Image: boxWidth: %f, boxHeight: %f", boxWidth, boxHeight);
                                NSLog(@"Image: imgWidth: %f, imgHeight: %f", imgWidth, imgHeight);
                                NSLog(@"Create Image: imgHScale: %f, imgVScale: %f", imgHScale, imgVScale);
                                
                                PTElement* element = [builder CreateImageWithCornerAndScale: img x:[boxRect GetX1] y:[boxRect GetY1] hscale:imgHScale vscale:imgVScale];
                                [writer WritePlacedElement: element];
                                [writer End];
                            }
                        }
                    }
                    [txtAnnot SetQuaddingFormat:0];  // Align 0:Left 1:Center
                    [txtAnnot RefreshAppearance];
                }
                    break;
                case e_ptLink:
                    break;
                case e_ptWidget:
                    break;
                case e_ptFileAttachment:
                    break;
                default:
                    break;
            }
        }
    }
}


转载于:https://my.oschina.net/u/1043582/blog/670188

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值