Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Image/Signature #33

Open
Twelve57 opened this issue Feb 24, 2016 · 14 comments
Open

Image/Signature #33

Twelve57 opened this issue Feb 24, 2016 · 14 comments

Comments

@Twelve57
Copy link

How to manage Image or Signature ?
Currently we can update TextField and Checkboxes, but what about updating a placeholder for image or signature ?

@mikerz
Copy link

mikerz commented Mar 30, 2016

Did you find a way to do this?

@Twelve57
Copy link
Author

The only solution I've found is to create again the pdf,
with CGPDFDocumentCreateWithUrl.

And then, at the correct page, draw my picture/signature at the correct position,
with picture.drawInRect(CGRectMake(x, y, width, height))

@jaouahbi
Copy link

jaouahbi commented Apr 21, 2016

You can render the image in the frame of the item but you must modify the library.

static void renderPageWithImageInfo(NSUInteger page, CGContextRef ctx,CGPDFDocumentRef doc,PDFFormContainer *forms, NSArray * imageInfo)
{
    CGRect mediaRect = CGPDFPageGetBoxRect(CGPDFDocumentGetPage(doc,page), kCGPDFMediaBox);
    CGRect cropRect = CGPDFPageGetBoxRect(CGPDFDocumentGetPage(doc,page), kCGPDFCropBox);
    CGRect artRect = CGPDFPageGetBoxRect(CGPDFDocumentGetPage(doc,page), kCGPDFArtBox);
    CGRect bleedRect = CGPDFPageGetBoxRect(CGPDFDocumentGetPage(doc,page), kCGPDFBleedBox);
    UIGraphicsBeginPDFPageWithInfo(mediaRect, @{(NSString*)kCGPDFContextCropBox:[NSValue valueWithCGRect:cropRect],(NSString*)kCGPDFContextArtBox:[NSValue valueWithCGRect:artRect],(NSString*)kCGPDFContextBleedBox:[NSValue valueWithCGRect:bleedRect]});
    CGContextSaveGState(ctx);
    CGContextScaleCTM(ctx,1,-1);
    CGContextTranslateCTM(ctx, 0, -mediaRect.size.height);
    CGContextDrawPDFPage(ctx, CGPDFDocumentGetPage(doc,page));
    CGContextRestoreGState(ctx);
    for (PDFForm *form in forms) {
        if (form.page == page) {
            MSImageInfo* infoFound = nil;
            for (MSImageInfo * info in imageInfo) {
                // its the element in the current page
                if (info.page == page && CGRectEqualToRect(info.frame, form.frame)) {
                    infoFound = info;
                    break;
                }
            }

            CGContextSaveGState(ctx);
            if( infoFound ) {
                CGRect frame = infoFound.frame;
                //@scale an traslate the matrix
                CGContextScaleCTM(ctx, 1.0, -1.0);
                CGContextTranslateCTM(ctx, 0, -mediaRect.size.height);
                CGContextDrawImage(ctx, frame, infoFound.image.CGImage );
            } else {
                CGRect frame = form.frame;
                CGRect correctedFrame = CGRectMake(frame.origin.x-mediaRect.origin.x,
                                                   mediaRect.size.height-frame.origin.y-frame.size.height-mediaRect.origin.y,
                                                   frame.size.width,
                                                   frame.size.height);
                CGContextTranslateCTM(ctx, correctedFrame.origin.x, correctedFrame.origin.y);
                [form vectorRenderInPDFContext:ctx forRect:correctedFrame];
            }
            CGContextRestoreGState(ctx);
        }
    }
}

@Twelve57
Copy link
Author

How do you retrieve the correct position of the signature in the PDF ?

@jaouahbi
Copy link

jaouahbi commented Apr 21, 2016

Create a hidden textfield and use its frame.
This solution works but you must generate the image from you own signature view.

@Twelve57
Copy link
Author

It's a profesional protected PDF with multiple signature fields, I cannot modify it.

@jaouahbi
Copy link

do not work?
NSArray * signatures = [_pdfDocument.forms formsWithType:PDFFormTypeSignature];
CGRect frame = [signatures firstObject].frame

@Twelve57
Copy link
Author

Nope :/ it's not working.
But I've found another solution to make it work, don't worry.

Thanks for your time and your answers ;)

@jaouahbi
Copy link

do not allow Adobe Acrobat X modify the document?

@Twelve57
Copy link
Author

The client doesn't allow me to modify it. Bank Business ;)

@jaouahbi
Copy link

ok :) sorry. Mi solution its dirty but in my case solved my problems.
We are using a https://tenonedesign.com/t1autograph.php for sign the document, then we are copy the image generated from t1autograph UIView and is draw in a frame of a hidden textfield in the pdf
Sorry for my english ;)

@sonimeister
Copy link

sonimeister commented Jun 17, 2016

Hi @jaouahbi
what is MSImageInfo in your code

@jaouahbi
Copy link

@interface MSImageInfo : NSObject
@property (nonatomic, assign)  CGRect      frame;
@property (nonatomic, assign)  NSUInteger  page;
@property (nonatomic, strong)  UIImage*    image;
@end


-(void) addImageInfo:(UIImage * ) image withRect:(CGRect) rect withPage:(NSUInteger) page {
    NSParameterAssert(image);
    if(image == nil) return;
    NSParameterAssert(page);    // pdf pages start at 1
    if(page <= 0) return;
    NSParameterAssert(!CGRectEqualToRect(rect,CGRectZero));
    if(CGRectEqualToRect(rect,CGRectZero)) return;
    NSParameterAssert(rect.size.width > 0 && rect.size.height > 0);
    if(rect.size.width == 0 || rect.size.height == 0) return;
    NSParameterAssert(_imageInfo);
    if(_imageInfo == nil) return;

    NSAssert([_imageInfo count] < kMaxNumberExpectedOfImages,
             @"Unexpected number of images. (images:%ld expected:%ld)",
             [_imageInfo count],
             kMaxNumberExpectedOfImages);

    // scale the source image without change the aspect ratio.
    BOOL hasAlpha   = true;
    CGFloat scale   = 0.0;
    CGAffineTransform affineTransform = CGAffineTransformMakeScale(image.size.width / rect.size.width,
                                                                   image.size.height / rect.size.height);
#if defined(VERBOSE)
    NSLog(@"Image aspect ratio transform (width/height) from (1.0/1.0) to (%2f / %2f)",
          image.size.width / rect.size.width,
          image.size.height / rect.size.height);
#endif

    // transform the CGSize
    CGSize size = CGSizeApplyAffineTransform(image.size, affineTransform);
    UIGraphicsBeginImageContextWithOptions(size, !hasAlpha, scale);
    [image drawInRect:(CGRect){CGPointZero, size}];
    UIImage * const scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    // save the image, its frame and the pdf document page for draw it.
    MSImageInfo * const info = [MSImageInfo new];
    // setup the item
    info.frame   = rect;
    info.page    = page;
    info.image   = scaledImage;
    // add to the array of image info for delayed process
    [_imageInfo addObject:info];
}

@sonimeister
Copy link

Thanks @jaouahbi 👍

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants