簡體   English   中英

使用c#打印word文檔

[英]Print word document using c#

我有這個代碼來打開一個word文件

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文件,不知道嗎?

快速提示(與您的主題無關,但實際上與C#相關):不需要像上面那樣寫出可選參數 ,您可以使用ParameterName: parameter為可選參數指定參數。

快速回答:使用Document.PrintOut()方法打印當前文檔。 有關參數的更多詳細信息,您可以查看MSDN站點此站點以獲取實用教程。

這是一個簡單的演示:

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

使用RawPrintHelper。 請點擊以下鏈接:

http://support.microsoft.com/kb/322091

以下是將文件發送到打印機進行打印的代碼:

//Send file for Printing
RawPrinterHelper.SendFileToPrinter(PrinterName, FileName);

//Send string to print
RawPrinterHelper.SendStringToPrinter(PrinterName, sData);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM