Skip to main content
Known Participant
January 8, 2017
Answered

How can I save PDF as single page TIFF's, or JPEG's

  • January 8, 2017
  • 4 replies
  • 9478 views

I have been saving single pages of PDF's using the GUI, but it would be much faster to use Java.

I  can't work out how to set the save path, and I also need error trap any failed TIFFS, and save as JPEG's instead. Acrobat won't save TIFFS larger than the international size. Also need to increment each file name to match the page number, please.

So far, I've got...

set tAT to POSIX path of ((path to desktop as text) & "Temporary Acrobat TIFFS")

tell application "Adobe Acrobat"

  activate

  tell document 1

  set theTempName to name of document 1

  set Params to ""

  set Params to Params & "var dEnd = (this.numPages-1);" & return as text

  set Params to Params & "this.extractPages(0,dEnd);" & return as text

  set Params to Params & "var cFlName = this.documentFileName ;" & return as text

  set Params to Params & "save to :   ((path to desktop as text) + \"Temporary Acrobat TIFFS\"   + \":\"+ cFlName) using: \".tiff\";" & return --< this fails, correct format needed.

  #set Params to Params & "var this.closeDoc(false);"   as text

  do script Params

  end tell

end tell

This topic has been closed for replies.
Correct answer Karl Heinz Kremer

Thank you Karl.

I did say at the end of my post, that I had a thought you might mens to wrap the '/' in its own quotes, and of course you did. Once I changed my code, viola!. It worked.

Until I tried to save it!!!

I  CAN'T  use "/Users/username/Desktop/" instead of just "/Desktop/" because I don't know the name of the user. It's going to be used an another machine, so I require a versatile path name. Surely there's something in Java that allows this?

There IS a Desktop in MacOS, 'path to desktop as text' is a common used command when accessing the desktop and automatically generates the Desktop path, for the user.

However, is this the reason the new file gives a 'can't save' error?  Or, is the file actually locked?

AND, I have checked the new file path, just before trying to save the referred file. I got up early this morning to read through any  Java Script documentation I could, but could not find answers.

Regards

Santa


There is no "/Desktop" folder - you need to read exactly what I typed. I did not say that there was no Desktop folder. That's different, and when it comes to programming, these small differences are important. When it comes to Unix paths (or what you refer to as Posix paths in AppleScript), a path that start with a '/' always means that it is in the "root" directory of the disk. What I said was that there is no "Desktop" folder in the root directory, which does not mean that there is no Desktop folder in a user's home directory. Every user on Mac OS does have a Desktop folder, but that is referenced differently (e.g. /Users/username/Desktop).

Take a look at the App.getPath() method: Acrobat DC SDK Documentation

This is all there is in terms of asking Acrobat about user specific (or application specific) paths. Unfortunately, there is no function to get the user's Desktop. There is however a function to get the user's Documents directory, which on a Mac is on the same level as the user's Desktop. This means you could get the Documents directory and then just replace "Documents" with "Desktop":

var pathToDesktop = app.getPath("user", "documents").replace("Documents", "Desktop");

This is dangerous, because the next version of Mac OS X could move Documents, or a user could have reconfigured Mac OS X to store Documents in a different location. This is one area where you could use your AppleScript wrapper to your advantage and get the path to the desktop as a Posix path. (e.g. set pp to POSIX path of (path to desktop) as text )

As to why you are getting error when trying to save - until you fix your path problem, it's impossible to say if that is caused by a non-existing folder ("/Desktop") or something else.

4 replies

Legend
January 12, 2017

It isn't clear whether you ever verified the path name syntax or whether you've just stuck with /Desktop... In reply 10 /Desktop was used as an example "Let's say it was..."

Legend
January 11, 2017

No, but you can use AppleScript syntax in AppleScript. And then use DO SCRIPT to run the appropriate JavaScript. Rarely done but the Windows equivalent (VB/VBScript running JavaScript fragments) is often seen.

I agree that filename syntax is crucial and may be unexpected (being neither the HFS nor the UTF-8 name).

I hope the original poster will do as I asked and show the string passed to DO SCRIPT. I'm not going to try to guess what the AppleScript does, I used to use AppleScript and I know that what you get is rarely what you expect.

OzSanta2Author
Known Participant
January 11, 2017

Thank you TRY67, at last something I can get my teeth into.

