Search code examples
iospdfintegrateelectronic-signature

e-sign in pdf file, makes multiple paged pdf to single page with same content


Am trying to sign my pdf file which is multiple paged, by fetching the signature drawn in UIView to my pdf file, but the problem I face is, after signing the pdf, signature is integrated with the pdf file where all the pages in the pdf file is displayed in a single page as a pdf file.(ex if 8 pages are there in a pdf file, then all 8 pages are displaying as a single page along with the signature). The output of the pdf file with signature integrated, is as below

output of the code

Codes used for fetching the image from document directory and integrating with the pdf file is,

- (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);

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

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];
}

Can any one help me, how to display the pdf page separately along with the e-signature integrated.


Solution

  • Try this:

    // Create PDF context
    CGContextRef pdfContext = CGPDFContextCreateWithURL(url, NULL, NULL);     //(CFURLRef)outputURL
    
    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);
        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 = [[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);
    

    In your for loop you have to create a new PDF page for each page in the source file. I moved these lines inside the for loop:

        CGPDFContextBeginPage(pdfContext, NULL);
        UIGraphicsPushContext(pdfContext);
    

    and also the corresponding cleanup code.