I have a program which opens, then prints any given document. I did this using the Desktop class like this:
Which works perfectly. I want to be able to close it once it prints though, for which there isn't a method in the Desktop class. I've read a couple things online tat suggested I get the process id and close it like that, but I'm trying to figure out a pretty straightforward way to do that. Can anybody give me a hand with this?
I'm not sure if you know that you can call Desktop#print without opening a document first. If you do need to actually display it on the desktop first, then you will need to call Desktop#open as you have in your example.
Closing it is not simple because you don't know the PID for the launched application. If you were able to guess which application associated with the file got launched (such as notepad.exe for .txt files), running something like taskkill /IM notepad.exe (or the equivlent for other operating systems) using Runtime#exec, you would end-up killing all processes running that same application, not just the intended one.
posted 3 years ago
Yeah and that's part of the problem. If I already had say a .docx document open, and used my app to open another one, then just killed WINWORD.exe, they would both go. I think you solution of just printing without opening is the way I'm going to go. But just for knowledge/curiosity, is there a different way anybody knows of to open a file so that you can get the specific PID for that process?
posted 3 years ago
So I ended up going with Ron's suggestion and just going right to print without opening. It doesn't really need to be opened anyway. I'm having another weird issue though. If I open a PDF (Adobe Reader), a Notepad or Word document it just flashes the app for a second then closes it, and prints. Perfect. When I try and print an image file (.jpeg, .png etc...) it opens Windows Photo Viewer (my default app for those extensions), but takes me to the print screen and I have to manually click PRINT. Any idea why the behaviors would be different between the apps?