Create bookmark list automatically

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rjobaan
Posts: 30
Joined: 14 Jul 2012 11:16

Create bookmark list automatically

#1 Post by rjobaan » 15 Jul 2012 12:56

Next challenge,
i need to create a bookmark list automatically without making any manual adjustment

First of all i need to get the bookmarks from the specific pdf files

jpdfbookmarks_cli.exe --dump "Report 1.pdf" > "bookmarks1.txt"
jpdfbookmarks_cli.exe --dump "Report 2.pdf" > "bookmarks2.txt"
jpdfbookmarks_cli.exe --dump "Report 3.pdf" > "bookmarks3.txt"

BOOKMARKS1.txt content:

KPI/1,Black,notBold,notItalic,open,TopLeftZoom,1,0,0.0

BOOKMARKS2.txt content:(in front of the last three SALES should be a TAB)

SALES/1,Black,notBold,notItalic,closed,TopLeftZoom,1,0,0.0
SALES UK/1,Black,notBold,notItalic,open,TopLeftZoom,63,8,0.0
SALES VS/3,Black,notBold,notItalic,open,TopLeftZoom,63,8,0.0
SALES NL/5,Black,notBold,notItalic,open,TopLeftZoom,63,8,0.0

BOOKMARKs3.txt content:

MISC/1,Black,notBold,notItalic,open,TopLeftZoom,0,0,0.0

When merging above pdf files with pdftk the bookmarks are not taken into account.
So i need to add them, but i can not just combine the three txt files because the pages are ofcourse changed.
The bookmark page is the number behind "/" sine. So SALES NL is bookmarked on page 5.

How can I create below file?
(in front of the last three SALES should be a TAB)

BOOKMARKS MAIN.txt should be:

KPI/1,Black,notBold,notItalic,open,TopLeftZoom,1,0,0.0
SALES/2,Black,notBold,notItalic,closed,TopLeftZoom,1,0,0.0
SALES UK/2,Black,notBold,notItalic,open,TopLeftZoom,63,8,0.0
SALES VS/4,Black,notBold,notItalic,open,TopLeftZoom,63,8,0.0
SALES NL/6,Black,notBold,notItalic,open,TopLeftZoom,63,8,0.0
MISC/7,Black,notBold,notItalic,open,TopLeftZoom,0,0,0.0

When that file is created i can add the bookmarks into main pdf file :
jpdfbookmarks_cli.exe "Main Report.pdf" --apply "bookmarks main.txt" --out "Main Report_BOOK.pdf"

Hopefully you guys can help me out again.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Create bookmark list automatically

#2 Post by foxidrive » 15 Jul 2012 13:32

This seems to work here. It creates fullbookmarks.txt

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /f "tokens=2 delims=/," %%a in ('type "bookmarks1.txt"') do (
set /a a=%%a
)
for /f "tokens=2 delims=/," %%a in ('type "bookmarks2.txt"') do (
set /a b=%%a
)
for /f "tokens=2 delims=/," %%a in ('type "bookmarks3.txt"') do (
set /a c=%%a
)

for /f "delims=" %%a in ('type "bookmarks1.txt"') do (
>> fullbookmarks.txt echo %%a
)
for /f "tokens=1,2,* delims=/," %%a in ('type "bookmarks2.txt"') do (
set /a "num=%%b+a"
>> fullbookmarks.txt echo %%a/!num!,%%c
)
for /f "tokens=1,2,* delims=/," %%a in ('type "bookmarks3.txt"') do (
set /a "num=%%b+a+b"
>> fullbookmarks.txt echo %%a/!num!,%%c
)

rjobaan
Posts: 30
Joined: 14 Jul 2012 11:16

Re: Create bookmark list automatically

#3 Post by rjobaan » 15 Jul 2012 13:42

wow, im really jealous how quick you can create the code.

I will look into it right away.
Can you give me some more explanation about the code?

like what does "setlocal enabledelayedexpansion" mean?
tokens?

rjobaan
Posts: 30
Joined: 14 Jul 2012 11:16

Re: Create bookmark list automatically

#4 Post by rjobaan » 15 Jul 2012 13:59

it is working. thank you very much.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Create bookmark list automatically

#5 Post by foxidrive » 15 Jul 2012 14:07

