Saturday, September 24, 2022
HomeiOS Developmentios - Drawing on PDFPage with drawWithBox: toContext with giant web page...

ios – Drawing on PDFPage with drawWithBox: toContext with giant web page measurement


I’ve subclassed PDFPage and overridden – (void)drawWithBox:(PDFDisplayBox)field toContext:(CGContextRef)context to be able to put a watermark on the web page when being seen. That is working for all of the recordsdata which have pages which can be 8.5×11 however I’ve some pages which can be bigger than that and only for the bigger pages the watermark isn’t being drawn on the web page.

On a normal web page measurement pageBounds = (origin = (x = 0, y = 0), measurement = (width = 612, top = 792))
On the bigger web page pageBounds = (origin = (x = 0, y = 0), measurement = (width = 1190, top = 1682))

I do not see why the web page measurement would matter however that’s all I can see that’s completely different between the recordsdata. The PDF is rendered simply advantageous for both measurement and the drawWithBox perform is named for each sizes.

    - (void)drawWithBox:(PDFDisplayBox)field toContext:(CGContextRef)context {
    
        [super drawWithBox:box toContext:context];

        NSString *watermarkText = @"Watermark";
    
        UIGraphicsPushContext(context);
        CGContextSaveGState(context);
      
        CGRect pageBounds = [self boundsForBox:box];
 
        CGContextTranslateCTM(context, 0.0, pageBounds.measurement.top);
        CGContextScaleCTM(context, 1.0, -1.0);

        NSDictionary *attribs = @{NSForegroundColorAttributeName: [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.25],
                                                                    NSFontAttributeName: [UIFont systemFontOfSize:55.0 weight:UIFontWeightBold]
        };
        
        
        NSAttributedString *attribWatermark = [[NSAttributedString alloc] initWithString:watermarkText attributes:attribs];
        
        CGSize textBoxSize = [self texBoxSizeFor:watermarkText withAtttributes:attribs maxPageSize:pageBounds ];
        CGRect textBoxFrame = CGRectMake((pageBounds.measurement.width / 2) - (textBoxSize.width / 2), (pageBounds.measurement.top / 2) - (textBoxSize.top / 2), textBoxSize.width, textBoxSize.top);
        
        [attribWatermark drawInRect:textBoxFrame];
        CGContextRestoreGState(context);
        UIGraphicsPopContext();
    }

- (CGSize) texBoxSizeFor:(NSString *) textual content withAtttributes:(NSDictionary *) attributes maxPageSize:(CGRect) pageSize {
    
    CGSize textBoxSize = CGSizeMake(pageSize.measurement.width - kPadding * 2, pageSize.measurement.top - kPadding * 2);
    CGRect boundingBox = [text boundingRectWithSize: textBoxSize
                                            options: NSStringDrawingUsesLineFragmentOrigin
                                         attributes: attributes
                                            context: nil];

 return CGSizeMake(ceil(boundingBox.measurement.width), ceil(boundingBox.measurement.top));
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular