PDFBox get coordonnées beginText section (BT ET)
-
22-10-2019 - |
Question
Quelqu'un peut-il me s'il vous plaît aider à obtenir les coordonnées réelles de pixels pour pdf beginText sections? J'utilise PDFBox pour récupérer des textes à partir de fichiers PDF, mais maintenant besoin d'obtenir i rects sorounding que les sections de texte / paragraphes.
$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}, {Q} PDFOperator,
-
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 {} UOE, PDFOperator {} Tj, COSInt {0}, {Tc} PDFOperator, COSString {i}, {PDFOperator Tj}, {COSFloat 5,443}, {PDFOperator Tw}, {COSFloat -2,145}, {Tc} PDFOperator, COSString {} zimslco, PDFOperator {} Tj, COSInt {0}, {Tc} PDFOperator, COSString {g}, PDFOperat ou {Tj}, {COSFloat 7,202}, {PDFOperator Tw}, {COSFloat -0,148}, {Tc} PDFOperator, COSString {} odmor, PDFOperator {} Tj, COSInt {0}, {Tc} PDFOperator, COSString {a} , PDFOperator {Tj}, {PDFOperator ET}
-
PDFOperator {BT}, COSInt {1}, COSInt {0}, COSInt {0}, COSInt {1}, COSFloat {6,72}, COSFloat {513,12}, PDFOperator {Tf}, COSInt {0} , PDFOperator {} Tw, COSName {} F30, COSInt {14}, {Tf PDFOperator}, {} COSString, PDFOperator {Tj}, {COSFloat 2,751}, {PDFOperator Tw}, ...
je voudrais obtenir une sortie quelque chose comme fonction PrintTextLocations fait pour chaque mot / caractère. Je peux obtenir en bas et coordonner gauche, mais la façon d'obtenir la largeur et coordonnée supérieure?
PrintTextLocations:
- string [25.92,45.119995 fs = 56,0 xscale = 55,440002 40,208004 = hauteur space = 15,412322 36,978485 width =] p string [63.22914,45.119995 fs = 56,0 xscale = 55,440002 40,208004 = hauteur = 15,412322 espace width = 33,87384] o string [97.43364,45.119995 fs = 56,0 xscale = 55,440002 40,208004 = hauteur space = 15,412322 30,824646 width =] s string [128.58894,45.119995 fs = 56,0 xscale = 55,440002 hauteur = 42,168 space = 15,412322 width = 33,87384] p string [162.79344,45.119995 fs = 56,0 xscale = 55,440002 hauteur = 42,168 space = 15,412322 21,566162 width =] r string [184.69026,45.119995 fs = 56,0 xscale = 55,440002 hauteur = 42,168 space = 15,412322 30,824646 width =] e string [215.84557,45.119995 fs = 56,0 xscale = 55,440002 hauteur = 42,168 space = 15,412322 49,286148 width =] m ...
La solution
... en tant que section BT vous donne les coordonnées en bas à gauche, vous devez analyser tous les mots creux / lettres contenues dans le bloc courant BT pour obtenir toutes les autres coordonnées. hauteur du premier mot + BT = bas en haut, max (coordonnées gauche + largeur) = droite, dernier fond de mot = coordonnée bas.
J'espère que cela aide quelqu'un ...
Exemple chaîne pour une seule lettre:
string[32.94,35.099976 fs=8.0 xscale=1.0 height=4.4240003 space=2.2240002 width=3.959999]p
Extrait, analysé et préparé ligne:
32.94,35.099976 fs=8.0 xscale=1.0 height=4.4240003 space=2.2240002 width=3.959999
Fonction:
/**
* 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