Merging PDF files with PDFTK

0

1

I have a directory with PDF files that looks like this:

2016_AAA_SomeRandomText1.pdf

2016_BBB_SomeRandomText1.pdf

2016_AAA_SomeRandomText2.pdf

2016_BBB_SomeRandomText2.pdf

2016_AAA_SomeRandomText3.pdf

2016_BBB_SomeRandomText3.pdf

etc...

Note: SomeRandomText changes but it is in pairs.

So, I want to go through the folder via Windows CLI with a FOR loop and generate a PDF file for each pair of SomeRandomText with PDFTK. So, the output will be like this:

2016_AAA_SomeRandomText1.pdf + 2016_BBB_SomeRandomText1.pdf = 2016_SomeRandomText1.pdf

2016_AAA_SomeRandomText2.pdf + 2016_BBB_SomeRandomText2.pdf = 2016_SomeRandomText2.pdf

2016_AAA_SomeRandomText3.pdf + 2016_BBB_SomeRandomText3.pdf = 2016_SomeRandomText3.pdf

etc...

Here is what I have so far (assuming I am working in C:\user\pdfs):

FOR /R %I IN (*.pdf) DO pdftk 

Ramin Melikov

Posted 2016-03-28T14:47:54.210

Reputation: 41

Answers

-1

I’ll tell you how I did it in bash, and maybe you can translate it.

I use pdfunite (a little more direct than pdftk) but once you figure out the syntax, it’s easy enough.

for AFILE in `ls 2016*AAA*.pdf`
## For each of the files starting with 2016_AAA
do
   BFILE=`echo $AFILE | sed -e 's/AAA/BBB/'`
## I use stream editor to replace the AAA with BBB and define the
## new file name.  You can probably use SUBSTITUTE in Win
   pdftk $AFILE $BFILE cat output OUTPUT-$AFILE
## This will combine $AFILE and BFILE into OUTPUT-AFILE
done

Hopefully this will give you a start.

Jeff Dodd

Posted 2016-03-28T14:47:54.210

Reputation: 121