Ios 电子签名集成使我的多页pdf变成单页

Ios 电子签名集成使我的多页pdf变成单页,ios,cocoa-touch,pdf,digital-signature,Ios,Cocoa Touch,Pdf,Digital Signature,我试图通过将UIView中绘制的签名提取到我的pdf文件来对我的多页pdf文件进行签名,但我面临的问题是,在签署pdf后,我只能查看已签名的文件的单个页面,而不能查看webview中的其余页面。(例如,如果我的pdf文件的第3页已签名,我只能在我的webview中查看第3页,并且pdf文件仅限于我的文档目录中的第3页) 用于将签名从文档目录提取到pdf文件的代码为 - (void)viewWillAppear:(BOOL)animated { [webView reload]; UIWebVi

我试图通过将UIView中绘制的签名提取到我的pdf文件来对我的多页pdf文件进行签名,但我面临的问题是,在签署pdf后,我只能查看已签名的文件的单个页面,而不能查看webview中的其余页面。(例如,如果我的pdf文件的第3页已签名,我只能在我的webview中查看第3页,并且pdf文件仅限于我的文档目录中的第3页)

用于将签名从文档目录提取到pdf文件的代码为

- (void)viewWillAppear:(BOOL)animated
{
[webView reload];

UIWebView *webView;
webView= [[UIWebView alloc] initWithFrame:CGRectMake(0,44, 320, 460)];

NSString *path1;
path1 = [[NSBundle mainBundle] pathForResource:@"typo_tips" ofType:@"pdf"];

 NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
                                                     NSUserDomainMask, YES);

NSString *documentDirectoryPath;
NSURL *targetURL;

documentDirectoryPath  = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"typo_tips.pdf"];

[fileManager copyItemAtPath:path1 toPath:documentDirectoryPath error:&error];

NSLog(@"path1 value is %@ \n",path1);
NSLog(@"docu dir path is %@ \n",documentDirectoryPath);



if (entered==1)//"entered==1", after save button clicked in signviewcontroller 
{
   targetURL = [NSURL fileURLWithPath:documentDirectoryPath];


}
else     targetURL = [NSURL fileURLWithPath:path1];


 if (entered==1)
{


    CFURLRef url;
    url = (CFURLRef)CFBridgingRetain([NSURL fileURLWithPath:documentDirectoryPath]);
    CGPDFDocumentRef myDocument;
    myDocument = CGPDFDocumentCreateWithURL(url);

    // Create PDF context
    CGContextRef pdfContext = CGPDFContextCreateWithURL(url, NULL, NULL);     //(CFURLRef)outputURL
    CGPDFContextBeginPage(pdfContext, NULL);
    UIGraphicsPushContext(pdfContext);

    int totalPages = (int)CGPDFDocumentGetNumberOfPages(myDocument);
    NSLog(@"no. of pages in pdf is %d \n",totalPages);

    CGContextDrawPDFPage(UIGraphicsGetCurrentContext(), CGPDFDocumentGetPage(myDocument, page));
    //"page" is current pdf page to be signed

    NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Image.png"];// "image.png" is the saved user's signature in document directory


    image = [[UIImage alloc] initWithContentsOfFile:filePath];
    CGRect imageRect = CGRectMake(50, 50, image.size.width, image.size.height);


    CGContextDrawImage(UIGraphicsGetCurrentContext(), imageRect, image.CGImage);


    // Clean up
    UIGraphicsPopContext();
    CGPDFContextEndPage(pdfContext);
    CGPDFContextClose(pdfContext);

}

NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
}

您必须重新呈现每个页面(即使是未被修改的页面)

