问题描述
我需要将脚本内生成的脚本输出(基本上是按类别过滤的Outlook日历中的议程)发送给打印机。为了便于阅读,我希望将其格式化。至少将一些单词加粗,斜体,或者更改字体。 文本是从一个自定义对象组装而成的,可能看起来像这样:
text =
@"
Monday
Meeting with Joe : 07:00 - 08:00
Meeting with Ann : 8:30 - 09:00
Tuesday
No meetings
"@
理想情况下,我希望它打印出这样的内容:
星期一
与乔开会:07:00-08:00
与Ann开会: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()
我从Internet上相应的文章中摘录了,通过逐行读取多行字符串来替换文件中的StremReader。
我该如何格式化文本?像我在此处或在HTML中一样,在文本中添加一些标记?然后在$PrintPageHandler
中解析它们?我会为此使用$printFont
还是在StringFormat
中使用DrawString
?
请给我一些指导以继续挖掘...
解决方法
嗯,因为您有远见,所以我假设您也有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
您可以根据需要添加更多自定义方式,但是希望这是您需要的催化剂。