Frage

Kann mir bitte jemand helfen, echte Pixelkoordinaten für PDF -Begintext -Abschnitte zu erhalten? Ich verwende PDFBox, um Texte aus PDF -Dateien abzurufen, aber jetzt muss ich die Rektionen, die diese Textabschnitte/-absätze angezeigt haben, abrufen.

$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}, pdfoper {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}, cossstring {Zimslco}, Pdfoperator {tc} , Cosint {0}, pdfoperator {tc}, cosstring {g}, pdfoperator {tj}, cosfloa T {7.202}, pdfoperator {tw}, cosfloat {-0.148}, pdfoperator {tc}, cosstring {odmor}, pdfoperator {tj}, cossing {0}, pdfoperator {tc}, cossstring {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}, {tw {tw}, Cosname {f30}, cosint {14}, pdfoperator {tf}, cosstring {}, pdfoperator {tj}, cosfloat {2.751}, pdfoperator {tw}, ...

Ich möchte, dass die Funktion der PrintTextLocations -Funktion für jedes Wort/Charakter ausgibt. Ich kann die Boden- und Linkenkoordinate bekommen, aber wie kann ich Breite und obere Koordinate bekommen?

PrintTextLocations:

  • String [25.92,45.119995 FS = 56,0 XScale = 55,440002 Höhe = 40,208004 Space = 15,412322 Breite = 36,978485] = 56,0 xscale = 55,440002 Höhe = 40,208004 Raum = 15,412322 Breite = 30,824646] = 42,168 Raum = 15,412322 Breite = 21,566162] R -String [184,69026,45.119995 FS = 56,0 XSCALE = 55,440002 Höhe = 42,168 Raum = 15.412322 Width = 30,82466] E String [215,8457,45.119999999999999999999999999999995 FS = 56] E String [215,8457,45.11999999999999999999999999995 FS = 56] E String [215.8457,45.11999999999999999999999999999999995) 55,1055,55.0 56] = 49,286148] m ...
War es hilfreich?

Lösung

... Wie der BT -Abschnitt Ihnen unten linke Koordinaten gibt, müssen Sie alle Wörter/Buchstaben, die im aktuellen BT -Block enthalten sind, durchführen, um alle anderen Koordinaten zu erhalten. Erstes Worthöhe + Bt unten = oben, max (linke Koordinate + Breite) = rechts, letztes Wort unten = Bodenkoordinate.

Ich hoffe das hilft jemandem ...

Beispiel Zeichenfolge für einen einzelnen Buchstaben:

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

Extrahiert, analysiert und vorbereitete Linie:

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

Funktion:

/**
 * 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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top