如何将 powershell 脚本输出发送到带格式的打印机?

How to send powershell script output to printer with formatting?

我需要将在脚本中制作的脚本输出(基本上,我的 Outlook 日历中按类别过滤的议程)发送到打印机。出于可读性的目的,我希望对其进行格式化。至少打印一些粗体字,一些斜体字,也许改变字体。 文本由自定义对象组合而成,可能看起来像这样:

text = 
@"
Monday

Meeting with Joe : 07:00 - 08:00
Meeting with Ann : 8:30 - 09:00

Tuesday

No meetings
"@

我希望它打印出这样的东西:


Monday

Meeting with Joe : 07:00 - 08:00

Meeting with Ann : 8:30 - 09:00

Tuesday

No meetings


到目前为止我有什么(留下提取日历事件的代码):

text = 
@"
Monday

Meeting with Joe : 07:00 - 08:00
Meeting with Ann : 8:30 - 09:00

Tuesday

No meetings
"@

Add-Type -AssemblyName System.Drawing
$PrintDocument = New-Object System.Drawing.Printing.PrintDocument
$PrintDocument.PrinterSettings.PrinterName = 'Microsoft Print to PDF'
$PrintDocument.DocumentName = "PipeHow Print Job"
$PrintDocument.DefaultPageSettings.PaperSize = $PrintDocument.PrinterSettings.PaperSizes | Where-Object { $_.PaperName -eq 'Letter' }
$PrintDocument.DefaultPageSettings.Landscape = $true

$PrintPageHandler =
{
    param([object]$sender, [System.Drawing.Printing.PrintPageEventArgs]$ev)

    $linesPerPage = 0
    $yPos = 0
    $count = 0
    $leftMargin = $ev.MarginBounds.Left
    $topMargin = $ev.MarginBounds.Top
    $line = $null

    $printFont = New-Object System.Drawing.Font "Arial", 10

    # Calculate the number of lines per page.
    $linesPerPage = $ev.MarginBounds.Height / $printFont.GetHeight($ev.Graphics)

    # Print each line of the file.
    while ($count -lt $linesPerPage -and (($line = ($text -split "`r`n")[$count]) -ne $null))
    {
        $yPos = $topMargin + ($count * $printFont.GetHeight($ev.Graphics))
        $ev.Graphics.DrawString($line, $printFont, [System.Drawing.Brushes]::Black, $leftMargin, $yPos, (New-Object System.Drawing.StringFormat))
        $count++
    }

    # If more lines exist, print another page.
    if ($line -ne $null) 
    {
        $ev.HasMorePages = $true
    }
    else
    {
        $ev.HasMorePages = $false
    }
}

$PrintDocument.add_PrintPage($PrintPageHandler)
$PrintDocument.Print()

这是我从网上相应的文章中摘取的,将文件中的StremReader替换为逐行读取多行字符串。 我将如何格式化这样的文本?在文本中放置一些标记,就像我在此处或 HTML 中所做的那样?然后在 $PrintPageHandler 中解析它们?我会为此使用 $printFont 还是 DrawString 中的 StringFormat

请给我一些继续挖掘的方向...

嗯,因为你有 outlook,我假设你也有 Word。使用 Word COM 对象是我能想到的唯一真正简单的方法来完成您想要做的事情。

#didn't know how you generated the calender info, so I took liberties here,  using nested hashtables
$calendarStuff = @{

    week1 = @{
        Monday =  @(
            "Meeting with Joe : 07:00 - 08:00",
            "Meeting with Ann : 8:30 - 09:00"
        );
        Tuesday = @("No meetings")
    }

}

#path where to save the doc
$docPath = "C:\temp\printTestThing.docx"
# instantiation of the com-object. It's by default not visible
$wordCom = New-Object -ComObject Word.Application
# uncomment this to see what's going on
#$wordCom.visible = $true

# creates and selects the word document we'll be working with
$doc = $wordCom.Documents.Add()
$selection = $wordcom.Selection

# go through and grab each week
foreach($week in $calendarStuff.Keys)
{
    # go through each week and grab the days
    foreach($day in $calendarStuff[$week].keys)
    {
        # Apply the style 'Heading 1' to what we're about to type
        $selection.Style = "Heading 1"
        # type out what the day is
        $selection.TypeText($day)
        # make a paragraph
        $selection.TypeParagraph()
        # switch the style back to normal
        $selection.Style = "Normal"
        foreach($thing in $calendarStuff[$week][$day])
        {
            # type out what the event is, prepending it with a 'tab' character
            $selection.TypeText("`t" + $thing)
            # make a paragraph
            $selection.TypeParagraph()
        }
    }

}
# print the doc
Start-Process -FilePath $docPath -Verb print

如果需要,您可以添加更多自定义方式,但希望这是您需要的催化剂。

打印为 pdf 时的样子示例