1
$\begingroup$

I am asking this because I am drawing a plot in Mathematica and export it to a PDF format. I need to check the quality of the plot and may need to modify it and export it again. Sometimes I forget to close the PDF file and I have to go back to close it and run the Mathematica notebook again. Suppose I need to export a plot pic to PDF with the command

Export["C:\\Users\\abc\\figures\\pic.pdf", pic]

Is there a way I can put a command to close the file pic.pdf first (if it is opened), and open pic.pdf with another command after I Export it?

Basically, I am asking if Mathematica is capable of closing and opening a file by its path in another app, say a PDF reader.

$\endgroup$
9
  • $\begingroup$ Perhaps it's possible, but it would seem like a bad idea: what if you had made changes to the file you force-closed? I'm sure in most cases it won't be an issue, but that one time you forget and force-close something important, it will hurt! :-) $\endgroup$
    – MarcoB
    Jan 30, 2017 at 22:11
  • $\begingroup$ Since it is just an exported PDF file and I am not editing it directly in the PDF editor, I guess it is OK to force it closed. $\endgroup$
    – nanjun
    Jan 30, 2017 at 22:25
  • 2
    $\begingroup$ A process can't close a file it did not open by itself. You need access to the operating system. There are many topics on this you are read about. It is not trivial at all. You need number of tools to do this. And to do all this from inside Mathematica? Forget it :) $\endgroup$
    – Nasser
    Jan 31, 2017 at 2:19
  • 2
    $\begingroup$ Why don't you use a PDF viewer that supports auto-refresh once the file has changed? Under Windows you could use "sumatra pdf". $\endgroup$
    – grbl
    Feb 3, 2017 at 16:14
  • 2
    $\begingroup$ @nanjun Yes this should be possible. Adobe Acrobat is one of the very few PDF viewer which block the files from beeing written to. $\endgroup$
    – grbl
    Feb 6, 2017 at 12:35

1 Answer 1

2
$\begingroup$

As said in the comments, it's difficult to close and open application, but if you are using Adobe Acrobat, you can use:

RunProcess[{"taskkill", "/IM", "AcroRd32.exe"}]

this will send a WM_CLOSE message to Acrobat, and close all Acrobat windows properly.

Opening the PDF it's easy:

SystemOpen["C:\\Users\\abc\\figures\\pic.pdf"]

This will open the PDF with your PDF viewer by default.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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