【问题标题】:How to print word document using PrintDocument class如何使用 PrintDocument 类打印 word 文档
【发布时间】:2017-07-17 14:14:14
【问题描述】:

我的要求是使用 c# 打印一个 Word 文档。

由于不建议在服务器端使用 Interop Word,我想使用 PrintDocument 类打印 Word 文件。

那么,如何用c#打印Word文档呢?

我尝试了下面的代码,但它打印出了 2 个空白页:

 PrintDocument printDoc = new PrintDocument();
            printDoc.DocumentName = "E:\\WordPrint\\Output\\TEST.docx";
            printDoc.DefaultPageSettings.PrinterSettings.PrinterName = "Bullzip PDF Printer";
            printDoc.DefaultPageSettings.PrinterSettings.Copies = 2;
            printDoc.Print();

【问题讨论】:

  • 能否留下您的代码,看看您的问题出在哪里,或者您尝试打印什么文档?
  • 你没有。打印 Word 文档需要使用 Word。
  • @JesseR.Jose PrintDocument printDoc = new PrintDocument(); printDoc.DocumentName = "E:\\WordPrint\\Output\\TEST.docx"; printDoc.DefaultPageSettings.PrinterSettings.PrinterName = "Bullzip PDF 打印机"; printDoc.DefaultPageSettings.PrinterSettings.Copies = 2; printDoc.Print();
  • @LarsTech,很少有像 Spire Doc .NET 这样的 SDK 可用,它允许在不使用 word 的情况下打印 word doc。这怎么可能?
  • @sivaprakash 编辑您的问题并将您的代码留在那里。

标签: c# printing ms-word printdocument


【解决方案1】:
try 
{ 
    streamToPrint = new StreamReader ("C:\\My Documents\\MyFile.txt"); 

    try 
    { 
        printFont = new Font("Arial", 10); 
        PrintDocument pd = new PrintDocument(); 
        pd.PrintPage += new PrintPageEventHandler (this.pd_PrintPage); 
        pd.Print(); 
    } 
    finally 
    {
        streamToPrint.Close(); 
    } 
} 
catch (Exception ex) 
{ 
    MessageBox.Show(ex.Message); 
}

您可以在 msdn 网站上找到更多信息: https://msdn.microsoft.com/de-de/library/system.drawing.printing.printdocument(v=vs.110).aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    • 2014-04-11
    • 2013-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多