使用 UIPrintPageRenderer 在打印的 pdf 中绘制水平线
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在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.就像常规绘图一样,
不要忘记
CGStrokePath(...)
Just as regular drawing
Don't forget to
CGStrokePath(...)
所以,现在我已经应用了替代解决方案。我仍然很想知道如何使用 CGContext 来做到这一点(无需加载图像)。这是我的解决方案:
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: