[C#][WinForm]利用程式使用DDE(Dynamic Data Exchange)來打開PDF or ExcelViewer自動列印PDF or Excel檔案

摘要:[C#][WinForm]利用程式使用DDE(Dynamic Data Exchange)來打開PDF or ExcelViewer自動列印PDF or Excel檔案

首先什麼是DDE呢.我自已也不是很了解

在.NET要使用DDE要先去抓一下Library→.NET DDE library on CodePlex

這個範例主要功能是:

1.選一個PDF or Excel檔會自動開啓對應的執行檔(Adobe Reader or Microsoft Office Excel Viewer)

2.執行列印動作

3.自動關閉(Adobe Reader or Microsoft Office Excel Viewer)

C#(WinForm)

 

using System;
using System.Diagnostics;
using System.Windows.Forms;
using NDde;//記得先加入參考NDde.dll
using NDde.Client;

namespace WinFormCSharp
{
    public partial class FrmDDE : Form
    {
        public FrmDDE()
        {
            InitializeComponent();
        }

        //PDF
        private void bntPDF_Click(object sender, EventArgs e)
        {
            DdeClient client = new DdeClient("Acroview", "Control");

            bool tryStart = false;
            bool connected = false;
            do
            {
                try
                {
                    client.Connect();
                    connected = true;
                }
                catch (DdeException)
                {
                    System.Diagnostics.Process p = new System.Diagnostics.Process();
                    p.StartInfo.FileName = "AcroRd32.exe";
                    p.Start();
                    p.WaitForInputIdle();
                    tryStart = !tryStart;
                }
            } while (tryStart && !connected);

            if (connected)
            {
                try
                {
                    client.Execute("[DocOpen(\"C:\\Users\\Puma\\Desktop\\a.pdf\")]", 60000);
                    client.Execute("[FilePrintSilent(\"C:\\Users\\Puma\\Desktop\\a.pdf\")]", 60000);
                    client.Execute("[DocClose(\"C:\\Users\\Puma\\Desktop\\a.pdf\")]", 60000);
                    client.Execute("[AppExit()]", 60000);
                }
                catch (Exception ex)
                {
                }
            }
        }

        //Excel
        private void btnExcel_Click(object sender, EventArgs e)
        {
            DdeClient client = new DdeClient("Excel", "system");

            bool tryStart = false;
            bool connected = false;
            do
            {
                try
                {
                    client.Connect();
                    connected = true;
                }
                catch (DdeException)
                {
                    ProcessStartInfo info = new ProcessStartInfo(@"C:\Program Files\Microsoft Office\Office12\XLVIEW.exe");
                    info.WindowStyle = ProcessWindowStyle.Minimized;
                    info.UseShellExecute = true;
                    info.Arguments = "C:\\Users\\Puma\\Desktop\\a.xlsx";
                    System.Diagnostics.Process p = Process.Start(info);
                    p.WaitForInputIdle();
                    tryStart = !tryStart;
                }
            } while (tryStart && !connected);

            if (connected)
            {
                try
                {
                    client.Execute("[Open(\"C:\\Users\\Puma\\Desktop\\a.xlsx\")]", 60000);
                    client.Execute("[Print()]", 60000);
                    client.Execute("[Close()]", 60000);
                    client.Execute("[Quit()]", 60000);
                }
                catch (Exception ex)
                {
                }
            }
        }
    }
}

執行結果:

參考網址:
http://vidmar.net/weblog/archive/2008/04/14/printing-pdf-documents-in-c.aspx