CGContextRef pdfContext=CGPDFContextCreateWithURL(url,NULL,NULL);//(CFURLRef)outputURL
UIGraphicsPushContext(pdfContext);
int totalPages=(int)CGPDFDocumentGetNumberOfPages(myDocument);
NSLog(@“pdf中的页数为%d\n”,总页数);
对于(int currentPage=0;currentPage
在iPDFDev的帮助下找到了解决方案:

在for循环中,必须为源文件中的每个页面创建一个新的PDF页面。因此,请将这些行移到for循环中:

 CGPDFContextBeginPage(pdfContext, NULL);
UIGraphicsPushContext(pdfContext);
以及相应的清理代码

(void)viewWillAppear:(BOOL)animated
{
[webView reload];

UIWebView *webView;
webView= [[UIWebView alloc] initWithFrame:CGRectMake(0,44, 320, 460)];

NSString *path1;
path1 = [[NSBundle mainBundle] pathForResource:@"typo_tips" ofType:@"pdf"];

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
                                                 NSUserDomainMask, YES);

NSString *documentDirectoryPath;
NSURL *targetURL;

documentDirectoryPath  = [[paths objectAtIndex:0]              stringByAppendingPathComponent:@"typo_tips.pdf"];

[fileManager copyItemAtPath:path1 toPath:documentDirectoryPath error:&error];

NSLog(@"path1 value is %@ \n",path1);
NSLog(@"docu dir path is %@ \n",documentDirectoryPath);



if (entered==1)//"entered==1", after save button clicked in signviewcontroller 
{
targetURL = [NSURL fileURLWithPath:documentDirectoryPath];


}
else     targetURL = [NSURL fileURLWithPath:path1];


 if (entered==1)
{


CFURLRef url;
url = (CFURLRef)CFBridgingRetain([NSURL fileURLWithPath:documentDirectoryPath]);
CGPDFDocumentRef myDocument;
myDocument = CGPDFDocumentCreateWithURL(url);

// Create PDF context
CGContextRef pdfContext = CGPDFContextCreateWithURL(url, NULL, NULL);     

int totalPages = (int)CGPDFDocumentGetNumberOfPages(myDocument);
NSLog(@"no. of pages in pdf is %d \n",totalPages);

for (int currentPage = 0; currentPage < totalPages; currentPage++)
{

//(CFURLRef)outputURL
CGPDFContextBeginPage(pdfContext, NULL);
UIGraphicsPushContext(pdfContext);

CGContextDrawPDFPage(UIGraphicsGetCurrentContext(), CGPDFDocumentGetPage(myDocument, page));
//"page" is current pdf page to be signed

    if (page == currentPage)
    {

    NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Image.png"];// "image.png" is the saved user's signature in document directory

    image = [[UIImage alloc] initWithContentsOfFile:filePath];
    CGRect imageRect = CGRectMake(50, 50, image.size.width, image.size.height);

    CGContextDrawImage(UIGraphicsGetCurrentContext(), imageRect, image.CGImage);
    }

// Clean up
UIGraphicsPopContext();
CGPDFContextEndPage(pdfContext);
}
CGPDFContextClose(pdfContext);

}

NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
}
(void)视图将显示:(BOOL)动画
{
[webView重新加载];
UIWebView*webView;
webView=[[UIWebView alloc]initWithFrame:CGRectMake(0,44320460)];
NSString*path1;
path1=[[NSBundle mainBundle]pathForResource:@“pdf”类型的“打字技巧”;
NSFileManager*fileManager=[NSFileManager defaultManager];
n错误*错误;
NSArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,是);
NSString*documentDirectoryPath;
NSURL*目标URL;
documentDirectoryPath=[[paths objectAtIndex:0]stringByAppendingPathComponent:@“typo_tips.pdf”];
[fileManager copyItemAtPath:path1 toPath:documentDirectoryPath错误:&错误];
NSLog(@“路径1值为%@\n”,路径1);
NSLog(@“docu dir path为%@\n”,documentDirectoryPath);
如果(输入==1)/“输入==1”,则在signviewcontroller中单击保存按钮后
{
targetURL=[NSURL fileURLWithPath:documentDirectoryPath];
}
else targetURL=[NSURL fileURLWithPath:path1];
如果(输入==1)
{
cfurlRefURL;
url=(CFURLRef)CfBrigingRetain([NSURL fileURLWithPath:documentDirectoryPath]);
CGPDFDocumentRef myDocument;
myDocument=CGPDFDocumentCreateWithURL(url);
//创建PDF上下文
CGContextRef pdfContext=CGPDFContextCreateWithURL(url,NULL,NULL);
int totalPages=(int)CGPDFDocumentGetNumberOfPages(myDocument);
NSLog(@“pdf中的页数为%d\n”,总页数);
对于(int currentPage=0;currentPage
ya,我也尝试了同样的for循环,结果是将整个pdf内容显示为一个页面(例如:第1页到第5页内容显示为一个页面),签名集成在各自的位置。关于显示为pdf的一个页面,你能给我提些建议吗?谢谢。
(void)viewWillAppear:(BOOL)animated
{
[webView reload];

UIWebView *webView;
webView= [[UIWebView alloc] initWithFrame:CGRectMake(0,44, 320, 460)];

NSString *path1;
path1 = [[NSBundle mainBundle] pathForResource:@"typo_tips" ofType:@"pdf"];

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
                                                 NSUserDomainMask, YES);

NSString *documentDirectoryPath;
NSURL *targetURL;

documentDirectoryPath  = [[paths objectAtIndex:0]              stringByAppendingPathComponent:@"typo_tips.pdf"];

[fileManager copyItemAtPath:path1 toPath:documentDirectoryPath error:&error];

NSLog(@"path1 value is %@ \n",path1);
NSLog(@"docu dir path is %@ \n",documentDirectoryPath);



if (entered==1)//"entered==1", after save button clicked in signviewcontroller 
{
targetURL = [NSURL fileURLWithPath:documentDirectoryPath];


}
else     targetURL = [NSURL fileURLWithPath:path1];


 if (entered==1)
{


CFURLRef url;
url = (CFURLRef)CFBridgingRetain([NSURL fileURLWithPath:documentDirectoryPath]);
CGPDFDocumentRef myDocument;
myDocument = CGPDFDocumentCreateWithURL(url);

// Create PDF context
CGContextRef pdfContext = CGPDFContextCreateWithURL(url, NULL, NULL);     

int totalPages = (int)CGPDFDocumentGetNumberOfPages(myDocument);
NSLog(@"no. of pages in pdf is %d \n",totalPages);

for (int currentPage = 0; currentPage < totalPages; currentPage++)
{

//(CFURLRef)outputURL
CGPDFContextBeginPage(pdfContext, NULL);
UIGraphicsPushContext(pdfContext);

CGContextDrawPDFPage(UIGraphicsGetCurrentContext(), CGPDFDocumentGetPage(myDocument, page));
//"page" is current pdf page to be signed

    if (page == currentPage)
    {

    NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Image.png"];// "image.png" is the saved user's signature in document directory

    image = [[UIImage alloc] initWithContentsOfFile:filePath];
    CGRect imageRect = CGRectMake(50, 50, image.size.width, image.size.height);

    CGContextDrawImage(UIGraphicsGetCurrentContext(), imageRect, image.CGImage);
    }

// Clean up
UIGraphicsPopContext();
CGPDFContextEndPage(pdfContext);
}
CGPDFContextClose(pdfContext);

}

NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
}