文本搜索PDF [英] Text searching PDF

查看:103
本文介绍了文本搜索PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解析PDF时,给定一个字符串(从Tj或TJ运算符回调中弹出),使用Identity-H编码,如何将该字符串映射到unicode(比如UTF8)表示?

When parsing a PDF, given a string (popped from the Tj or TJ operator callbacks) with the Identity-H encoding how do you map that string to a unicode (say UTF8) representation?

如果我需要一个CMap,我该如何创建(或检索)并应用CMap?

If I need a CMap for this, how do I create (or retrieve) and apply the CMap?

推荐答案

您可能需要解析字体数据本身。 Identity-H仅表示将字节用作原始字形索引到给定字体中。这就是为什么你在使用Identity-H时必须嵌入字体...相同字体的不同版本不需要具有相同的字形顺序。

You'll probably have to parse the font data itself. Identity-H just means "use the bytes as raw glyph indexes into the given font". That's why you MUST embed fonts when using Identity-H... different versions of the same font need not have the same glyph order.

有关于如何做的示例代码在几个不同的开源项目中这类事情。 iText,例如(是的,我有偏见)。

There's example code on how to do this sort of thing in several different open source projects. iText, for example (yes, I'm biased).

你提到了一个CMap。 Identity-H字体可以有CMap,但不是必须这样做。 / ToUnicode条目将是一个CMap流,如某些adobe规范中所定义的那样。它们并不复杂:

You'd mentioned a CMap. Identity-H fonts can have a CMap but aren't required to do so. The /ToUnicode entry will be a stream that is a CMap, as defined in some adobe spec somewhere. They aren't all that complex:

/CIDInit /ProcSet findresource begin  
12 dict begin  
begincmap  
/CIDSystemInfo  
<< /Registry (TTX+0)  
/Ordering (T42UV)  
/Supplement 0  
>> def  
/CMapName /TTX+0 def  
/CMapType 2 def
1 begincodespacerange  
<0000><FFFF>  
endcodespacerange  
80 beginbfrange  
<0003><0003><0020>  
<0024><0024><0041>  
<0025><0025><0042>  
<0026><0026><0043>  
<0027><0027><0044>  
<0028><0028><0045>  
<0029><0029><0046>  
<002a><002a><0047>  
<002b><002b><0048>
<002c><002c><0049>
<002d><002d><004a>
<002e><002e><004b>
<002f><002f><004c>
<0030><0030><004d>
<0031><0031><004e>
<0032><0032><004f>
<0033><0033><0050>
<0034><0034><0051>
<0035><0035><0052>
<0036><0036><0053>
<0037><0037><0054>
<0038><0038><0055>
<0039><0039><0056>
<003a><003a><0057>
<003b><003b><0058>
<003c><003c><0059>
<003d><003d><005a>
<0065><0065><00c9>
<00c8><00c8><00c1>
<00cb><00cb><00cd>
<00cf><00cf><00d3>
<00d2><00d2><00da>
<00e2><00e2><0160>
<00e4><00e4><017d>
<00e9><00e9><00dd>
<00fd><00fd><010c>
<0104><0104><0104>
<0106><0106><010e>
<0109><0109><0118>
<010b><010b><011a>
<0115><0115><0147>
<011b><011b><0158>
<0121><0121><0164>
<0123><0123><016e>
<01a0><01a0><0116>
<01b2><01b2><012e>
<01cb><01cb><016a>
<01cf><01cf><0172>
<022c><022c><0401>
<023b><023b><0411>
<023c><023c><0412>
<023d><023d><0413>
<023e><023e><0414>
<023f><023f><0415>
<0240><0240><0416>
<0241><0241><0417>
<0242><0242><0418>
<0243><0243><0419>
<0244><0244><041a>
<0245><0245><041b>
<0246><0246><041c>
<0247><0247><041d>
<0248><0248><041e>
<0249><0249><041f>
<024a><024a><0420>
<024b><024b><0421>
<024c><024c><0422>
<024d><024d><0423>
<024e><024e><0424>
<024f><024f><0425>
<0250><0250><0426>
<0251><0251><0427>
<0252><0252><0428>
<0253><0253><0429>
<0254><0254><042a>
<0255><0255><042b>
<0256><0256><042c>
<0257><0257><042d>
<0258><0258><042e>
<0259><0259><042f>
endbfrange
endcmap
CMapName currentdict /CMap defineresource pop
end end

哇。那个特定的CMap非常低效。 bfrange从参数1开始,然后转到并包含参数2,从参数3开始的maping值(并继续执行,直到没有更多要映射的内容。

Wow. That particular CMap is horribly inefficient. A "bfrange" starts from parameter 1, and goes to and includes parameter 2, maping values starting at parameter 3 (and continuing on until there are no more things to map.

例如:

<0003><0003><0020>
<0024><0024><0041>
<0025><0025><0042>
<0026><0026><0043>
<0027><0027><0044>
<0028><0028><0045>
<0029><0029><0046>
<002a><002a><0047>
<002b><002b><0048>
<002c><002c><0049>
<002d><002d><004a>
<002e><002e><004b>
<002f><002f><004c>
<0030><0030><004d>
<0031><0031><004e>
<0032><0032><004f>

可以表示为

<0003><0003><0020>
<0024><0032><0041>

快速谷歌搜索提出了CMap / CID字体规范

还有 beginbfchar / endbfchar 只需要两个参数(src和dest值) ,没有范围),基于CID的版本(此时您需要访问Adobe的角色ID表。它们是Acrobat / Reader安装的一部分,尽管需要阅读Reader以下载各种语言包(或套件或无论他们被称为什么)),还有其他各种你真正想要阅读的规范来了解它。

There are also beginbfchar/endbfchar which just take two parameters (src and dest values, no ranges), CID based versions (at which point you need to have access to Adobe's character ID tables. They're part of Acrobat/Reader installations, though Reader will need to be prodded into downloading the various Language Packs (or kits or whatever they're called)), and various other stuff you really out to read that spec to find out about.

这篇关于文本搜索PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