高亮显示 PDF 文档 iPhone xcode 中的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1755584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:57:45  来源:igfitidea点击:

HIghlighting the text in PDF document iPhone xcode

iphonexcodepdfwebview

提问by cooliphonedeveloper

Hii friends, i am developing a e Book using a web view where i am loading the web view with PDF document.Now i want to develop the search functionality but i am not unserstanding how to implement it.So kindly give me a solution,

嗨朋友们,我正在使用 web 视图开发 ae Book,我正在加载带有 PDF 文档的 web 视图。现在我想开发搜索功能,但我并不了解如何实现它。所以请给我一个解决方案,

Thanks in advance.

提前致谢。

回答by Alex Reynolds

Instead of using UIWebView, you might want to look into using Quartz 2D's CGPDFDocumentRefand parsing a CGPDFDictionaryfor strings of interest.

UIWebView您可能想要研究使用 Quartz 2DCGPDFDocumentRef并解析 aCGPDFDictionary以获取感兴趣的字符串,而不是使用。

Once your know which PDF dictionary elements contain your text and where they are located, you might be able to highlight that text with other Quartz 2D routines.

一旦您知道哪些 PDF 字典元素包含您的文本以及它们的位置,您就可以使用其他 Quartz 2D 例程突出显示该文本。

Another option might be to convert your PDF document to RTF or HTML formats, either of which would probably be easier to parse and edit to add highlighting.

另一种选择可能是将您的 PDF 文档转换为 RTF 或 HTML 格式,这两种格式可能更容易解析和编辑以添加突出显示。

回答by Joe Yang

it seems not an easy work, here is my thought.

这似乎不是一件容易的工作,这是我的想法。

Instead of using CGContextDrawPDFPage in Quartz2D, you may need to layout the page yourself for getting the CGRect of the text for highlight.

而不是在 Quartz2D 中使用 CGContextDrawPDFPage,您可能需要自己布局页面以获得文本的 CGRect 以进行高亮显示。

  1. Parse PDF page content to PDF objects.
  2. Combine the PDF objects in binary form or other forms to the objects you can use in Core Text/CoreGraphics, like CTFontDescriptorRef, PDF operators binary streams
  3. Use Core Text/CoreGraphics function to layout the text/graphics from the fonts/text/images/pdf graphic operators, and then you can get the position and rect
  4. return a CGRect, and highlight that CGRect using CoreGraphics functions
  1. 将 PDF 页面内容解析为 PDF 对象。
  2. 将二进制形式或其他形式的 PDF 对象组合到您可以在 Core Text/CoreGraphics 中使用的对象,例如 CTFontDescriptorRef、PDF 运算符二进制流
  3. 使用 Core Text/CoreGraphics 函数从 fonts/text/images/pdf 图形操作符中对文本/图形进行布局,然后就可以得到位置和矩形
  4. 返回一个 CGRect,并使用 CoreGraphics 函数突出显示该 CGRect