【问题标题】:How do I get character offset information from a pdf document? [closed]如何从 pdf 文档中获取字符偏移信息? [关闭]
【发布时间】:2010-09-16 20:13:47
【问题描述】:

我正在尝试在网络应用程序中实现 pdf 的搜索结果突出显示。我有原始 pdf 文件和搜索结果中使用的小 png 版本。本质上我正在寻找这样的 api:

pdf_document.find_offsets('somestring')
# => { top: 501, left: 100, bottom: 520, right: 150 }, { ... another box ... }, ...

我知道可以从 pdf 中获取这些信息,因为 Apple 的 Preview.app 实现了这一点。

需要在 Linux 上运行并且理想情况下是开源的东西。我知道您可以在 Windows 上使用 acrobat 来执行此操作。

【问题讨论】:

  • 嗨 quackingduck,你能找到答案吗?如果是,请在此处发布。
  • @Thushan pdftohtml -xml => <text top="650" left="72" width="426" height="22" font="6">It also contains

标签: search pdf


【解决方案1】:

CAM::PDF 可以很好地完成几何部分,但有时在字符串匹配方面会遇到一些问题。该技术类似于以下经过轻微测试的代码:

use CAM::PDF;
my $pdf = CAM::PDF->new('my.pdf') or die $CAM::PDF::errstr;
for my $pagenum (1 .. $pdf->numPages) {
   my $pagetree = $pdf->getPageContentTree($pagenum) or die;
   my @text = $pagetree->traverse('MyRenderer')->getTextBlocks;
   for my $textblock (@text) {
      print "text '$textblock->{str}' at ",
            "($textblock->{left},$textblock->{bottom})\n";
   }
}

package MyRenderer;
use base 'CAM::PDF::GS';

sub new {
   my ($pkg, @args) = @_;
   my $self = $pkg->SUPER::new(@args);
   $self->{refs}->{text} = [];
   return $self;
}
sub getTextBlocks {
   my ($self) = @_;
   return @{$self->{refs}->{text}};
}
sub renderText {
   my ($self, $string, $width) = @_;
   my ($x, $y) = $self->textToDevice(0,0);
   push @{$self->{refs}->{text}}, {
      str => $string,
      left => $x,
      bottom => $y,
      right => $x + $width,
      #top => $y + ???,                                                                                 
   };
   return;
}

输出看起来像这样:

text 'E' at (52.08,704.16)
text 'm' at (73.62096,704.16)
text 'p' at (113.58936,704.16)
text 'lo' at (140.49648,704.16)
text 'y' at (181.19904,704.16)
text 'e' at (204.43584,704.16)
text 'e' at (230.93808,704.16)
text ' N' at (257.44032,704.16)
text 'a' at (294.6504,704.16)
text 'm' at (320.772,704.16)
text 'e' at (360.7416,704.16)
text 'Employee Name' at (56.4,124.56)
text 'Employee Title' at (56.4,114.24)
text 'Company Name' at (56.4,103.92)

从该输出中可以看出,字符串匹配会有点乏味,但几何图形很简单(可能除了字体高度)。

【讨论】:

    【解决方案2】:

    尝试查看 PdfLib TET http://www.pdflib.com/products/tet/

    (它不是免费的)

    法布里齐奥

    【讨论】:

      【解决方案3】:

      我认为您可以使用 Adob​​e Acrobat SDK 执行此操作,其 Linux 版本可以是 downloaded for free from Adobe。您可以将其用于extract text from PDFs,然后计算出偏移量。然后可以使用 Acrobat XML highlighting file 突出显示 PDF。这用于指定要突出显示的位置的哪些单词,并按如下方式馈送到 acrobat:

      http://example.com/a.pdf#xml=http://example.com/highlightfile.xml

      【讨论】:

        猜你喜欢
        • 2023-02-01
        • 1970-01-01
        • 2019-07-13
        • 2011-10-23
        • 1970-01-01
        • 1970-01-01
        • 2014-03-31
        • 2010-12-21
        相关资源
        最近更新 更多