rjobaan wrote:Can you give me some more explanation about the code?
like what does "setlocal enabledelayedexpansion" mean?


That enables a method of using variables within a loop. You use !var! instead of %var% but a drawback is that ! characters will break the code.

tokens?


tokens and delims is a way of splitting up a string.

say you have this string
KPI/12,Black,notBold,notItalic,open,TopLeftZoom,1,0,0.0

and you state that the delimiters are / and , and to only use tokens 1 and 2 and then * (which is the remainder of the line).
The line is separated into KPI in token 1 and 12 in token 2 and the rest of the line in token 3 - Black,notBold,notItalic,open,TopLeftZoom,1,0,0.0

So the code first checks the number in the token 2 in each line and saves the highest number reached, per file.

After doing that to three files it then opens the first file and simply copies the info line by line into fullbookmarks.txt

When opening the second and third files it adds the the current number in token 2 plus the highest number reached in the previous file
and then echoes the first token in the line %%a, the modified number, and the third token (rest of line) in %%c. Note that I had to re-add the first / and , characters because they were removed in the splitting process.

The for in do loops process each line in the same manner and so the final file is built up.
A drawback to using for in do loops is that blank lines are ignored, but this doesn't affect your source files.

I hope that explains the essence of what the code does - ask if you need further clarification.

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Create bookmark list automatically

#6 Post by Liviu » 15 Jul 2012 14:52

rjobaan wrote:BOOKMARKS MAIN.txt should be:
...
KPI/1,Black,notBold,notItalic,open,TopLeftZoom,1,0,0.0
SALES/2,Black,notBold,notItalic,closed,TopLeftZoom,1,0,0.0

I believe foxidrive's code does what you asked for, yet wonder if that's what you really wanted ;-)

Don't know much about the PDF internals, but it looks to me like the page number for 'SALES' would need to be offset by the total number of pages in report1.pdf. Simply incrementing the page number of the last bookmark in the previous file won't necessarily work - unless you know for a fact that all those PDFs have a bookmark on their last page.

Liviu

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Create bookmark list automatically

#7 Post by foxidrive » 15 Jul 2012 15:11

I think you're right.

I wonder if the pdf program has an option to include bookmarks. I mean the program which processed the three files into one file earlier in the procedure.

Edit: I checked and it seems not to have the option from all PDF parts.

There's another option here: http://www.pdfmerge.com/

and more stuff here. Surely one of them will work better.

http://www.google.com.au/search?hl=en&q ... gle+Search

rjobaan
Posts: 30
Joined: 14 Jul 2012 11:16

Re: Create bookmark list automatically

#8 Post by rjobaan » 15 Jul 2012 15:32

I think you are right because you don't know how many pages are coming after sales nl right, matbe that part has 3 pages then MISC should be 9 right
Is pdf merge also useable with command line?

I also check out pdf combine command line, but that messed up the bookmarks. For example you merge two files report1.PDF with bookmark BK/1 and BK/3 and report2.PDF with BMK/1 and BMK/2, the merge file look like
BK/1, BK/3,BK/4,BK/5,BMK/6, BMK/7, As you see the first file is merged twice. Very strange.

Do you have the same?
I will post code tomorrow.

rjobaan
Posts: 30
Joined: 14 Jul 2012 11:16

Re: Create bookmark list automatically

#9 Post by rjobaan » 15 Jul 2012 23:58

@Foxidrive, when searching for counting number of pages i came along a code from yourself.

@echo off
setlocal enabledelayedexpansion
set apgpc=C:\Windows\System32\apgetpagecount.exe
for /f "tokens=*" %%z in ('dir /a:d /b /s') do (
for /f "tokens=*" %%a in ('dir /b /s "%%z\*.pdf"') do (
for /f "tokens=2 delims=:" %%b in (
'%apgpc% "%%a"^|find "PageCount:"') do (
echo/%%a : PageCount %%b
set /a cnt=!cnt!+%%b
)
set /a total+=!cnt!
set /a subcnt+=!cnt!
set cnt=0
)
if not defined subcnt echo/----------------------------------------------
if not defined subcnt echo/Page Count for %%z: !subcnt!
if not defined subcnt set subcnt=
if not defined subcnt echo/----------------------------------------------
)
echo/Total Page Count: %total%
pause


