Drawing horizontal line in printed pdf using UIPrintPageRenderer

1.7k Views Asked by At

I'm using UIPrintPageRenderer sub-class to print html content on a pdf. How can i add a horizontal line on my printed content (both on header and footer)?

CGContextAddLineToPoint doesn't seem to work in UIPrintPageRenderer methods. Specifically those used to draw header and footer. NSString's drawAtPoint is working perfectly.

Here's what i've tried so far:

- (void)drawHeaderForPageAtIndex:(NSInteger)pageIndex inRect:(CGRect)headerRect {
    ...

    // Attempt 1 (Doesn't work!)
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1);
    CGContextSetRGBStrokeColor(context, 1.0f, 1.0f, 1.0f, 1);
    CGContextMoveToPoint(context, 10.0, 20.0);
    CGContextAddLineToPoint(context, 310.0, 20.0);

    // Attempt 2 (Doesn't work!)
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1);
    CGContextSetRGBStrokeColor(context, 1.0f, 1.0f, 1.0f, 1);

    CGContextTranslateCTM(context, 0, headerRect.size.height);
    CGContextScaleCTM(context, 1, -1);

    CGContextMoveToPoint(context, 10.0, 20.0);
    CGContextAddLineToPoint(context, 310.0, 20.0);
}
3

There are 3 best solutions below

1
On BEST ANSWER

So, for now i've applied an alternate solution. I would still love to know how to do this using CGContext (without having to load an image). Here's my solution:

// Draw horizontal ruler in the header
UIImage *horizontalRule = [UIImage imageNamed:@"HorizontalRule.png"];
horizontalRule = [horizontalRule stretchableImageWithLeftCapWidth:0.5 topCapHeight:0];

CGFloat rulerX = CGRectGetMinX(headerRect) + HEADER_LEFT_TEXT_INSET;
CGFloat rulerY = self.printableRect.origin.y + fontSize.height + HEADER_FOOTER_MARGIN_PADDING + PRINT_RULER_MARGIN_PADDING;
CGFloat rulerWidth = headerRect.size.width - HEADER_LEFT_TEXT_INSET - HEADER_RIGHT_TEXT_INSET;
CGFloat rulerHeight = 1;
CGRect ruleRect = CGRectMake(rulerX, rulerY, rulerWidth, rulerHeight);

[horizontalRule drawInRect:ruleRect blendMode:kCGBlendModeNormal alpha:1.0];
0
On

Just as regular drawing

- (void)drawHeaderForPageAtIndex:(NSInteger)pageIndex inRect:(CGRect)headerRect {

    CGContextRef context = UIGraphicsGetCurrentContext();

    CContextMoveToPoint(context, CGRectGetMinX(headerRect), 70);
    CGContextAddLineToPoint(context, CGRectGetMaxX(headerRect), 70);

    CGFloat grayScale = 0.5f;
    CGContextSetRGBStrokeColor(context, grayScale, grayScale, grayScale, 1);

    CGContextStrokePath(context);
}

Don't forget to CGStrokePath(...)

0
On

In Core Graphics, a logical graphics elements are added to the context and then drawn. I see you adding a path to the context with e.g. CGContextAddLineToPoint(context, 310.0, 20.0);

This creates the path in memory, but to composite it to the screen you need to either fill or stroke the context's path. Try adding CGContextStrokePath(context); after to actually stoke the path.