2

I want to automate the process of finding and replacing hrefs (not the anchor text) in 100's of PDF files in a Windows 8.1 OS.

I am happy to convert file formats so long as they can be re-compiled as a working PDF.

UPDATE: I have been able to find and replace only the href using PDFTK..

  1. pdftk foo.pdf output foo.tmp uncompress
  2. find and edit a link using a text editor
  3. pdftk foo.tmp output foo-updated.pdf compress

Now I need a cmdline tool that can automate step 2 by replacing foo.tmp values from a find and replace list.

  • possible duplicate stackoverflow.com/a/220791 – Paul Oct 12 '15 at 7:18
  • Please edit your question to include your operating system. This will allow you to get more focussed answers. – DavidPostill Oct 12 '15 at 7:51
  • Or try this code from this site: levien.zonnetjes.net/?q=replacepdftext Can be easily converted to batch or used directly from msys, cygwin. But not sure if it preserve the hyperlink – Paul Oct 12 '15 at 8:29
  • Thanks @Paul the levien.zonnetjes.net/?q=replacepdftext link has helped me move closer to an answer – darmi Oct 20 '15 at 3:47
1

I'll assume we are talking about Windows O.S. and with Batch tag you will reffer to batch-processing instead of Batch lang, anyways I'll advise that Batch is a limited language that only covers basic common system based tasks, it is not intended to read a non-plain text file such a PDF, it can't.

To read an specific format like a PDF an external parser/interpreter is need.

There is a good C# based solution that demonstrates how to read the data contained in a PDF in a human readable way:

https://stackoverflow.com/questions/2550796/reading-pdf-content-with-itextsharp-dll-in-vb-net-or-c-sharp

It uses iTextSharp library, which is open-source:

http://sourceforge.net/projects/itextsharp/

Alternatively, here is an VBS based solution from the Adobe user guide, it needs the COM objects installed from Adobe app.

http://wwwimages.adobe.com/content/dam/Adobe/en/devnet/indesign/sdk/cs6/scripting/InDesign_ScriptingGuide_VB.pdf

Note the chapters where you are interested for this task:

enter image description here

To assist you a little bit in VBS, here is an VBS script question that could helps you:

Option Explicit
Dim accapp, acavdocu
Dim pdf_path, bReset, Wrd_count
pdf_path="C:\LS\Test\Invoices\02_2011_PDF\rpt_Invoice_1.pdf"
'AcroExch is acrobat application object
Set accapp=CreateObject("AcroExch.App")
accapp.Show()

'Need to create one AVDoc object par displayed document
Set acavdocu=CreateObject("AcroExch.AVDoc")

'Opening the PDF
If acavdocu.Open(pdf_path,"") Then
acavdocu.BringToFront()
bReset=1 : Wrd_count = 0
'Find Text Finds the specified text, scrolls so that it is visible, and highlights it
Do While acavdocu.FindText("Invoice#", 1, 1, bReset)
bReset=0 : Wrd_count=Wrd_count+1
'Wait 0, 200
Loop
End If

accapp.CloseAllDocs()
accapp.Exit()
msgbox "The word 'Invoice#' was found " & Wrd_count & "times"
Set accap=nothing : Set accapp=nothing

But I'll advise apparently it seems don't work as expected, I taken it from this site:

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_26834618.html

| improve this answer | |
  • @fixer1234 thanks for the gentle suggestion. The reason why I linked that url its because the OP could see the script and copy it, an image is not a copyable text then If I provide an image I'm providing not too efficient help, and If I share the script as text then I'm not sure If I am applying emphasis on my answer because is not clear the language(s) accepted by the OP. I think we all need to do an exception if we found an answer on these circunstances because the link to that url could be wrong only if seen by and specific perspective. thanks again. – ElektroStudios Oct 12 '15 at 9:02
  • 1
    In that case, include the instruction to show all to find the script. I, too, assumed the script would be in the unavailable answer and didn't think to expand the question. Personally, I think copying the script would be fine. That puts all of the information here and you can credit Experts Exchange without sending people there, where they'll be confused by the membership stuff. You cover a lot of territory in your answer and the OP can use what they want. It's more convenient to see the content here and decide you don't need it than to pursue a link to come to the same conclusion. My 2 cents. – fixer1234 Oct 12 '15 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.