0

我试图通过将 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];
}
4

2 回答 2

1

您必须重新渲染每个页面(即使是那些没有被更改的页面)。

CGContextRef pdfContext = CGPDFContextCreateWithURL(url, NULL, NULL);     //(CFURLRef)outputURL

UIGraphicsPushContext(pdfContext);

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

for (int currentPage = 0; currentPage < totalPages; currentPage++)
{
    CGPDFContextBeginPage(pdfContext, NULL);
    CGContextDrawPDFPage(UIGraphicsGetCurrentContext(), CGPDFDocumentGetPage(myDocument, currentPage));

    //"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);
于 2013-11-13T20:07:48.230 回答
0

在 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];
}
于 2013-11-14T10:35:34.367 回答