ios - 将签名图像添加到 pdf 而不向 iOS 中的用户显示 pdf 数据

标签 ios objective-c ipad pdf pdf-generation

我想在现有的 pdf 上添加签名。我通过以下方式完成了此操作:

1) 在 UIView 上加载现有的 pdf,比如 mainView。

2) 在mainView上添加签名图片。

3) 调用下面的函数

-(NSMutableData *)getPDFDatafromUIView
{
DebugLog(@"");
// Creates a mutable data object for updating with binary data, like a byte array
NSMutableData *pdfData = [NSMutableData data];

// Points the pdf converter to the mutable data object and to the UIView to be converted
UIGraphicsBeginPDFContextToData(pdfData, mainView.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();

// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
mainView.layer renderInContext:pdfContext];

// remove PDF rendering context
UIGraphicsEndPDFContext();

return pdfData;
} 

4) 此函数会暂时阻塞您的 UI,因此请在新线程上调用它

   [NSThread detachNewThreadSelector:@selector(launchExportViewForExportDrawing) toTarget:self withObject:nil];

使用上述方法我得到一个带有签名的 pdf 数据,其中也包含旧的 pdf 数据。

但对于上述方法,我必须需要在 UIView 中显示 pdf。如果我想在不加载 UIView 的情况下执行上述操作,而不向用户显示 pdf,我该怎么做?

我可以通过创建新的 pdf 页面在 pdf 上添加图像。如何在现有 pdf 上添加图像?

最佳答案

我已经通过创建新的 PDF 并在其上绘制 pdf 数据和签名来解决了。请引用以下代码:

// For adding the Siganture we need to wite the content on new PDF
-(void) addSignature:(UIImage *) imgSignature onPDFData:(NSData *)pdfData {

NSMutableData* outputPDFData = [[NSMutableData alloc] init];
CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData((CFMutableDataRef)outputPDFData);

CFMutableDictionaryRef attrDictionary = NULL;
attrDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(attrDictionary, kCGPDFContextTitle, CFSTR("My Doc"));
CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, NULL, attrDictionary);
CFRelease(dataConsumer);
CFRelease(attrDictionary);
CGRect pageRect;

// Draw the old "pdfData" on pdfContext
CFDataRef myPDFData = (__bridge CFDataRef) pdfData;
CGDataProviderRef provider = CGDataProviderCreateWithCFData(myPDFData);
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithProvider(provider);
CGDataProviderRelease(provider);
CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1);
pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
CGContextBeginPage(pdfContext, &pageRect);
CGContextDrawPDFPage(pdfContext, page);

// Draw the signature on pdfContext
pageRect = CGRectMake(0, 0,imgSignature.size.width , imgSignature.size.height);
CGImageRef pageImage = [imgSignature CGImage];
CGContextDrawImage(pdfContext, pageRect, pageImage);

// release the allocated memory
CGPDFContextEndPage(pdfContext);
CGPDFContextClose(pdfContext);
CGContextRelease(pdfContext);

// write new PDFData in "outPutPDF.pdf" file in document directory
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pdfFilePath =[NSString stringWithFormat:@"%@/outPutPDF.pdf",docsDirectory];
[outputPDFData writeToFile:pdfFilePath atomically:YES];

}

关于ios - 将签名图像添加到 pdf 而不向 iOS 中的用户显示 pdf 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23154614/

相关文章:

ios - 如何在 UIScrollView 上添加 UIViewController View

iphone - 删除动画完成后更新 TableView

ios - iPhone - Grand Central Dispatch 主线程

ios - 如何在后台使用 session.uploadTask 我正在崩溃

ios - 获取操作系统名称(例如 iOS)

ios - Objective-C 到 Swift : Pass Completion Handler

ios - 找不到CorePlot/CorePlot.h

ios - 拍照并尝试关闭 didFinishPickingMediaWIthInfo 中的预览

jquery - iPad 绝对定位(这次有点不同)

ios - Cordova ipad app 100%高度问题