ILPDFKit icon indicating copy to clipboard operation
ILPDFKit copied to clipboard

Image/Signature

Open Twelve57 opened this issue 9 years ago • 14 comments

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

Twelve57 avatar Feb 24 '16 15:02 Twelve57

Did you find a way to do this?

mikerz avatar Mar 30 '16 20:03 mikerz

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

Twelve57 avatar Mar 31 '16 06:03 Twelve57

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

jaouahbi avatar Apr 21 '16 14:04 jaouahbi

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

Twelve57 avatar Apr 21 '16 14:04 Twelve57

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

jaouahbi avatar Apr 21 '16 14:04 jaouahbi

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

Twelve57 avatar Apr 21 '16 14:04 Twelve57

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

jaouahbi avatar Apr 21 '16 14:04 jaouahbi

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

Twelve57 avatar Apr 21 '16 15:04 Twelve57

do not allow Adobe Acrobat X modify the document?

jaouahbi avatar Apr 21 '16 15:04 jaouahbi

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

Twelve57 avatar Apr 21 '16 15:04 Twelve57

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

jaouahbi avatar Apr 21 '16 15:04 jaouahbi

Hi @jaouahbi what is MSImageInfo in your code

sonimeister avatar Jun 17 '16 13:06 sonimeister

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

jaouahbi avatar Jun 20 '16 09:06 jaouahbi

Thanks @jaouahbi 👍

sonimeister avatar Jun 20 '16 11:06 sonimeister