我正在尝试从PowerShell使用WPF打印。我在VB.Net here中找到了一个简单的示例
'Create a new Run class, passing it the text.
' The Run class contains 1 line of text
Dim r As New Run(text)
'Create a new paragraph, passing it the new run class instance
Dim ph As New Paragraph(r)
'Create the document, passing a new paragraph
Dim doc As New FlowDocument(ph)
doc.PagePadding = New Thickness(100) 'Creates margin around the page
'Send the document to the printer
diag.PrintDocument( _
CType(doc, IDocumentPaginatorSource).DocumentPaginator, _
printCaption)但我无法将其转换为PowerShell。这是我的尝试:
$run = New-Object System.Windows.Documents.Run('text')
$paragraph = New-Object System.Windows.Documents.Paragraph($run)
$flowDocument = New-Object System.Windows.Documents.FlowDocument($paragraph)
$thickness = New-Object System.Windows.Thickness(100)
$flowDocument.PagePadding = $thickness
$flowDocument -is [System.Windows.Documents.IDocumentPaginatorSource]
$source = $flowDocument -as [System.Windows.Documents.IDocumentPaginatorSource]
$source.DocumentPaginator -eq $null
$printDialog = New-Object System.Windows.Controls.PrintDialog
$printDialog.PrintDocument($source.DocumentPaginator, "test")$source.DocumentPaginator似乎为空,并引发异常。( VB.Net代码可以工作)
编辑
下面是另一个失败的尝试:
Add-Type -Assembly 'ReachFramework'
$flowDocument = New-Object System.Windows.Documents.FlowDocument
$textRange = New-Object System.Windows.Documents.TextRange(
$flowDocument.ContentStart, $flowDocument.ContentEnd)
$textRange.Text = 'Text'
$xpsDocument = New-Object System.Windows.Xps.Packaging.XpsDocument(
"C:\scripts\test.xps", [System.IO.FileAccess]::ReadWrite)
$xpsDocumentWriter =
[System.Windows.Xps.Packaging.XpsDocument]::CreateXpsDocumentWriter(
$xpsDocument)
$source = $flowDocument -as [System.Windows.Documents.IDocumentPaginatorSource]
$xpsDocumentWriter.Write($source.DocumentPaginator)
$xpsDocument.Close()我试图使用其中一个XpsDocumentWriter.Write()重载将FlowDocument发送到打印机,但失败了,$source.DocumentPaginator为空。我甚至没有设法创建并保存一个.xps文件。
发布于 2010-11-16 08:29:58
有时候,当我对PowerShell的语言“问题”感到沮丧时,我会求助于C#,V2让它变得非常容易。下面是为我打印的内容:
$src = @'
public static System.Windows.Documents.DocumentPaginator
GetDocumentPaginator(System.Windows.Documents.FlowDocument flowDocument)
{
return ((System.Windows.Documents.IDocumentPaginatorSource)flowDocument).DocumentPaginator;
}
'@
Add-Type -MemberDefinition $src -Name Utils -Namespace Foo `
-ReferencedAssemblies WindowsBase,PresentationFramework,
PresentationCore
$run = New-Object System.Windows.Documents.Run('text')
$paragraph = New-Object System.Windows.Documents.Paragraph($run)
$flowDocument = New-Object System.Windows.Documents.FlowDocument($paragraph)
$thickness = New-Object System.Windows.Thickness(100)
$flowDocument.PagePadding = $thickness
$paginator = [Foo.Utils]::GetDocumentPaginator($flowDocument)
$printDialog = New-Object System.Windows.Controls.PrintDialog
$printDialog.PrintDocument($paginator, "test") 请注意,您可以使用VB代码执行相同的操作。只需在Add-Type上使用-Language VisualBasic参数。
https://stackoverflow.com/questions/4174628
复制相似问题