C # How to finish printing Word documents _c# Tutorial

Source: Internet
Author: User

In daily work, we may often need to print a variety of file materials, such as Word documents. For programmers, the printing of an application's Chinese document is a very important feature and has always been a very complex task. Especially when it comes to Web printing, it's really tricky. In general, if you want to select a Non-default printer or want to display the Print Setup dialog box, we also need to set the code to a certain degree.

In response to this question, today's article I would like to share how to easily print Word documents with free Third-party components. The free component simplifies the code and improves our productivity. So, in the example below I used one of the free Spire.doc components to do this.

The steps for C # to complete printing of Word documents are as follows:

This is the original Word document screenshot:

First step: After the component is installed, create a C # console project, add references and namespaces as follows:

Using System;
Using Spire.doc;
Using System.Windows.Forms;

Step two: Instantiate a Word Document object and call the LoadFromFile method to load the Word document that you want to print:

Document doc = new document ();
Doc. LoadFromFile ("Sample.doc");

Step three: Instantiate a PrintDialog object and set the related properties. Associate Doc. PrintDialog Properties and PrintDialog objects:

PrintDialog dialog = new PrintDialog ();
Dialog. Allowprinttofile = true;
Dialog. Allowcurrentpage = true;
Dialog. Allowsomepages = true;
Dialog. Useexdialog = true; 
Doc. PrintDialog = Dialog;

Fourth step: Print in the background.

Print out all pages using the default printer. This piece of code can also be used for Web page spool printing:

PrintDocument Printdoc = doc. PrintDocument;
Printdoc.print ();

Fifth: If you want to display the Print dialog box, call the ShowDialog method, and print the Word document according to the Print preview setup options:

if (dialog. ShowDialog () = = DialogResult.OK)
{ 
printdoc.print ();
}

This is a screenshot of the XPS format after you print the document:

All code:

Using System;
Using Spire.doc;
Using System.Windows.Forms;
Namespace Doc_print
{public
partial class Form1:form
{public
Form1 ()
{ InitializeComponent ();
}
private void Button1_Click (object sender, EventArgs e)
{
//instantiate a Word Document Object
Document DOC = new document (); c15/>//Load document
doc. LoadFromFile (@ "C:\Users\Administrator\Desktop\ sample document. doc");
Instantiate System.Windows.Forms.PrintDialog Object
PrintDialog Dialog = new PrintDialog ();
Dialog. Allowprinttofile = true;
Dialog. Allowcurrentpage = true;
Dialog. Allowsomepages = true;
Dialog. Useexdialog = true;
Associate Doc. PrintDialog property and PrintDialog object
doc. PrintDialog = Dialog;
Background print
//PrintDocument Printdoc = Doc. PrintDocument; 
Printdoc.print ();
Displays the Print dialog box and prints the
if (dialog). ShowDialog () = = DialogResult.OK)
{
//printdoc.print ()
;
}
}}}

The above is a small set for you to introduce C # to complete the Word document printing method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.