使用C#打印Word文档 [英] Print word document using c#

查看:397
本文介绍了使用C#打印Word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码可以打开文字文件

I have this code to open a word file

int num = 0;
object fileName = FD.FileName;
object readOnly = false;
object isVisible = false;
object missing = System.Reflection.Missing.Value;

Word.Application WordApp = new Word.Application();
Word.Document aDoc = null;
WordApp.Visible = false;

aDoc = WordApp.Documents.Open(ref fileName,
                              ref missing, 
                              ref readOnly,
                              ref missing,
                              ref missing, 
                              ref missing, 
                              ref missing,
                              ref missing, 
                              ref missing,
                              ref missing,
                              ref missing, 
                              ref isVisible,
                              ref missing,
                              ref missing, 
                              ref missing, 
                              ref missing);

Word.WdStatistic stat = Word.WdStatistic.wdStatisticPages;
num = aDoc.ComputeStatistics(stat, ref missing);

label3.Text = "Page Count :"+aDoc.ComputeStatistics(stat, ref missing).ToString();
GC.Collect();

现在,我想在按钮的单击事件上打印打开的Word文件,知道吗?

Now, I want to print the opened word file on the click event of a button, Any idea?

推荐答案

快速提示(与您的主题无关,但实际上是C#):无需像上面一样写出可选参数,您可以使用ParameterName: parameter将参数指定为可选参数.

Quick tip (not relevant to your topic but actually C#): there's no need to write out optional parameters as you did above, you can use ParameterName: parameter to specify a parameter to a optional parameter.

快速解答:使用Document.PrintOut()方法打印当前文档.有关参数的更多详细信息,可以查看 MSDN网站该网站以获取动手教程.

Quick answer: use Document.PrintOut() method to print the current document. For more details about the parameters, you can take a look at MSDN site and this site for a hand-on tutorial.

这是一个简单的演示:

public class YourClass : Form
{
    private Word.Application word = new Word.Application {Visible = false};
    private Word.Document doc;
    // where did you get this file name?
    private string fileName;

    private void Count()
    {
        // as you mentioned, you open your word document here
        doc = word.Documents.Open(fileName, ReadOnly : readOnly, Visible: isVisible);
    }

    // in your button click handler, just call PrintOut() function
    private void ButtonClickHandler(object sender, EventArgs e)
    {
        // if doc == null, open the document
        if (doc == null)
        {
            // here i assume fileName has been assigned
            doc = word.Documents.Open(fileName, ReadOnly : readOnly, Visible: isVisible);
        }

        doc.PrintOut();
    }
}

这篇关于使用C#打印Word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