使用 Cocoa 的 CoreGraphics 渲染多种文档格式

发布于 2024-09-09 03:56:45 字数 995 浏览 0 评论 0原文

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 技术交流群。

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

发布评论

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

评论(2

对你的占有欲 2024-09-16 03:56:45

我认为 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 current NSGraphicsContext.

听闻余生 2024-09-16 03:56:45

我发现它是如何工作的。

这是一个很棒的开源示例,仅使用 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

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