I have a requirement to have a small script on a Windows Server using PowerShell to convert files to PDF (from RTF). I've got a prototype using Word that works on my desktop machine, but the servers involved don't have Office/Word installed. Is there a similar way I can call Wordpad to achieve the same thing?
$documents_path = 'c:\projects\pbl\hi.rtf'
$word_app = New-Object -ComObject Word.Application
$document = $word_app.Documents.Open($documents_path)
$pdf_filename = 'c:\projects\pbl\hi.pdf'
$document.SaveAs([ref] $pdf_filename, [ref] 17)
$document.Close()
$word_app.Quit()
You would not be able to accomplish this goal using only WordPad because it has no automation interface like Microsoft Word has. You would need to investigate an alternative solution to achieve this.
This is old thread so no point in addressing OP question as such
However for the benefit of others landing here, this is a "Yes" Answer, I have not run it on a server, but understand there may be issues calling MS Print to PDF -- headless, for several reasons, including it uses the Graphics Device Interface. It works perfectly on a workstation.
It is done as a one line command along these lines, Input could be %1 with %2 for Output.
There are 2 WordPad.exe's to try :-)
This is the default American English one:-
%comspec% "C:\Windows\System32\wordpad.exe" /pt "%Input%" "Microsoft Print to PDF" "Microsoft Print to PDF" "%Output%"
OR my Locale British English one, which I target !
"%ProgramFiles%\Windows NT\Accessories\WORDPAD.EXE" /pt %1 "Microsoft Print to PDF" "Microsoft Print to PDF" %2
For a drag and drop wrapper see:-
Doc2PDF.cmd - Convert .Doc(x), .ODT, .RTF or .Txt to PDF & open them in SumatraPDF
P.S
A better replacement to WordPad is Jarte Pro and if you have a Portable WinWord.exe you could look at https://stackoverflow.com/a/2749172/10802527
Related
Question about running a custom function in Powershell.
I'm on Windows 10 and I'd like to somehow print my monorepository's directory tree structure excluding node_modules. This is not supported out of the box but requires a custom function to be defined. I found one solution on StackOverflow (https://stackoverflow.com/a/43810460/9654273), which would enable using a command like:
tree -Exclude node_modules -Ascii > tree.txt
The problem is I don't know what to do with the provided source code :D The answer says "add to your $PROFILE, for instance", so I ran notepad $PROFILE in PowerShell, pasted the code snippet there, saved it and tried running the command. It didn't work because I did something wrong. According to the StackOverflow post's comments from anand_v.singh and mklement0 I was still running some other tree command, not the one I just attempted to define.
So how do I use a custom function in PowerShell? Starting point is that source code is on StackOverflow and I don't know where to paste it. Or do you know some other, easier way to print a directory tree on Windows 10 excluding node_modules?
I had the same problem with that function. The issue is the special characters in the hashtable at line 106:
$chars = #{
interior = ('├', '+')[$ndx]
last = ('└', '\')[$ndx] #'
hline = ('─', '-')[$ndx]
vline = ('│', '|')[$ndx]
space = ' '
}
I changed the special characters to ascii as follows:
$chars = #{
interior = ('+', '+')[$ndx]
last = ('\', '\')[$ndx] #'
hline = ('-', '-')[$ndx]
vline = ('|', '|')[$ndx]
space = ' '
}
The only downside is that you do not now have the option of using special graphics characters (the Ascii switch is still there, but does nothing). Maybe someone could tell us how to embed them properly.
I'm trying to make a system which turns the contents of a .txt file into a variable. This isn't my problem, though; for some reason, my files are reading characters I didn't enter, and can't use.
Please note: What I'm doing is in no way efficient, and I'm positive there are other ways to go about this, but this is the best way for me. Also, I'm not amazingly intelligent when it comes to coding. Just thought I'd throw that out there.
First, let me show you the system I have in place.
value.txt
4
This file has the contents which I'd like to make into a variable.
Batch Files
setcmdvar.bat
set cmdvar=
I leave this empty so that I can put the contents of value.txt at the end (more on this later).
start.bat
#echo off
call PowerShell.exe cd "C:\Users\%username%\Desktop\Folder"; $PSvar = Get-Content value.txt; $PSvar >> setcmdvar.bat
pause
call setcmdvar.bat
pause
echo The variable equals %cmdvar%.
pause
exit
The second line from start.bat creates this script in PowerShell:
PowerShell script
cd "C:\Users\%username%\Desktop\Folder\"
$PSvar = Get-Content value.txt; $PSvar >> setcmdvar.bat
This creates a variable in PowerShell, $PSvar, which equals the contents of value.txt; in our case, 4. Then, it puts $PSvar (4) at the end of setcmdvar.bat, using >>, which changes it to:
setcmdvar.bat (changed)
set cmdvar=4
Or, at least, it should, to my knowledge. Instead, it changes the file to this:
set Items=桔瑳楲杮椠業獳湩桴整浲湩瑡牯›⸢ †⬠䌠瑡来牯䥹普††††㨠倠牡敳䕲牲牯›㨨
嵛慐敲瑮潃瑮楡獮牅潲割捥牯䕤捸灥楴湯 †⬠䘠汵祬畑污晩敩䕤牲牯摉㨠吠牥業慮潴䕲灸捥整䅤䕴摮晏瑓楲杮
Or some other strange combination of characters. I looked one up, and it was Chinese. There's also some other characters like †, ⬠, ›, ⸢,
, and . I have no idea why these are being typed. Along with this, start.bat displays the following:
Press any key to continue . . .
(PowerShell script runs here)
'■s' is not recognized as an internal or external command,
operable program or batch file.
Press any key to continue . . .
The variable equals .
Press any key to continue . . .
(exit)
I did not type "■s," and I assume this may be the problem. Whatever it is, does anyone have any ideas?
P.S. I'm sorry if my code is complicated, or if it looks bad, but I have it this way for a reason. Mostly my incompetence, actually. But I think it's better that way.
Also, I know there are commands like for /f "delims=" %a in ('ver') do #set foobar=%a (I just took this off the internet) but I've tried commands like those, and I suppose I just don't understand them all that well, because they didn't work.
I appreciate the help!
It would probably be better to avoid the static setcmdvar.bat script and write it all in the script. Using -Encoding ascii is what keeps the output from being Unicode and having a BOM (Byte Order Mark) at the beginning. It has nothing to do with Chinese characters.
ECHO>"%USERPROFILE%\value.txt" 4
SET "CMDFILE=%USERPROFILE%\setcmdvar.bat"
powershell -NoLogo -NoProfile -Command ^
"'SET ""cmdvar=' + (Get-Content $Env:USERPROFILE\value.txt) + '""""' |" ^
"Out-File -FilePath %CMDFILE% -Encoding ascii"
pause
call "%CMDFILE%"
pause
echo The variable equals %cmdvar%
pause
EXIT /B 0
>> has its problems with mixing encodings. Plus it defaults to utf16. I recommend changing
$PSvar >> setcmdvar.bat
to
add-content setcmdvar.bat $PSvar
I'm not sure how to keep everything on one line. You can make setcmdvar.bat like this:
set cmdvar=^
So that it continues.
Looking here:
Is it possible to change an .rtf file to .txt file using some sort of batch script on Windows?
I have saw which possible use POWERSHELL for to do it. Was present a full example for to do it but link don't work.
Who can tell me as i can to solve it? Thanks.
You can use .NET to do this in powershell very easily by implementing the System.Windows.Forms.RichTextBox control, loading the richtextfile into it, then pulling the text version out. This is by far the easiest and quickest way I have found to do this.
My function for doing exactly this is here: https://github.com/Asnivor/PowerShell-Misc-Functions/blob/master/translate-rtf-to-txt.ps1
To explain this a little more basically:
$rtfFile = [System.Io.FileInfo]"path/to/some/rtf/file"
$txtFile = "path/to/the/destination/txt/file"
# Load *.rtf file into a hidden .NET RichTextBox
$rtBox = New-Object System.Windows.Forms.RichTextBox
$rtfText = [System.IO.File]::ReadAllText($rtfFile);
$rtBox.Rtf = $rtfText
# Get plain text version
$plainText = $rtBox.Text;
# Write the plain text out to the destination file
[System.IO.File]::WriteAllText($txtFile, $plainText)
i'm trying to write a Powershell-script that edits Text Form Fields in Microsoft Office Word 2007. It should find a Form Field via the Bookmark I configured before and write a Text into it. The default text I wrote into it for test purposes is "Something".
That's what I have so far:
$document = 'D:\Powershell\Test.docx'
$Word = New-Object -Com Word.Application
$Word.Visible = $True
$doc = $word.Documents.Open($document)
$text = "Hello"
$bookmark = "server1"
$doc.Bookmarks.Item($bookmark).Range.Text.Replace("Something", $text)
While it works in the console since the output is:
FORMTEXT Hello
Word still displays the String I inserted manually before.
When I type in:
$doc.Bookmarks.Item($bookmark).Range.Text
The output is:
FORMTEXT Something
I already tried:
$Word.ActiveDocument.Reload()
$Word.ActiveDocument.Fields.Update()
$doc.PrintPreview()
$doc.ClosePrintPreview()
$doc.Bookmarks.Item($bookmark).Range.Fields.Update()
But nothing seems to work.
Has somebody an idea how to write something in that Text Form Field permanently?
Alternatively if that's easier I could use a (rich) Text Content Control (which seem to be newer). Those don't use a bookmark but a tag and title.
Thanks for you help in advance .
PS:It doesn't work with MS Word 2016 either.
When you have a legacy text form field, the bookmark is really there to identify the field. If you try to write replace the text of the bookmark in VBA (say) you'll probably get Error 6028 - "The range cannot be deleted".
I don't know Powershell well enough to do this without checking, but the equivalent VBA would be
doc.FormFields($bookmark).Result = "Something"
so I would guess the powershell is something like
$doc.FormFields.Item($bookmark).Result = "Something"
Is it possible to get Powershell to read the stdout of an exe into a byte[] instead of the usual text processed array of lines?
Best I've been able to do is this:
cmd /c foo.exe > foo.tmp
$b = [io.file]::readallbytes('foo.tmp')
del foo.tmp
Yucky, not to mention it is not streamable. Any better way to do this?
Got some info from the PowerShell team. The short answer is that unfortunately, it is not easy. :-(
The medium length answer is: http://poshcode.org/2175.
The long answer is: Capture and Redirect Binary Process Output