So how can i add this into code for creating bookmarks.
The first report has always only one page.
So we only need to determine how many pages report 2 has, so we know when to start bookmark MISC right?

But im wondering if apgetpagecount.exe is for free?
Maybe there is another opensource tool which can count pages in pdf

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Create bookmark list automatically

#10 Post by foxidrive » 16 Jul 2012 07:17

I found this that creates pagecount.txt with the 'filename.pdf|numofpages'

save it as "pagecount.vbs"

Code: Select all

' By Chanh Ong
'File: pdfpagecount.vbs
' Purpose: count pages in pdf file in folder
Const OPEN_FILE_FOR_READING = 1

Set gFso = WScript.CreateObject("Scripting.FileSystemObject")
Set gShell = WScript.CreateObject ("WSCript.shell")
Set gNetwork = Wscript.CreateObject("WScript.Network")

  directory="."
  set base=gFso.getFolder(directory)
  call listPDFFile(base)
 
Function ReadAllTextFile(filespec)
   Const ForReading = 1, ForWriting = 2
   Dim f
   Set f = gFso.OpenTextFile(filespec, ForReading)
   ReadAllTextFile =   f.ReadAll
End Function

function countPage(sString)
  Dim regEx, Match, Matches, counter, sPattern
  sPattern = "/Type\s*/Page[^s]"  ' capture PDF page count
  counter = 0

  Set regEx = New RegExp         ' Create a regular expression.
  regEx.Pattern = sPattern    ' Set pattern "^rem".
  regEx.IgnoreCase = True         ' Set case insensitivity.
  regEx.Global = True         ' Set global applicability.
  set Matches = regEx.Execute(sString)   ' Execute search.
  For Each Match in Matches      ' Iterate Matches collection.
    counter = counter + 1
  Next
  if counter = 0 then
    counter = 1
  end if
  countPage = counter
End Function

sub listPDFFile(grp)
  Set pf = gFso.CreateTextFile("pagecount.txt", True)
for each file in grp.files
    if (".pdf" = lcase(right(file,4))) then
      larray = ReadAllTextFile(file)
      pages = countPage(larray)
'      wscript.echo "The " & file.name & " PDF file has " & pages & " pages"
      pf.WriteLine(file.name&"|"&pages)
    end if
next
  pf.Close
end sub


and given that you said the first PDF will always have only 1 page then this could work (I have not tested it):


Code: Select all

@echo off
setlocal enabledelayedexpansion
cscript /nologo "pagecount.vbs"

for /f "tokens=1,2 delims=|" %%a in ('type "pagecount.txt"') do (
if /i "%~1"=="report 2.pdf" set b=%%b
)
del pagecount.txt

for /f "delims=" %%a in ('type "bookmarks1.txt"') do (
>> fullbookmarks.txt echo %%a
)
for /f "tokens=1,2,* delims=/," %%a in ('type "bookmarks2.txt"') do (
set /a "num=%%b+1"
>> fullbookmarks.txt echo %%a/!num!,%%c
)
for /f "tokens=1,2,* delims=/," %%a in ('type "bookmarks3.txt"') do (
set /a "num=%%b+1+b"
>> fullbookmarks.txt echo %%a/!num!,%%c
)

rjobaan
Posts: 30
Joined: 14 Jul 2012 11:16

Re: Create bookmark list automatically

#11 Post by rjobaan » 16 Jul 2012 12:28

great i will look into it.

rjobaan
Posts: 30
Joined: 14 Jul 2012 11:16

Re: Create bookmark list automatically

#12 Post by rjobaan » 18 Jul 2012 11:01

how can i change that VBS so its counting pdf in different folder?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Create bookmark list automatically

#13 Post by foxidrive » 18 Jul 2012 12:09

It's a common issue that people request something and then want changes made after they have a working solution.

The problem is that in *many* cases with batch files it requires large changes - so if you are going to make changes then please state the exact situation so that further rewrites and testing aren't needed.

rjobaan
Posts: 30
Joined: 14 Jul 2012 11:16

Re: Create bookmark list automatically

#14 Post by rjobaan » 18 Jul 2012 12:19

i thought will start simple and built the batch file from their, but getting complicated

So i will start new treads , will current code and more explanation of new adjustment.

Post Reply