You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Spire.Doc打印Word文档?WPF应用PrintDialog类型转换报错求助

Fixing Spire.Doc PrintDialog Mismatch in WPF & Proper Printing Methods

Got it, let's break down your problem and fix it step by step. The error you're seeing happens because Spire.Doc's Document.PrintDialog returns a WinForms-specific System.Windows.Forms.PrintDialog, but your WPF project uses WPF's own System.Windows.Controls.PrintDialog. These are distinct classes from different UI frameworks, so implicit conversion between them isn't allowed.

Here are two reliable, clean ways to print Word documents with Spire.Doc in WPF:

1. Use Spire.Doc's Built-in Printing (Simplest Approach)

Spire.Doc has a native Print() method that handles the printing dialog and process automatically, no manual PrintDialog type handling needed. This works seamlessly in WPF:

using Spire.Doc;

// Load your Word document
Document doc = new Document();
doc.LoadFromFile(@"C:\YourDocument.docx");

// Option 1: Show system print dialog and print
doc.Print();

// Option 2: Print directly to a specific printer (no dialog)
// doc.Print("Your Printer Name");

This method is straightforward—Spire.Doc takes care of adapting to the WPF environment under the hood.

2. Use WPF's PrintDialog with XPS Conversion (For Customization)

If you need full control over WPF's PrintDialog (like setting paper size, print range, or custom settings), convert the Word document to an XPS stream first, then print it using WPF's native printing system:

Step 1: Add Required References

Make sure your WPF project references these assemblies:

  • System.Printing
  • ReachFramework (for XPS document handling)

Step 2: Code Implementation

using Spire.Doc;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Xps.Packaging;

// Load the Word document
Document doc = new Document();
doc.LoadFromFile(@"C:\YourDocument.docx");

// Convert Word to XPS stream
MemoryStream xpsStream = new MemoryStream();
doc.SaveToStream(xpsStream, FileFormat.XPS);
xpsStream.Position = 0; // Reset stream position to start

// Initialize WPF's PrintDialog
PrintDialog printDialog = new PrintDialog();

// Show dialog and print if user confirms
if (printDialog.ShowDialog() == true)
{
    using (XpsDocument xpsDoc = new XpsDocument(xpsStream, FileAccess.Read))
    {
        var fixedDocumentSequence = xpsDoc.GetFixedDocumentSequence();
        printDialog.PrintDocument(fixedDocumentSequence.DocumentPaginator, "Printing Word Document via WPF");
    }
}

This approach lets you leverage all the customization options of WPF's PrintDialog while still using Spire.Doc to process the Word document.

While you could technically add WinForms references to your WPF project and use System.Windows.Forms.PrintDialog, this introduces unnecessary interop complexity and isn't ideal for WPF applications. The methods above are far cleaner.

内容的提问来源于stack exchange,提问作者omid naghipoor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:21:38