The Applescript version produces a file as below. (after all the /// 's indicating quote marks are removed)

On the Java console, it Errors in line 6, Syntax error 5 (coloured blue.)

The second line does NOT produce any output, don’t know why??? There's a 2 page document open.

Regards

Santa

var dEnd =  this.numPages - 1;

alert(dEnd);

var cFlName = this.documentFileName;

var outputPath =  "/Desktop/"  ;

var outputPathTwo = outputPath & "Temporary Acrobat TIFFS"/;

for (i = 0; i <  dEnd ; i++ )

{

var oNewDoc = this.extractPages(i,i);

oNewDoc.saveAs({cPath: outputPathTwo  + "Page "+ (i + 1) +  " of " + cFlName + ".pdf", cConvID: "com.adobe.acrobat.pdf"});

}

When I run the actual Applescript version, I get a different error…

Keep in mind "\"" represents a quotation mark in the built up text string.

error "Adobe Acrobat got an error: \"var dEnd =  this.numPages - 1;

alert(dEnd);

var cFlName = this.documentFileName;

var outputPath = \\\"/Desktop/\\\"  ;

var outputPathTwo = outputPath & \\\"Temporary Acrobat TIFFS\\\"/;

for (i = 0; i <  dEnd ; i++ );

{

var oNewDoc = this.extractPages(i,i);

oNewDoc.saveAs({cPath: outputPathTwo  + \\\"Page \\\"+ (i + 1) +  \\\" of \\\" + cFlName + \\\".pdf\\\", cConvID:\\\"com.adobe.acrobat.pdf\\\"});

}\" doesn’t understand the “do script” message." number -1708 from "var dEnd =  this.numPages - 1;

alert(dEnd);

var cFlName = this.documentFileName;

var outputPath = \"/Desktop/\"  ;

var outputPathTwo = outputPath & \"Temporary Acrobat TIFFS\"/;

for (i = 0; i <  dEnd ; i++ );

{

var oNewDoc = this.extractPages(i,i);

oNewDoc.saveAs({cPath: outputPathTwo  + \"Page \"+ (i + 1) +  \" of \" + cFlName + \".pdf\", cConvID:\"com.adobe.acrobat.pdf\"});

}”

tell application "Adobe Acrobat"

  activate

  tell document 1

  set Params to ""

  set Params to Params & "var dEnd =  this.numPages - 1;" & return

  set Params to Params & "alert(dEnd);" & return

  set Params to Params & "var cFlName = this.documentFileName;" & return

  set Params to Params & "var outputPath = " & "\"" & "/Desktop/" & "\"  ;" & return

  set Params to Params & "var outputPathTwo = outputPath & " & "\"" & "Temporary Acrobat TIFFS" & "\"" & "/;" & return

  set Params to Params & "for (i = 0; i <  dEnd ; i++ )" & return & "{" & return

  set Params to Params & "var oNewDoc = this.extractPages(i,i);" & return

  set Params to Params & "oNewDoc.saveAs({cPath: outputPathTwo  + " & "\"" & "Page " & "\"" & "+ (i + 1) + " & " \"" & " of " & "\"" & " + cFlName + " & "\"" & ".pdf" & "\"" & ", cConvID:" & "\"" & "com.adobe.acrobat.pdf" & "\"" & "});" & return & "}"  as text

  #set Params to Params & "var this.closeDoc(false);" & return as text

  end tell

  do script Params

end tell

Karl Heinz  Kremer
Community Expert
Community Expert
January 13, 2017

Darn it, I've been chasing shadows.

The setTimeout does NOT work in Acrobat.

I HAVE to save as TIFFS. It's the only format that will trim Acrobat pages to any crop boxes with coding. JPEG is for emergencies.

Does anyone know of a way I can trap that darn error? It's occurring in the actual JavaScript, and I can't use Applescript to deal with it.

Using Java cuts the save time dramatically.

Regards

Santa

Here's my Applescript code.

property PreserveFileName : ""

property theItem : ""

tell application "Finder"

  set pp to (POSIX path of (path to desktop) as text)

  set thefile to file ((path to desktop as text) & "Problem PDF.pdf") as alias as text

end tell

set my theItem to thefile

try

  tell application "Finder" to set my PreserveFileName to name of file (my theItem) as text

on error errmsg number errnum

  my sayTheText:("Acrobat error setting file name")

end try

tell application "Finder"

  set pp to (POSIX path of (path to desktop) as text)

  set p to 101.2

  set tempEXT to name extension of file (my theItem)

  set p to 101.3

  set displayName to (characters 1 through -((count of tempEXT) + 2) of my PreserveFileName) as text

end tell

tell application "Adobe Acrobat"

  activate

  open thefile as alias

  tell document 1

  say 1

  set theScript to my setDrawPages(pp, displayName)

  try

  say 4

  do script theScript

  say 5

  on error errmsg

  tell application "System Events" to display dialog errmsg

  end try

  end tell

end tell

on setDrawPages(pp, displayName)

  say 2

  set Params to ""

  set Params to Params & "var dEnd =  this.numPages;" & return as text

  set Params to Params & "var cFlName = " & "\"" & displayName & "\"" & ";" & return as text

  set Params to Params & "var outputPath  = " & "\"" & pp & "\";" & return as text

  set Params to Params & "var outputPathTwo = outputPath  " & " + \"" & "Temporary Acrobat TIFFS" & "\"" & " + " & "\"" & "/" & "\";" & return as text

  set Params to Params & "for (i = 0; i <  dEnd ; i++ ){;" & return as text

  set Params to Params & "var oNewDoc = this.extractPages(i,i);" & return as text

  set Params to Params & "var tempText = outputPathTwo " & " + " & "\"" & "Page " & "\"" & " + (i + 1) + " & "\"" & " of " & "\"" & " + cFlName;" & return as text

  set Params to Params & "try {" & return as text

  set Params to Params & "oNewDoc.saveAs({cPath: outputPathTwo  + " & "\"" & "Page " & "\"" & " + (i + 1) + " & "\"" & " of " & "\"" & " + cFlName + " & "\"" & ".tiff" & "\"" & ", cConvID:" & "\"" & "com.adobe.acrobat.tiff" & "\"" & "});" & return as text

  set Params to Params & "}" & return

  set Params to Params & "catch(err) {" & return

  set Params to Params & "app.alert(err);" & return as text

  set Params to Params & "oNewDoc.saveAs({cPath: outputPathTwo  + " & "\"" & "Page " & "\"" & " + (i + 1) + " & "\"" & " of " & "\"" & " + cFlName + " & "\"" & ".jpeg" & "\"" & ", cConvID:" & "\"" & "com.adobe.acrobat.jpeg" & "\"" & "});" & return

  set Params to Params & "}" & return

  set Params to Params & "};" & return as text

  tell application "System Events" to display dialog Params as text

  say 3

  return Params as text

end setDrawPages

Here's the screen capture of the actual code.

Here's the two consecutive errors, which have to be dismissed manually. Everything MUST be automatic. Cropping and reducing resolution don't work. I need to now save as a JPEG, after the error.


Even though you found a way to get around this, here is how you would get rid of the ".pdf" in the filename. I've already introduced you to the String.replace() method when we talked about how to get the path to the user's Desktop folder. You would use the same approach here, but replace with an empty string. In this case however, the simple replace may not work because you may have something like filename.pdf.pdf and when you run "filename.pdf.aaa.pdf".replace(".pdf", "") (yes, you can do this in JavaScript), you will end up with "filename.aaa.pdf" - it only replaced one - the first instance - of the ".pdf"s in the name. You very likely want to replace the actual extension, which would be the last instance of ".pdf". This is where you would use regular expressions to identify the actual file extension:

var cFlName = this.documentFileName;

cFlName = cFlName.replace(/.pdf$/i, "");

This will find the ".pdf" which is directly followed by the end of the string, regardless of the case (e.g. .pdf and .PDF are both found, as would be .PdF) and replaces it with the empty string .

As far as your second problem goes, there is nothing you can do in JavaScript to get around this problem: Acrobat's automation interface is not meant for "lights out" operation, it assumes that somebody is sitting in front of the screen and reads error messages and clicks on the "OK" button when such an error occurs. The condition you are encountering is handled by the user interface, and there is no way to - from within JavaScript - do anything about that. You will not be able to simulate a click on the button, or internally tell Acrobat to continue with the script. You are actually not allowed to use Acrobat on a server, or on a server like environment (see the EULA for more information), so that's Adobe's way of making this server use more complicated.

What I would try is to find out what the limitations of the export to TIFF functions are, and therefore what the maximum page size is that you can process this way. You would then have to analyze your PDF document to see if the page you are about to export is larger than that maximum allowed size, and if that's the case, export in a different format (before I would use JPEG, I would try PNG first - JPEG is only good for photographic images).

If this is for a server or server-like environment, you will have to look at other tools.

Legend
January 11, 2017

You could do worse than the Acrobat JavaScript forum. My advice: don't show the AppleScript. it won't be understood. It will be a distraction from what you really need to know. Tell us the specific value of Params when you run Do Script Params. Never mind that it's wrong, it's a starting point. Tell us (copy and paste don't paraphrase!) the console error.

OzSanta2Author
Known Participant
January 11, 2017

OK, I give in.

Here's the best I can do showing the Java Script without any Applescript added. I don't know if the script is exactly right, cause I know stuff all about Java Script formatting, but the error I get is...error "The variable i is not defined." number -2753 from "i” and it occurs in the saveAs line

var cFlName = this.documentFileName;

var dEnd =  this.numPages-1;

alert(dEnd );

for (i = 0; i <= dEnd-1; i+ );

{

var oNewDoc = this.extractPages(i,i);

oNewDoc.saveAs : “~/Desktop" & "/" & "\"" & "Temporary Acrobat TIFFS" & "\"" & "/" & "\"Page " & "\"" & (i + 1) & " \"" & "\"" & cFlName & "\"" & " using: " & "\"" & ".pdf" & "\"; "

}

try67
Community Expert
Community Expert
January 11, 2017

There are a lot of errors in your code. You need to first of all study the code JS syntax and then the Acrobat-specific methods and properties that you want to use.

You can't use AS syntax in your JS code. That's just not going to work.

You also need to find out the correct file-path syntax for your files. The best way to do that is to open a PDF from that folder in Acrobat and then execute this command in the JS Console:

this.path;

It will print out that file's full path, in the syntax that you must use in your code.

Let's say that path is "/Desktop/". In that case, the code above should be changed to something like this:

var outputPath = "/Desktop/";

var originalDoc = this;

var cFlName = originalDoc.documentFileName;

var dEnd = originalDoc.numPages;

for (i=0; i<dEnd; i++) {

    var oNewDoc = originalDoc.extractPages(i,i);

    oNewDoc.saveAs({cPath: outputPath + "Page " + (i+1) + " of " + cFlName + ".tiff", cConvID: "com.adobe.acrobat.tiff"});

    oNewDoc.closeDoc(true);

}

Edit: Made some minor fixes to the code

Karl Heinz  Kremer
Community Expert
Community Expert
January 8, 2017

This is not Java, which also would not be supported by Acrobat, it's also not JavaScript, which is a supported language within Acrobat, it's actually AppleScript.

Can you please tell us what the script is not doing correctly. What are you expecting, and what is the script doing?

OzSanta2Author
Known Participant
January 8, 2017

G'day from there land of Oz, Karl.

What I am trying to do is save each individual page of a PDF, as a separate document, as a TIFF, and also it must be 'trimmed' of excess graphics, to the crop box. I then re-open the cropped TIFF in Acrobat,( which it opens as a PDF), enlarge the top of the document, and add 4 text boxes, 2 with barcodes, then print and save the amended page.

Ate the moment, I'm using the Graphics User Interface in the 'save As' dialog box to save the PDF as TIFFS, and it trims each page to it's crop box. But it's slow, and I need to speed up the process.

The handler above is the accepted method of creating a 'Java Script' within Applescript, and calling the compiled script. I have a long way to go yet, but I need to get the 'save to:' path right. The correct terms are eluding me. I'm a complete Novice at Java.

I want to use the above to check if the 'Save' trims to the crop box, first.

Thanks for you interest.

Santa (aka Brian Christmas)

PS, Here a handler I use to add dialog boxes in Acrobat

# This handler uses Java Script

  #

  on Add_WaterMarkText_(temp)

  try

  local cName, cText, cFont, nFontSize, nS, nE, nTA, nHorizontalAlignment, nVerticalAlignment, nHorizontalWidth, nVerticalHeight, nR, currentPage

  set p to 1

  set {cName, cText, cFont, nFontSize, nS, nE, nTA, nHorizontalAlignment, nVerticalAlignment, nHorizontalWidth, nVerticalHeight, nR, currentPage} to temp as list

  set p to 2

  set Params to "" as text

  set p to 3

  set Params to Params & "var f = this.addField(\"" & cName & "\", " & "\"" & "text" & "\", " & currentPage - 1 & ", [" & nHorizontalAlignment & ", " & nVerticalAlignment & ", " & nHorizontalAlignment + nHorizontalWidth & ", " & nVerticalAlignment - nVerticalHeight & "]" & ");" & return as text

  set p to 4

  set Params to Params & "f.value = \"" & cText & "\";" & return as text

  set p to 5

  set Params to Params & "f.userName = \"" & cName & "\";" & return as text

  set p to 6

  set Params to Params & "f.textSize = " & nFontSize & ";" & return as text

  set p to 7

  set Params to Params & "f.textFont =  \"" & cFont & "\";" & return as text

  set p to 8

  set Params to Params & "f.alignment = \"" & "left" & "\";" & return as text

  set p to 9

  set Params to Params & "f.multiline = false;" & return as text

  set p to 10

  set Params to Params & "f.lineWidth = 0;" & return as text

  set p to 11

  set Params to Params & "f.display = display.visible;" & return as text

  set p to 12

  set Params to Params & "f.textColor = color.black;" & return as text

  # set javaScript to Params

  on error errmsg

  set temp to "Add_WaterMarkText error " & errmsg & " Text is " & cText as text

  my writeFile4("MMErrorsList", (temp & return & return as text))

  tell application "System Events" to display dialog temp giving up after 20

  # if RunForOz then tell application "System Events" to display dialog temp giving up after 36000

  end try

  return Params as text

  end Add_WaterMarkText_

Karl Heinz  Kremer
Community Expert
Community Expert
January 9, 2017

Let's take the AppleScript out of the equation. What is the JavaScript you are trying to run?