简体   繁体   中英

How to print a word document using spire.doc?

I'm creating a WPF application and I want print a word document using spire.doc. I read some tutorials and they say that I should use this code.

//Create Word document.
Document document = new Document();
document.LoadFromFile(@"..\..\..\..\..\..\Data\Template.docx");
//Print doc file.
System.Windows.Forms.PrintDialog dialog = new System.Windows.Forms.PrintDialog();
dialog.AllowCurrentPage = true;
dialog.AllowSomePages = true;
dialog.UseEXDialog = true;
try
{
document.PrintDialog = dialog;
dialog.Document = document.PrintDocument;
dialog.Document.Print();
}

But it doesn't work because document.PrintDialog type is System.Windows.Controls.PrintDialog and I get this error:

cannot implicitly convert type 'System.Windows.Forms.PrintDialog' to 'System.Windows.Controls.PrintDialog'

Can you not use Document.PrintOut() documentation can be found here.

You can use Spire.Doc.Wpf.dll to print Word document in WPF applications. Refer to the following code:

private void button1_Click(object sender, RoutedEventArgs e)
    {
        Document doc = new Document();
        doc.LoadFromFile(@"Test.docx");
        PrintDialog dialog = new PrintDialog();

        if (dialog.ShowDialog() == true)
        {
            dialog.PrintDocument(doc.PrintDocument.DocumentPaginator, "test");
        }
    }

If my answer helps, please mark it as answer, thanks :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM