【问题标题】:pdfbox get begintext section (BT ET) coordinatespdfbox 获取 begintext 部分(BT ET)坐标
【发布时间】:2011-10-11 18:26:36
【问题描述】:

有人可以帮我获取 pdf begintext 部分的真实像素坐标吗? 我正在使用 pdfbox 从 pdf 文件中检索文本,但现在我需要获取围绕该文本部分/段落的矩形。

$contents = $page->getContents();
$contentsStream = $page->getContents()->getStream();
$resources=$page->getResources();
$fonts = $resources->getFonts();
$xobjects = $resources->getImages();
$tokens=$contentsStream->getStreamTokens();
  • [PDFOperator{q}, COSFloat{690.48}, COSInt{0}, COSInt{0}, COSFloat{633.6}, COSInt{0}, COSInt{0}, PDFOperator{cm}, COSName{im1 }, PDFOperator{Do}, PDFOperator{Q},

  • PDFOperator{BT}, COSInt{1}, COSInt{0}, COSInt{0}, COSInt{1}, COSFloat{25.92}, COSFloat{588.48}, PDFOperator{Tm}, COSInt {99}, PDFOperator{Tz}, COSName{F30}, COSInt{56}, PDFOperator{Tf}, COSInt{3}, PDFOperator{Tr}, COSFloat{0.334}, PDFOperator{Tc}, COSString{Pospremanj}, PDFOperator {Tj}, COSInt{0}, PDFOperator{Tc}, COSString{e}, PDFOperator{Tj}, COSFloat{9.533}, PDFOperator{Tw}, COSString{ i}, PDFOperator{Tj}, COSFloat{6.062}, PDFOperator {Tw}, COSFloat{0.95}, PDFOperator{Tc}, COSString{ ciscenj}, PDFOperator{Tj}, COSInt{0}, PDFOperator{Tc}, COSString{e }, PDFOperator{Tj}, COSInt{1}, COSInt {0}, COSInt{0}, COSInt{1}, COSFloat{55.68}, COSFloat{539.76}, PDFOperator{Tm}, COSInt{0}, PDFOperator{Tw}, COSFloat{0.262}, PDFOperator{Tc}, COSString {uoè}, PDFOperator{Tj}, COSInt{0}, PDFOperator{Tc}, COSString{i}, PDFOperator{Tj}, COSFloat{5.443}, PDFOperator{Tw}, COSFloat{-2.145}, PDFOperator{Tc}, COSString{ zimslco}, PDFOperator{Tj}, COSInt{0}, PDFOperator{Tc}, COSString{g}, PDFO perator{Tj}, COSFloat{7.202}, PDFOperator{Tw}, COSFloat{-0.148}, PDFOperator{Tc}, COSString{ odmor}, PDFOperator{Tj}, COSInt{0}, PDFOperator{Tc}, COSString{a } , PDFOperator{Tj}, PDFOperator{ET},

  • PDFOperator{BT}, COSInt{1}, COSInt{0}, COSInt{0}, COSInt{1}, COSFloat{6.72}, COSFloat{513.12}, PDFOperator{Tm}, COSInt{0} , PDFOperator{Tw}, COSName{F30}, COSInt{14}, PDFOperator{Tf}, COSString{}, PDFOperator{Tj}, COSFloat{2.751}, PDFOperator{Tw}, ...

我想获得类似于 PrintTextLocations 函数对每个单词/字符所做的输出。 我可以获取底部和左侧坐标,但是如何获取宽度和顶部坐标?

PrintTextLocations:

  • 字符串[25.92,45.119995 fs=56.0 xscale=55.440002 height=40.208004 space=15.412322 width=36.978485]p 字符串[63.22914,45.119995 fs=56.0 xscale=55.440002 高度=40.208004 空间=15.412322 宽度=33.87384]o 字符串[97.43364,45.119995 fs=56.0 xscale=55.440002 高度=40.208004 空间=15.412322 宽度=30.824646]s 字符串[128.58894,45.119995 fs=56.0 xscale=55.440002 高度=42.168 空间=15.412322 宽度=33.87384]p 字符串[162.79344,45.119995 fs=56.0 xscale=55.440002 高度=42.168 空间=15.412322 宽度=21.566162]r 字符串[184.69026,45.119995 fs=56.0 xscale=55.440002 高度=42.168 空间=15.412322 宽度=30.824646]e 字符串[215.84557,45.119995 fs=56.0 xscale=55.440002 高度=42.168 空间=15.412322 宽度=49.286148]m ...

【问题讨论】:

    标签: pdf text coordinates pdfbox sections


    【解决方案1】:

    ...由于 BT 部分为您提供左下角坐标,您需要解析当前 BT 块中包含的所有单词/字母以获取所有其他坐标。 第一个字高+BT 底=顶,最大(左坐标+宽度)=右,最后一个字底=底坐标。

    我希望这对某人有所帮助...

    单个字母的示例字符串:

    string[32.94,35.099976 fs=8.0 xscale=1.0 height=4.4240003 space=2.2240002 width=3.959999]p
    

    提取、解析和准备的行:

    32.94,35.099976 fs=8.0 xscale=1.0 height=4.4240003 space=2.2240002 width=3.959999
    

    功能:

    /**
     * Parse single word / letter element
     *
     * @param string $str_raw  Extracted word string line.
     * @param string $str_elem Element of interest, word, char.
     * @param int    $pdf_w    Pdf page width.
     * @param int    $pdf_h    Pdf page height.
     * @param int    $pdf_d    Pdf page dpi.
     * @param int    $pdf_r    Pdf page relative dpi.
     *
     * @return array
     */
    function createRealCoordinates($str_raw, $str_elem, $pdf_w, $pdf_h, $pdf_d = 400, $pdf_r = 72)
    {
        $stringstrip = array('fs=', 'xscale=', 'height=', 'space=', 'width=');
        $string_info = str_replace($stringstrip, '', $str_raw);
    
        $coord_info = explode(' ', $string_info);
        $coord_xy   = explode(',', $coord_info[0]);
    
        $coord = array(
            'pdfWidth'  => $pdf_w,
            'pdfHeight' => $pdf_h,
            'pdfDpi'    => $pdf_d,
            'pdfRel'    => $pdf_r,
            'word'      => $str_elem,
    
            'x1' => null,
            'y1' => null,
            'x2' => null,
            'y2' => null,
    
            'fontSize'     => null,
            'xScale'       => null,
            'HeightDir'    => null,
            'WidthDir'     => null,
            'WidthOfSpace' => null,
        );
    
        // Left, Bottom coordinate.
        $coord['x1'] = ($coord_xy[0] / $pdf_r) * $pdf_d;
        $coord['y2'] = ($coord_xy[1] / $pdf_r) * $pdf_d;
    
        $coord['fontSize']     = $coord_info[1]; // font size.
        $coord['xScale']       = $coord_info[2]; // x size scale.
        $coord['HeightDir']    = $coord_info[3]; // height.
        $coord['WidthDir']     = $coord_info[5]; // word width.
        $coord['WidthOfSpace'] = ($coord_info[4] / $pdf_r) * $pdf_d; // width of space.
    
        // Right, Top coordinate.
        $coord['x2'] = $coord['x1'] + (($coord['WidthDir'] / $pdf_r) * $pdf_d);
        $coord['y1'] = $coord['y2'] - (($coord['HeightDir'] / $pdf_r) * $pdf_d);
    
        return $coord;
    }
    

    -matija kancijan

    【讨论】:

    • 如果可能的话,你能提供一个示例代码吗?我正在处理同样的问题,但我无法理解`第一个单词高度 + BT 底部 = 顶部,最大(左坐标 + 宽度)= 右,最后一个单词底部 = 底部坐标。`这部分。
    • 这东西有点老了 ;-)... 你应该提取所有“字符串”元素并为它计算矩形坐标。从那里您可以匹配内联元素、单词、行等...
    • 感谢您的回复 :) 我最终做到了这一点 :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-25
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多