2017-01-09 4 views
0

私は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 

--EDIT--

答えて

0

あなたはこのような何かを試すことができますが、種類の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 

これが役立つかどうかを確認してください。

+0

これはうまく機能しました。しかし、私はPDFファイルを保存するときに元のテキストファイル名を維持する方法があるのだろうか? – Eric

+0

答えを更新しました。直接アプローチは見つかりませんでした。それが役立つかどうかを見てください! –

関連する問題