<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
Thanks for your advice & help Mark,<br>
<br>
Typical, after spending ages searching, I finally found a couple of
pascal solutions this morning once I googled with the word Delphi
instead of freepascal/lazarus. Then checked and of course freepascal
has the same functionality.<br>
<br>
The first is reading the file into a string list and then using pos
to see if the search term exists. This is fairly simple and may meet
my requirements.<br>
<br>
The second is searchbuf which appears to require an understanding of
pointers - I've only just got my head around creating my own classes
so I may need to study a little more for this one!<br>
<br>
However, both still require converting the pdf file to text first
using pdftotext. So I'll keep on looking for a pure pascal solution.<br>
<br>
Thanks & best regards,<br>
<br>
Martin Collins<br>
<br>
Martin Collins wrote:
<br>
<blockquote type="cite" style="color: #000000;">Hi,
<br>
<br>
I'm writing a little personal program in Lazarus that manages pdf
files. One of the things I want to do is search for text/phrases
within the pdfs. Has anybody tried to do this before and if so
what is the best (easiest) way you've come across?
<br>
<br>
I've detailed what I've been doing below, but this is for
background information, as after messing about with it for a
couple of days I am not so sure this is the most sensible way to
go about this even if I can get it work. The awk count command
detailed below was just me trying out a proof of concept and for
the real search I was planning on it being slightly more
sophisticated, but failed at the first hurdle!
<br>
<br>
I will appreciate your opinions and experiences please. Many
thanks.
<br>
<br>
Martin Collins
<br>
<br>
Free Pascal Compiler version 2.6.2-5 [2013/07/25] for x86_64
<br>
Lazurus SVN 1.3
<br>
Awk - GNU Awk 4.0.1
<br>
<br>
<br>
I'm using Linux and have access to all the opensource goodies that
offers. I Googled for a pure pascal solution and did not find
anything. So over the last couple of days I been experimenting
with pdftotext and then awk on the text files, both executed
through TProcess.
<br>
</blockquote>
<br>
One minor warning for background. On a given distro, the PDF-related
utilities usually use a single underlying library. So if you come
across a situation where you're having problems extracting content,
it can be more useful to look at a system upgrade than spending time
trying to hack in updated versions of utilities such as pdftotext.
<br>
<br>
<blockquote type="cite" style="color: #000000;">Working on the bash
command line awk is fine but it seems to play up when executed
through TProcess. I think it's an awk (or stupid me) problem
rather than a TProcess (note: I am an awk novice and not an
experienced programmer in general!).
<br>
<br>
The bash command line awk instruction (to count the number of
search string instances) -
<br>
<br>
awk '$1 ~ <i class="moz-txt-slash"><span class="moz-txt-tag">/</span>searchstring<span
class="moz-txt-tag">/</span></i> {++c} END {print c}' FS=:
textfile.txt
<br>
<br>
In a simple pascal program to replicate the above, this works;
<br>
<br>
...
<br>
aString := 'awk ''$1 ~ <i class="moz-txt-slash"><span
class="moz-txt-tag">/</span>searchstring<span
class="moz-txt-tag">/</span></i> {++c} END {print c}'' FS=:
textfile.txt';
<br>
AProcess.CommandLine := aString;
<br>
AProcess.Execute;
<br>
</blockquote>
<br>
I'd suggest reading the file back into a stringlist, and
manipulating it in Pascal. There might be efficiency problems if
you're dealing with <i class="moz-txt-slash"><span
class="moz-txt-tag">/</span>really<span class="moz-txt-tag">/</span></i>
big files, but that way you'll be able to move forward and backward
in the file if you want context, have a chance at handling UTF-8
properly and so on.
<br>
<br>
AWK was all very well when it was the only tool available, and I'm
generally defensive of Perl. But if the main program is already
written in Pascal you might as well use it for the text handling as
well.
<br>
<br>
<span class="moz-txt-tag">-- <br>
</span>Mark Morgan Lloyd
<br>
markMLl .AT. telemetry.co .DOT. uk
<br>
<br>
[Opinions above are the author's, not those of his employers or
colleagues]
</body>
</html>