使用 UIPrintPageRenderer 在打印的 pdf 中绘制水平线

发布于 2024-11-19 23:50:14 字数 1052 浏览 5 评论 0原文

我正在使用 UIPrintPageRenderer 子类在 pdf 上打印 html 内容。如何在打印内容(页眉和页脚)上添加水平线?

CGContextAddLineToPoint 似乎不适用于 UIPrintPageRenderer 方法。特别是那些用于绘制页眉和页脚的。 NSString 的drawAtPoint 工作完美。

到目前为止,这是我尝试过的:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

方圜几里 2024-11-26 23:50:14

在Core Graphics中,逻辑图形元素被添加到上下文中然后绘制。我看到您使用例如 CGContextAddLineToPoint(context, 310.0, 20.0); 添加了上下文的路径

,这会在内存中创建路径,但要将其合成到屏幕上,您需要填充或描边上下文的小路。尝试在实际添加路径之后添加 CGContextStrokePath(context); 。

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.

梅倚清风 2024-11-26 23:50:14

就像常规绘图一样,

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

不要忘记 CGStrokePath(...)

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

羞稚 2024-11-26 23:50:14

所以,现在我已经应用了替代解决方案。我仍然很想知道如何使用 CGContext 来做到这一点(无需加载图像)。这是我的解决方案:

// 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];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文