【问题标题】:How to send powershell script output to printer with formatting?如何通过格式化将powershell脚本输出发送到打印机?
【发布时间】:2021-02-12 09:06:47
【问题描述】:

我需要将在脚本中生成的脚本输出(基本上是按类别过滤的 Outlook 日历中的议程)发送到打印机。为了便于阅读,我希望它被格式化。至少打印一些粗体,一些斜体,也许改变字体。 文本由自定义对象组合而成,可能如下所示:

text = 
@"
Monday

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

Tuesday

No meetings
"@

理想情况下,我希望它打印如下内容:


星期一

与乔会面 : 07:00 - 08:00

会见安 : 8:30 - 09:00

星期二

没有会议


到目前为止我所拥有的(留下代码拉日历事件):

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

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

【问题讨论】:

    标签: powershell printing system.drawing


    【解决方案1】:

    嗯,因为你有前景,我假设你也有 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 后的外观示例

    【讨论】:

    • 谢谢,这是我绝对会尝试的方法。我更喜欢构建 html 文件。我想打印html文件基本上是一样的,对吧?
    • 还有一个问题 - 一切都会改变吗?我想打印的不是普通的办公室打印机,而是用于收据的热敏打印机?
    • 我认为这与构建和打印 HTML 文件不同。至于热敏打印问题,我不确定,但我希望至少格式保持不变。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多