你可以嘗試這樣的事情,
$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
更新了答案,找不到直接的方法!看看是否有幫助! –