Powershell打开文本文件打印出口
问题描述:
我有一个文本文件,我需要在Notepad.exe中打开并让用户添加一些输入。然后我想在用户退出文件时将文件打印到AdobePDF。Powershell打开文本文件打印出口
这是我不得不打开该文件
Start-Process notepad.exe C:\path\to\text\file\MyTextFile.txt -NoNewWindow -Wait
不知道如何打印PDF退出。 AdobePDF是已安装的打印机,但它不是默认打印机。任何帮助或提示将不胜感激。
答
你可以尝试这样的事情,
$TxtFilePath = "C:\folder\txtfile.txt"
Start-Process notepad.exe $TxtFilePath -NoNewWindow -Wait #Assuming user save the file
Get-Content -Path $TxtFilePath| Out-Printer PDFCreator #PDFCreator is the printer name for PDF
- 编辑 -
我无法找出一个直接的方法来保留文件名,那种写了与MS黑客攻击总之,
Function Save-TxtAsPDF
{
Param([string] $FilePath, [string] $OutFolder)
# Required Word Variables
$ExportASPDFConstant = 17
$DoNotSaveConstant = 0
# Create a hidden Word window
$word = New-Object -ComObject word.application
$word.visible = $false
# Add a Word document
$doc = $word.documents.add()
# Put the text into the Word document
$txt = Get-Content $FilePath
$selection = $word.selection
$selection.typeText($txt)
# Set the page orientation to landscape
$doc.PageSetup.Orientation = 1
$FileName = (Split-Path -Path $FilePath -Leaf).Replace(".txt",".pdf")
$DestinationPath = Join-Path $OutFolder $FileName
# Export the PDF file and close without saving a Word document
$doc.ExportAsFixedFormat($DestinationPath,$ExportASPDFConstant)
$doc.close([ref]$DoNotSaveConstant)
$word.Quit()
}
$TxtFilePath = "C:\folder\tt.txt"
$OutFolder = "C:\Outputfolder"
Start-Process notepad.exe $TxtFilePath -NoNewWindow -Wait #Assuming user save the file
Save-TxtAsPDF -FilePath $TxtFilePath -OutFolder $OutFolder
看看是否有帮助!
这很好。但是,我想知道在保存pdf时是否有保留原始文本文件名的方法? – Eric
更新了答案,找不到直接的方法!看看是否有帮助! –