简体   繁体   English

如何在C#中打印word文档

[英]How to print word document in C#

I'm using the following code. 我正在使用以下代码。 When I print from notepad it get printed. 当我从记事本打印时,它会打印出来。 But when I print from MS Word it get printed without words containing symbols. 但是当我从MS Word打印时,它打印出来的时候没有包含符号的单词。 I think I have to enter doc format in code. 我想我必须在代码中输入doc格式。 How can I do this? 我怎样才能做到这一点?

String content="";
   private void btnUpload_Click(object sender, EventArgs e)
    {
        string fileName;
        // Show the dialog and get result.
        OpenFileDialog ofd = new OpenFileDialog();
        DialogResult result = openFileDialog1.ShowDialog();
        if (result == DialogResult.OK) // Test result.
        {
            fileName = ofd.FileName;

            var application = new Microsoft.Office.Interop.Word.Application();
            //var document = application.Documents.Open(@"D:\ICT.docx");
             //read all text into content
            content=System.IO.File.ReadAllText(fileName);
            //var document = application.Documents.Open(@fileName);
        }
    }
 private void btnPrint_Click(object sender, EventArgs e)
    {
        PrintDialog printDlg = new PrintDialog();
        PrintDocument printDoc = new PrintDocument();
        printDoc.DocumentName = "fileName";
        printDlg.Document = printDoc;
        printDlg.AllowSelection = true;
        printDlg.AllowSomePages = true;
        //Call ShowDialog
        if (printDlg.ShowDialog() == DialogResult.OK)
        {
             printDoc.PrintPage += new PrintPageEventHandler(pd_PrintPage);            
             printDoc.Print(); 
        }
    }
 private void pd_PrintPage(object sender, PrintPageEventArgs ev)
 {
   ev.Graphics.DrawString(content,printFont , Brushes.Black,
                   ev.MarginBounds.Left, 0, new StringFormat());
 }

As far as I know there are no basic functions which support reading the word format and / or printing it with the default Print Functionality in .net . 据我所知,没有基本功能支持读取字格式和/或使用.net中的默认打印功能进行打印。

IF you just want to print the document without any further information you can start a basic windows print process by using the Start method of the Process Class with the PrintTo Verb 如果您只是想在没有任何进一步信息的情况下打印文档,可以使用PrintTo Verb的Process类的Start方法启动基本的Windows打印过程

s. 秒。 MSDN Forum Print Word Document in c# Example form the linkes post: MSDN论坛在c#中打印Word文档示例链接帖子:

using (PrintDialog pd = new PrintDialog())
{
pd.ShowDialog();
ProcessStartInfo info = new ProcessStartInfo(@"D:\documents\filetoprint.doc");
info.Verb = "PrintTo";
info.Arguments = pd.PrinterSettings.PrinterName;
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(info);
}

If you need to do more (layout, other data ...) you could write your own doc / docx parser or use something like the aspose tools 如果您需要做更多(布局,其他数据......),您可以编写自己的doc / docx解析器或使用aspose工具之类的东西

s. 秒。 http://www.aspose.com/.net/word-component.aspx http://www.aspose.com/.net/word-component.aspx

perhaps infragistics / devexpress may also components to read word documents, convert them to HTML or furthermore supporting direct printing of the word. 或许infragistics / devexpress也可以用于读取word文档,将它们转换为HTML或者进一步支持单词的直接打印。

For all tools trial versions should be aviable 对于所有工具,试用版应该是可用的

http://www.infragistics.com http://www.infragistics.com

https://www.devexpress.com/ https://www.devexpress.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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