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上面的这个标记
- (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;
}
}
}
}