print automatically pdf file with powershell - pdf

I'm printing a pdf file with:
Start-Process -FilePath "C:\file.pdf" –Verb Print
This opens an empty adobe reader window, prints the file, but keeps the adobe reader open. So, how do I close adobe reader?
Thank you in advance.

From here seems like that is how AcroRd32 will work - it will keep the Adobe Reader open and you can only control whether it is minimized when it starts or not.
One alternative is to use Foxit Reader as described here- http://www.deltasblog.co.uk/code-snippets/printing-pdf-files-from-command-line-without-adobe-reader/
Or,Hack:
Start-Process -FilePath "test.pdf" –Verb Print
sleep 10
kill -name AcroRd32
Updated hack for multiple Adobe readers open:
Start-Process -FilePath "test.pdf" –Verb Print -PassThru | %{sleep 10;$_} | kill

Related

Setting password for Word doc from Powershell

I'd like to make a Microsoft Word document be set with a password for opening the document. When I try to run the following code in Powershell, the script will hang once I enter the desired password.
This script will be run in Kaseya to help automate protecting Word documents. I've tried both modifying the Document.Password property and using the Document.Protect method and they both will hang the script.
$path = Read-Host("Specify path to word document")
Write-Host "Creating Word application object..."
$wordAppObj = New-Object -ComObject Word.Application
Write-Host "Creating Word document object..."
$wordDocObj = $wordAppObj.Documents.Open($path)
# Write-Host "Activating Word document object..."
# $wordDocObj.Activate
Write-Host "Setting password..."
$securePass = Read-Host("Set password as") -AsSecureString
$password = ConvertFrom-SecureString $securePass
# $wordDocObj.Protect(3, $true, $password)
$wordDocObj.Password = $password
Write-Host "Saving Word document object..."
$wordDocObj.Save
Write-Host "Closing the Word document..."
$wordDocObj.Close
Write-Host "Closing Word..."
$wordAppObj.Application.Quit()
I expect the script to run through and protect the file, but the script will hang and an instance of Microsoft Word will be running in the background taking up about 6-9% of the CPU. Nothing will happen to the file or in the script. There are no error messages that pop up.
UPDATE: As suggested, I added $wordAppObj.Visible = $true to the script to see if there were any pop-ups that happened during the execution of the script. Unfortunately, I didn't see any. I believe the script may be hanging when it prompts the user to re-enter the password. This happens when I use Word to encrypt a document with a password. Is there any way to fill this field in from Powershell?
I have developed a program in C# with the Microsoft Interop reference for Word. This program worked for me. At this point, I believe that this kind of thing cannot be done from Powershell and I would advise that anyone trying to do this seek another way to get this done. I think Powershell simply doesn't support modifying the password field of a document.

Command line to kill Adobe Reader process only if it was not already running

I am trying some cmd.exe coding for the first time. I created a registry key, which contains a command called by a custom protocol handler­. What I'm trying to accomplish : Call Adobe Reader which will automatically print a pdf to a given printer. This part already works. I'm using powershell to download the pdf locally from a URL. I also want to kill the Acrobat process once i'm done, but only if it was already opened BEFORE calling my command, so I don't kill the process if the user already has a pdf opened when calling the protocol handler via Chrome.
Excuse the more than ugly formatting, a one-line command is a must in my case, I can't make a batch file or .ps1 file.
cmd /V /C set exists="0" & (tasklist | find "AcroRd32.exe" > nul) & if
(%%ERRORLEVEL%%==0 set exists="1") & set myvar=%1 & call set
myvar=%%myvar:ie:=%% & call set printer=%%myvar:*###=%% & call set
printer=^"%%printer%%^" & call set printer=%%printer:'=%% & call start
powershell.exe -ExecutionPolicy Bypass -Command $url=%%myvar%%; $pdf =
$url.split('#'); md -Force c:\TempPdf "| Out-Null"; (New-Object
System.Net.WebClient).DownloadFile($pdf,'C:/TempPdf/PatientLabel.pdf'); &
timeout /t 2 & call start acrord32.exe /n /t C:/TempPdf/PatientLabel.pdf
%%printer%% & timeout /t 2 & (if !exists!=="1" taskkill /f /im acrord32.exe)
The part which does not work is the tasklist/taskkill. I want to set the %%exists%% variable to 1 if I can find the process running before starting AcroRd32.exe and kill it after printing if %%exists%%=1. I think this might just not be working because of the execution time/parse, but I don't really know how to use /V and !var! anyway. The exists and ERRORLEVEL variables don't seem to expand.
Thanks for the help !

