打印为 PDF 时使文本不可选择
我有一个专为打印而设计的视图,其中包括水印,一个透明视图,可在其他内容之上绘制一些文本。
打印和使用 Mac OS 另存为 PDF 功能时,可以选择水印文本。有时这会干扰选择其他内容,有时它只是分散注意力。
如何使生成的 PDF 中的文本不可选?
我尝试将水印绘制在其他内容后面而不是前面。它不会阻止选择水印,但不会妨碍其他内容。然而,表视图行遮挡了水印,这当然更糟糕。
评论者要求提供代码,因此这里有一些准备视图的代码:
// self.view is the print view
// watermark is an instance of WatermarkBackground, an NSView
if (watermark) {
watermark.frame = self.view.frame;
[self.view addSubview:watermark positioned:NSWindowAbove relativeTo:nil];
}
以及 [WatermarkBackground drawRect] 中进行绘图的行:
// _message is an NSString
// textAttributes returns a dictionary with a color and font
[_message drawWithRect:textRect
options:NSLineBreakByWordWrapping
attributes:[WatermarkBackground textAttributes]];
我本来打算发布此屏幕截图:
I have a view designed for printing which includes a watermark, a transparent view which draws some text atop the other content.
When printing and using the Mac OS Save as PDF feature, the watermark text is selectable. Sometimes this interferes with selecting the other content, other times it's just distracting.
How can I make the text not selectable in the generated PDF?
I tried drawing the watermark behind the other content instead of in front. It didn't prevent selecting the watermark, but kept it out of the way of the other content. However, the table view rows occluded the watermark, which of course is worse.
Commenter asked for code, so here's some code which prepares the view:
// self.view is the print view
// watermark is an instance of WatermarkBackground, an NSView
if (watermark) {
watermark.frame = self.view.frame;
[self.view addSubview:watermark positioned:NSWindowAbove relativeTo:nil];
}
And the line in [WatermarkBackground drawRect] which does the drawing:
// _message is an NSString
// textAttributes returns a dictionary with a color and font
[_message drawWithRect:textRect
options:NSLineBreakByWordWrapping
attributes:[WatermarkBackground textAttributes]];
I meant to post this screenshot originally:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种选择是从字符串创建一个或多个 CGPath,并将它们绘制到 PDF 中。一种方法是使用 CTFontCreatePathForGlyph,但实际上对整个字符串执行此操作需要大量工作,Core Text 确实有帮助,但它是一个相当低级的框架。
如果您总是绘制相同的水印,那么在某些矢量图形应用程序中创建静态 PDF 并将其与 CGPDFPageDraw 等一起使用会更容易。Illustrator 有一个用于文本的“转换为路径”命令对象。
One option would be to create one or multiple
CGPath
s from your string and draw those into the PDF instead. One way to do so would be to useCTFontCreatePathForGlyph
, but it's actually quite a lot of work to do this for entire strings, Core Text does help, but it's a pretty low-level framework.If you're always drawing the same watermark, it would be much easier to create a static PDF in some vector graphics app and use that with
CGPDFPageDraw
etc. Illustrator has a "Convert to Paths" command for text objects.据我所知,没有办法使PDF中的文本变得不可选择。
也许最好的解决方案是使用图像水印。但是,如果它位于文本前面,则会导致背景文本难以选择。如果它位于所有内容的后面,那么用表格来模糊它也会出现同样的问题。因此,更好的行动计划可能不是尝试使文本不可选择,而是使表格背景透明。然后,使用图像水印。而不是使用 CGPaths 并动态生成它们。
As far as I know, there in no way to make text unselectable in PDF.
Probably the best solution would be to use an image watermark instead.However, if it is in front of text, it can make background text difficult to select. If it is behind everything, there will be same issues with obscuring it with tables. So, possibly a better plan of action would be not to try to make text unselectable, but rather make table background transparent. Then, use image watermark.Taking an idea from omz, instead of using CGPaths and generating them on the fly, the simplest, most elegant solution would be this:
有一个解决方案可以使整个 PDF 变得不可选择且无法复制。
这样,生成的新 PDF 就变成了一组像素,而不是精心渲染的矢量字体、文本、形状等。虽然阅读起来不太舒服,但仍然足够好。
There is a solution that makes the whole PDF non-selectable and impossible to copy.
This way, the produced new PDF becomes a set of pixels, rather than nicely rendered vectorial fonts, texts, shapes, etc. It is less nice to read but still mostly good enough.