C#如何打印word文档 [英] How to print word document in C#

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

问题描述

我正在使用以下代码.当我从记事本打印时,它会被打印出来.但是当我从 MS Word 打印时,它打印出来的单词没有包含符号.我想我必须在代码中输入 doc 格式.我该怎么做?

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. I think I have to enter doc format in code. 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());
 }

推荐答案

据我所知,没有支持读取 word 格式和/或使用 .net 中的默认打印功能打印它的基本功能.

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 .

如果您只想在没有任何进一步信息的情况下打印文档,您可以使用 启动方法 使用PrintTo动词

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

s.MSDN 论坛 c# 打印 Word 文档链接帖子的示例形式:

s. MSDN Forum Print Word Document in c# Example form the linkes post:

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

如果您需要做更多(布局、其他数据...),您可以编写自己的 doc/docx 解析器或使用诸如 aspose 工具之类的东西

If you need to do more (layout, other data ...) you could write your own doc / docx parser or use something like the aspose tools

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

也许 infragistics/devexpress 也可能是读取 word 文档、将它们转换为 HTML 或进一步支持直接打印 word 的组件.

perhaps infragistics / devexpress may also components to read word documents, convert them to HTML or furthermore supporting direct printing of the word.

对于所有工具,试用版都应该可用

For all tools trial versions should be aviable

http://www.infragistics.com

https://www.devexpress.com/

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

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