首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PowerShell WPF打印-无法获取IDocumentPaginatorSource接口

PowerShell WPF打印-无法获取IDocumentPaginatorSource接口
EN

Stack Overflow用户
提问于 2010-11-14 04:50:18
回答 1查看 1.5K关注 0票数 0

我正在尝试从PowerShell使用WPF打印。我在VB.Net here中找到了一个简单的示例

代码语言:javascript
复制
'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。这是我的尝试:

代码语言:javascript
复制
$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代码可以工作)

编辑

下面是另一个失败的尝试:

代码语言:javascript
复制
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文件。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-11-16 08:29:58

有时候,当我对PowerShell的语言“问题”感到沮丧时,我会求助于C#,V2让它变得非常容易。下面是为我打印的内容:

代码语言:javascript
复制
$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参数。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4174628

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档