Can I use CreateObject to print a .tif document with vba? If not, what?

The following vba routine works well to print a word document
If sHlink <> "" Then
Set OfficeObject = CreateObject("Word.Application")
OfficeObject.Documents.Open sHlink
OfficeObject.PrintOut Background:=False
OfficeObject.Quit
Set OfficeObject = Nothing
End If
but what I need to print are .TIF documents. They open by default with the Microsoft Photo Viewer. Is there something similar that will call the MS Photo Viewer, or failing that, Acrobat? Perhaps with Acrobat could I use some kind of command line?
Thank You
I found this shell command that prints using the Photo Editor that was formerly installed with Office:
Shell """C:\Program Files\Common Files\Microsoft Shared\PhotoEd\photoed.exe"" -p h:\misc\MyPicture.tif"
I think it can be adapted to print using the current Picture Manager - I bet it uses the same -p switch. This seems to be named "OIS.EXE" for some reason.

Access Word 'Save As' dialog box with PowerShell script

I've got a PowerShell script (running on Windows Server 2008 R2 Enterprise) that opens a Word doc in Word 2010, performs a SaveAs, and saves the doc as a PDF. In brief my code looks similar to the below:
$word = new-object -ComObject "word.application"
$word.Visible = $true
$doc = $word.documents.open("path\file.doc")
$doc.SaveAs("path\file.pdf", [ref] 17)
$doc.Close()
ps winword | kill
The above works fine, no problems at all and is converting the documents as expected.
My question is:
If I physically open Word myself and navigate to 'File > Save As' I get various options in the dialog when saving as PDF (eg. page range, optimisation etc)
How can I, if at all, access these options from within the PowerShell script when performing the same action?
Any advice would be appreciated. Maybe it's just not possible.
Thanks in advance
After much investigation I've found that option I needed was ExportAsFixedFormat().
The documentation can be found here:
http://msdn.microsoft.com/en-us/library/bb256835%28v=office.12%29.aspx
And you can see it in action within a PowerShell script here:
http://blog.coolorange.com/2012/04/20/export-word-to-pdf-using-powershell/

Adobe Reader Command Line Reference

Is there any official command line (switches) reference for the different versions of
Adobe (formerly Acrobat) Reader?
I didn't find anything on Adobe Developer Connection.
Especially I want to:
Start Reader and open a file
Open a file at a specific position (page)
Close Reader (or single file)
You can find something about this in the Adobe Developer FAQ. (It's a PDF document rather than a web page, which I guess is unsurprising in this particular case.)
The FAQ notes that the use of the command line switches is unsupported.
To open a file it's:
AcroRd32.exe <filename>
The following switches are available:
/n - Launch a new instance of Reader even if one is already open
/s - Don't show the splash screen
/o - Don't show the open file dialog
/h - Open as a minimized window
/p <filename> - Open and go straight to the print dialog
/t <filename> <printername> <drivername> <portname> - Print the file the specified printer.
I found this:
http://www.robvanderwoude.com/commandlineswitches.php#Acrobat
Open a PDF file with navigation pane active, zoom out to 50%, and search for and highlight the word "batch":
AcroRd32.exe /A "zoom=50&navpanes=1=OpenActions&search=batch" PdfFile
To open a PDF at page 100 the follow works
<path to Adobe Reader> /A "page=100" "<Path To PDF file>"
If you require more than one argument separate them with &
I use the following in a batch file to open the book I'm reading to the page I was up to.
C:\Program Files\Adobe\Reader 10.0\Reader\AcroRd32.exe /A "page=149&pagemode=none" "D:\books\MCTS(70-562) ASP.Net 3.5 Development.pdf"
The best list of command line args for Adobe Reader I have found is here.
http://partners.adobe.com/public/developer/en/acrobat/PDFOpenParameters.pdf
It's for version 7 but all the arguments I tried worked.
As for closing the file, I think you will need to use the SDK, or if you are opening the file from code you could close the file from code once you have finished with it.
Call this after the print job has returned:
oShell.AppActivate "Adobe Reader"
oShell.SendKeys "%FX"
Having /A without additional parameters other than the filename didn't work for me, but the following code worked fine with /n
string sfile = #".\help\delta-pqca-400-100-300-fc4-user-manual.pdf";
Process myProcess = new Process();
myProcess.StartInfo.FileName = "AcroRd32.exe";
myProcess.StartInfo.Arguments = " /n " + "\"" + sfile + "\"";
myProcess.Start();