Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3391570
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:49:04+00:00 2026-05-18T03:49:04+00:00

I am trying to use WPF printing from PowerShell. I have found a simple

  • 0

I am trying to use WPF printing from PowerShell. I have found a simple example in 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)

but I am unable to convert it to PowerShell. Here is my attempt:

$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 seems to be null, and an exception is raised. (The VB.Net code works)

[Edit]

Here is another attempt that failed:

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()

I was trying to use one of the XpsDocumentWriter.Write() overloads to send the FlowDocument to a printer, but it failed, $source.DocumentPaginator is null. I didn’t even manage to create a .xps file and save it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T03:49:05+00:00Added an answer on May 18, 2026 at 3:49 am

    Sometimes when I get frustrated with PowerShell’s language “issues”, I drop back to C# which V2 makes very easy. The following prints for me:

    $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") 
    

    Note that you could do the same with VB code. Just use the -Language VisualBasic parameter on Add-Type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use the FolderBrowserDialog from my WPF application - nothing fancy. I
I am trying to adapt a simple WPF application to use the Model-View-ViewModel pattern.
I'm trying to use the MediaUriElement from WPF-MediaKit, it works fine for every format
I have recently started learning wpf and am trying to use mvvm. My understanding
I'm trying to ditch Windows Forms, and learn to use WPF professionally from now
I'm currently trying to use a WPF component that makes use of Application.Current from
I'm trying to use a custom control in a WPF app, and I have
I have a WPF application in which I'm trying to use Entity Framework 4.1
I am trying to use MVVM in my WPF application which displays data from
I am trying to use charts from the WPF Toolkit (with LineSeries) and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.