admin管理员组文章数量:1396787
I have a PDF where I have a question which is in darker text colour and the answer is in lighter text colour. Other than text colour there is no other logic I can use to separate the question & answer texts.
I'm using tabulapdf to extract the content from the PDF, but in PHP
application
Appreciate any suggestions or guidance. Thanks!
I have a PDF where I have a question which is in darker text colour and the answer is in lighter text colour. Other than text colour there is no other logic I can use to separate the question & answer texts.
I'm using tabulapdf to extract the content from the PDF, but in PHP
application
Appreciate any suggestions or guidance. Thanks!
Share Improve this question asked Mar 26 at 17:37 Saumini NavaratnamSaumini Navaratnam 8,8905 gold badges46 silver badges74 bronze badges 4 |1 Answer
Reset to default 1Tabula like many PDF text extractions will not see the PDF text with its surface colours but simply the core monochromatic plain text.
The main reason is that PDF colouring can be applied many ways unlike say HTML where the style is cascading so may be defined purely for text in an area/object via tags in a .css file or a header <style>
or inline <p style="color: rgb(255,0,0)">
or older pre HTML5 <font color="red">
.
The nice thing about HTML is the colour is text related and defined before the text it applies to.
The Problem with PDF is the colour may be after the text and is NOT only text related. So more like colour all objects in a division.
What about the shown text? Well it is actually, in this one case, fairly simple to see the colours. HOWEVER the page objects are first set to red = 1 0 0 rg
before any Begin Text starts thus is the default color for the first line. Then black = 0 0 0 rg
for the next line. Green = 0 1 0
and then blue binary text is different type of colour 0 0 1 scn
. Thus we are lucky for this simple plain text file. We look for a colour then can extract the text after it.
stream
q /GS0 gs 1 0 0 1 222 775.68008 cm 1 0 0 rg
BT
.001 Tc /F0 12.0005 Tf <0037004c0057004f0048> Tj
0 0 0 rg 1 0 0 1 36.0072 0 Tm <000300200003> Tj
0 1 0 rg 1 0 0 1 57.611497 0 Tm <002b0048004f004f0052> Tj
0 0 0 rg 1 0 0 1 93.6187 0 Tm <000f0003> Tj
0 0 1 scn 1 0 0 1 108.0216 0 Tm <003a00520055004f0047> Tj
0 0 0 scn 0 Tc 1 0 0 1 144.0288 0 Tm <0004> Tj
ET
The simplest way to see what text is what colour is convert PDF into HTML which is easier to reverse so as to trace the colour differences.
There are many utilities to convert PDF to HTML in one single command.
Poppler does not work in this case:
But MuTool does (PyMuPDF would too). >mutool convert -o colours.html colours.pdf
<p style="top:56.6pt;left:222.0pt;line-height:12.0pt"><tt><span style="font-family:F1,monospace;font-size:12.0pt;color:#ff0000">Title</span></tt><tt><span style="font-family:F1,monospace;font-size:12.0pt;color:#000000"> = </span></tt><tt><span style="font-family:F1,monospace;font-size:12.0pt;color:#00ff00">Hello</span></tt><tt><span style="font-family:F1,monospace;font-size:12.0pt;color:#000000">, </span></tt><tt><span style="font-family:F1,monospace;font-size:12.0pt;color:#0000ff">World</span></tt><tt><span style="font-family:F1,monospace;font-size:12.0pt;color:#000000">!</span></tt></p>
<p style="top:69.5pt;left:70.9pt;line-height:12.0pt"><tt><span style="font-family:F1,monospace;font-size:12.0pt;color:#000000"># Tabula</span></tt></p>
Yet again, no method is perfect as you need to note the source was Courier (monospaced) Fonts: CIDFont+F1 (TrueType (CID); Identity-H; embedded)
but the Browser HTML default.css style is SansSerif (for font-family:F1,monospace;).
Others may fail to see the colours correctly. So remember I said the default line colour was Red! I wont mention names but here is another apps attempt!
本文标签: phpExtract text colour from PDFStack Overflow
版权声明:本文标题:php - Extract text colour from PDF - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744135413a2592372.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
java -jar -Dfile.encoding=utf-8 ../java/tabula-1.0.5-jar-with-dependencies.jar --no-spreadsheet -f TSV -a 120.63,224.98,145.26,384.23 -p 1 "/private/tmp/sample.pdf"
– Saumini Navaratnam Commented Mar 26 at 18:17