ILPDFKit
ILPDFKit copied to clipboard
Image/Signature
How to manage Image or Signature ? Currently we can update TextField and Checkboxes, but what about updating a placeholder for image or signature ?
Did you find a way to do this?
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))
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);
}
}
}
How do you retrieve the correct position of the signature in the PDF ?
Create a hidden textfield and use its frame. This solution works but you must generate the image from you own signature view.
It's a profesional protected PDF with multiple signature fields, I cannot modify it.
do not work? NSArray * signatures = [_pdfDocument.forms formsWithType:PDFFormTypeSignature]; CGRect frame = [signatures firstObject].frame
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 ;)
do not allow Adobe Acrobat X modify the document?
The client doesn't allow me to modify it. Bank Business ;)
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 ;)
Hi @jaouahbi what is MSImageInfo in your code
@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];
}
Thanks @jaouahbi 👍