2017-07-17 672 views
0

我的需求是使用c#打印Word文檔。如何使用PrintDocument類打印Word文檔

由於不推薦在服務器端使用Interop Word,因此我想使用PrintDocument類打印Word文件。

那麼,如何使用c#打印Word文檔?

我嘗試下面的代碼,但它打印出兩張空白頁:

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(); 
+0

你能離開你的代碼,看看您的問題在哪裏?或者您嘗試打印文檔的內容是什麼? –

+0

你不知道。打印Word文檔需要使用Word。 – LarsTech

+0

@ JesseR.Jose 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(); – sivaprakash

回答

0
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

+0

我也一樣,它打印出一張空白頁。 – sivaprakash