从Asp.net打印Word文档c#没有安装MS Office

时间:2015-09-24 07:51:38

标签: c# asp.net ms-word

我需要在不安装MS Office的情况下打印Word文档。 我正在使用WordprocessingDocument来操作Word文件。

现在我需要打印它。

我试过了:

System.Diagnostics.Process printProcess = new System.Diagnostics.Process();
printProcess.StartInfo.FileName = "D:/testWordPad1.docx";
printProcess.StartInfo.Verb = "Print";
printProcess.StartInfo.CreateNoWindow = true;
printProcess.Start();
printProcess.WaitForExit();

它适用于安装了MS Office的本地系统。 但它在我没有安装MS Office的服务器上不起作用。

它告诉我:

  

没有应用程序与此操作的指定文件关联 - 在System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)

请告诉我你的建议。

3 个答案:

答案 0 :(得分:0)

为了打印文本文件[这是您尝试做的事情],您可以使用记事本的命令行参数[了解更多here]

以下内容会将文档发送到默认打印机:

my $stmt = "INSERT INTO ATSD_MOB_CASH_POSITION ( ROUTING_SEQ, ORIGIN, POSITION_ACCOUNT_ID, PUBLISH_DATE,
    TRANSACTION_ID, SETTLEMENT_DATE, SOURCE_ID, SETTLEMENT_CURRENCY, SOD_SNAPSHOT, RECORD_STATUS,
    CASH_BALANCE_TYPE, LATEST, CURRENT_VALUE, CHANGED_VALUE, REFERENCE, BANK_CODE,
    TRANSACTION_REFERENCE, LAST_TRANSACTION_DATE_TIME, CASH_BALANCE_SUB_TYPE, POSITION_ID,
    SECURITIES_ACCOUNT_ID, CASH_LOCATION, CASH_PURPOSE, UPDATE_SEQUENCE ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )";

如果要定义要将文件发送到的打印机,请使用以下参数:

/ PT [filename] [printername] [driverdll] [port]

答案 1 :(得分:0)

错误意味着动词不可用。 首先,您可以检查可用的动词:

    foreach (String verb in printProcess.StartInfo.Verbs)
    {
        System.Diagnostics.Debug.WriteLine(verb);
    }

您确定服务器上有可用的打印设备吗? 如果您在服务器上右键单击该文件,它是否会显示" Print"在上下文菜单中?

答案 2 :(得分:0)

要在未安装MS Office的Web服务器上打印.docx文件,第三方库可能是最佳选择。我尝试了a free component(nuget包),对于小项目有100个段落和5个表限制,它可以工作。

using System;
using System.Collections.Generic;
using System.Text;
using Spire.Doc;
using System.Windows.Forms;
using System.Drawing.Printing;


namespace Doc_Print
{
    class Program
    {
        static void Main(string[] args)
        {

            Document doc = new Document(); 
            doc.LoadFromFile("sample.doc");
            PrintDialog dialog = new PrintDialog();
            dialog.AllowPrintToFile = true; 
            dialog.AllowCurrentPage = true;
            dialog.AllowSomePages = true;
            dialog.UseEXDialog = true; 
            doc.PrintDialog = dialog;               
            PrintDocument printDoc = doc.PrintDocument; 
            printDoc.Print();
            if (dialog.ShowDialog() == DialogResult.OK)
            {               
                printDoc.Print();
            }            

        }
    }
}