【发布时间】: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