get string info

In any Acrobat '.pdf' file you can find the number of pages (/N) in the header
How do I retrieve this number and I want to output this number to another file in VB6
How do I programm this in VB??
hschaakeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mcriderCommented:
Go to http://wotsit.org/ and do a search on PDF.  The PDF file format is there...



Cheers!
0
hschaakeAuthor Commented:
Edited text of question.
0
mcriderCommented:
Here you go...


Put the following function in your program, and you can call it this way:

Dim NumOfPages as Long
NumOfPages = GetPDFPages("C:\windows\desktop\pdfspec.pdf")


Cheers!


THE CODE:


    Function GetPDFPages(FileName As String) As Long
        Dim fNum As Long
        Dim lBuf As String
       
        On Error Resume Next
        fNum = FreeFile
        GetPDFPages = 0
        Err = 0
        Open FileName For Input Access Read As #fNum
        If Not Err = 0 Then Exit Function
        Do While Not EOF(fNum)
            Line Input #fNum, lBuf
            lBuf = Trim$(lBuf)
            If UCase$(Left$(lBuf, 2)) = "/N" Then
                GetPDFPages = CLng(Mid$(lBuf, 3))
                Exit Do
            End If
        Loop
        Close fNum
    End Function

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

vikiingCommented:
>>      lBuf = Trim$(lBuf)

That's totally unuseful; as the number of pages is taken from bytes 4 to 7, ¿what is the idea about removing right blanks?

0
mcriderCommented:
Viking,

Not totally unuseful... Some PDF writers in PDF 1.2 put spaces in the header to indent it.  If the spaces are not taken out, then the NEXT line does not properly work... As it stands, the code I proposed works on all versions of PDF files.

Cheers!
0
vikiingCommented:
Clng() function works with trailing or leading spaces, or without them; so, I don't see the reason of trimming.

If line comes as

/N 100  (w/o spaces)

    or

/N 100  (with spaces)

the result will be exactly the same, as trailing blanks don't affect conversion functions.


0
mcriderCommented:
vikiing,

You can't just do:  x=clng("/N 123")
or you will get "run-time error 13 type mismatch"

My code parses everything correctly and it works...

Besides, the trim is used a part of finding the "/N" line, not for doing the clng...


Cheers!
0
vikiingCommented:
>>You can't just do:  x=clng("/N 123")

Of course, you can't; that's why you use Mid$(lbuf,3) to get rid of "/N", but I didn't question that point, my friend.

>My code parses everything correctly and it works...

Nobody said your code does not work (and yes, it does) but solely the use of Trim$() in Lbuf is totally unuseful. If you apply Clng() to Mid$(Lbuf,3), the argument to Clng() (w/o trimming)may contain:
a) A number (a sequence of digits) plus trailing blanks
b) A number, without blanks.

Both situations depends on if line has space filling or not. As Clng() (and many other conversion functions, like Val(), Cint(), Csng(), Cdbl(), etc.) do the same job, with or without trailing blanks, Clng() doesn't care if expression has blanks or not.

Under that point of view, ¿what is the benefit of trimming Lbuf before applying Clng()?

Yes, I know, now you'll say "the trim is fast, and it adds almost nothing". First of all: string-related procedures are always slow (more or less, depending on the length of the string), but, they're always lengthy operations. Sure, is adds ALMOST nothing of time; but if you are like me, a fighter against unuseful code, you would remove that dummy instruction, ¿wouldn't you?
0
mcriderCommented:
vikiing, You still don't get it...

The header record of some versions of PDF look like this:

<<
Linearized 1
/O 2246
/H [ 14836 14346 ]
/L 5574625
/E 154780
/N 581
/T 5133644
>>
endobj

AND... Some look like this:
<<
     Linearized 1
     /O 2246
     /H [ 14836 14346 ]
     /L 5574625
     /E 154780
     /N 581
     /T 5133644
>>
endobj

If you are reading a PDF file that happens to be formatted in the second format, and don't do the trim, line 14:
   If UCase$(Left$(lBuf, 2)) = "/N" Then
WILL NOT PROPERLY FIND the /N record.


Cheers!
0
hschaakeAuthor Commented:
Thanks mcrider it's a very good answer on my question.
0
mcriderCommented:
Glad I could help! Thanks for the points!


Cheers!
0
vikiingCommented:
Good for McRider; now I've catched the way he spoke about justification.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.