使用 Cocoa 的 CoreGraphics 渲染多种文档格式
CoreGraphics 提供了渲染 PDF 文档的可能性,这很棒。但它似乎也可以支持其他类型,如 Word 文档、HTML、RTF 等。
快速浏览一下 Mac OS X Developer DVD 上的 doc2pdf.py 示例让我这么想:
pageRect = CGRectMake(0, 0, 612, 792) c = CGPDFContextCreateWithFilename(output_file, pageRect) c.beginPage(pageRect) if fnmatch(ext,".txt") or fnmatch(ext,".?") or fnmatch(ext,".??"): tr = c.drawPlainTextInRect(text, pageRect, font_size) elif fnmatch(ext,".rtf"): tr = c.drawRTFTextInRect(text, pageRect, font_size) elif fnmatch(ext,".htm*") or fnmatch(ext,".php"): tr = c.drawHTMLTextInRect(text, pageRect, font_size) elif fnmatch(ext,".doc"): tr = c.drawDocFormatTextInRect(text, pageRect, font_size) elif fnmatch(ext,"*ml"): tr = c.drawWordMLFormatTextInRect(text, pageRect, font_size) else: return "Error: unknown type '%s' for '%s'"%(ext, input_file)
不幸的是,我不明白 CoreGraphics 的 Python 绑定是如何工作的(而且绑定本身似乎是二进制的和专有的)。我在 Apple 开发人员文档中找不到这些方法的 C/Obj-C 等效项。 Google 发现没有太多讨论这些功能的网页。
有什么想法吗?
CoreGraphics gives the possibility to render PDF documents, this is great. But it seems like it could support other types too like Word documents, HTML, RTF and others.
A quick look in the doc2pdf.py example on the Mac OS X Developer DVD makes me think so :
pageRect = CGRectMake(0, 0, 612, 792) c = CGPDFContextCreateWithFilename(output_file, pageRect) c.beginPage(pageRect) if fnmatch(ext,".txt") or fnmatch(ext,".?") or fnmatch(ext,".??"): tr = c.drawPlainTextInRect(text, pageRect, font_size) elif fnmatch(ext,".rtf"): tr = c.drawRTFTextInRect(text, pageRect, font_size) elif fnmatch(ext,".htm*") or fnmatch(ext,".php"): tr = c.drawHTMLTextInRect(text, pageRect, font_size) elif fnmatch(ext,".doc"): tr = c.drawDocFormatTextInRect(text, pageRect, font_size) elif fnmatch(ext,"*ml"): tr = c.drawWordMLFormatTextInRect(text, pageRect, font_size) else: return "Error: unknown type '%s' for '%s'"%(ext, input_file)
Unfortunately, I do not understand how works the CoreGraphics' Python bindings (and it seems that the binding itself is binary and proprietary). I cannot find the C/Obj-C equivalent for these methods in Apple developers documentation. And there is not much webpages found by Google talking about these functionnality.
Any ideas ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为 CoreGraphics 不能单独做到这一点。 App Kit 添加到
NSAttributedString
类中,提供了从各种文档初始化属性字符串并在当前NSGraphicsContext
中绘制它的方法。I don't think CoreGraphics alone does this. The App Kit Addition to the
NSAttributedString
class provides ways to initialize an attributed string from various kinds of documents, and to draw it in the currentNSGraphicsContext
.我发现它是如何工作的。
这是一个很棒的开源示例,仅使用 Cocoa 框架对这些格式的支持来解析 doc、docx、odt、rtf 等文件:
http://www.bean-osx.com/Bean.html
I found how it works.
Here's a great Open-Source example here parsing doc, docx, odt, rtf, etc. files only by using Cocoa framework support for these formats :
http://www.bean-osx.com/Bean.html