c# 調用Microsoft XPS Document Writer打印機,將Pdf文件轉換成Xps文件



  最近碰到個項目,其中有個需要將pdf文件轉換成xps文件的功能,xps文件還算是新東西,所以基本沒啥了解,通過一段時間的調查,

本人算是找到了2個方法:

  1)通過PDFNet第三發開發組件即可很容易的完成轉換功能,並且還有其他針對pdf文件操作的功能,還是很強大的。當然啦,這個東

      西是要買的,可以先下一個試用版先體驗體驗。

    下載地址:http://www.pdftron.com/pdfnet/index.html

  2)通過“Microsoft XPS Document Writer”打印機,將pdf打印成本地的xps文件,以此來完成轉換功能。

      這個打印機的驅動在WIN7的系統上裝Office2007的時候會自動裝上,如果是XP系統的話,可能沒有,可以去微軟官網下載個

     “SaveAsPDFandXPS.exe”,裝上后,就會有這個打印機。

    打印機也有了,那么接下來的問題就是怎么調用這個打印機了,淡然了,可以通過一系列的API的配合去調用這個打印機,但我覺得

      Windows的打印機調用起來實在是太麻煩了,通過一番調查,可以直接使用Adobe acro Reader或Foxit Reader這兩個軟件的打

    印功能,將文件打出,下面的列出了代碼供參考。

   開發環境:VS2010,.Net FrameWork4.0,C#,WPF

 

窗體代碼
<Window x:Class="TestPdfToXps1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Click="Button_Click">PDF TO XPS</Button>
</Grid>
</Window>

 

窗體的后台關鍵代碼
 
          

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

 
          

using System.ComponentModel;
using System.Runtime.InteropServices;

 
          

namespace TestPdfToXps1
{

    /// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
//Win32 Api定義
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll")]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfeter, string lpszClass, string lpszWindow);

[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, String lParam);

[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);


//Win32消息定義
const uint WM_SETTEXT = 0x000c;
const uint WM_IME_KEYDOWN = 0x0290;
const uint WM_LBUTTONDOWN = 0x0201;
const uint WM_LBUTTONUP = 0x0202;


public MainWindow()
{
InitializeComponent();
}


private void Button_Click(object sender, RoutedEventArgs e)
{
PrintWithFoxitReader();
}

        void PrintWithFoxitReader()
{
string pdf_filepath = @"d:\原文件.pdf"; // 需要轉換的PDF文件
string xps_filepath = @"d:\目標文件.xps"; // 目標XPS文件

           /***** 調用Foxit Reader.exe的打印功能,並且制定打印機為Microsoft XPS Document Writer *****/
           System.Diagnostics.ProcessStartInfo psInfo = new System.Diagnostics.ProcessStartInfo();
           psInfo.FileName = @"D:\Tools\Reader\Foxit Reader\Foxit Reader.exe"; //Foxit Reader.exe的全路徑
           psInfo.Arguments = String.Format(@" -t {0} ""Microsoft XPS Document Writer""", pdf_filepath);
           psInfo.CreateNoWindow = true
           psInfo.UseShellExecute = false
           System.Diagnostics.Process ps = new System.Diagnostics.Process();
           ps.StartInfo = psInfo;
           ps.Start();


// 等待
System.Threading.Thread.Sleep(5 * 1000);


/***** 啟動Foxit Reader后,會彈出文件另存為對話框********************************/
/***** 因此使用Win32Api找到文件另存為對話框中的文件名輸入框,並且通過給輸入******/
/***** 框發消息在輸入框中自動填入目標xps文件名,最后通過給保存按鈕發消息來*******/
/***** 最后通過給保存按鈕發消息來按下對話框中的保存按鈕**************************/
// 找到文件另存為對話框的窗口句柄
IntPtr hWnd = FindWindow("#32770", "文件另存為");
IntPtr hChild;
// 由於輸入框被多個控件嵌套,因此需要一級一級的往控件內找到輸入框
hChild = FindWindowEx(hWnd, IntPtr.Zero, "DUIViewWndClassName", String.Empty);
hChild = FindWindowEx(hChild, IntPtr.Zero, "DirectUIHWND", String.Empty);
hChild = FindWindowEx(hChild, IntPtr.Zero, "FloatNotifySink", String.Empty);
hChild = FindWindowEx(hChild, IntPtr.Zero, "ComboBox", String.Empty);
hChild = FindWindowEx(hChild, IntPtr.Zero, "Edit", String.Empty); // File name edit control
// 向輸入框發送消息,填充目標xps文件名
SendMessage(hChild, WM_SETTEXT, IntPtr.Zero, xps_filepath);
// 等待1秒鍾
System.Threading.Thread.Sleep(1000);
// 找到對話框內的保存按鈕
hChild = IntPtr.Zero;
hChild = FindWindowEx(hWnd, IntPtr.Zero, "Button", "保存(&S)");
// 向保存按鈕發送2個消息,以模擬click消息,借此來按下保存按鈕
PostMessage(hChild, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
PostMessage(hChild, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);



/***** 跟蹤打印機隊列中的文件打印狀況,知道文件打印完成為止 *****/
// 調用本地打印機隊列
System.Printing.LocalPrintServer prtSrv = new System.Printing.LocalPrintServer();
System.Printing.PrintQueue queue = prtSrv.GetPrintQueue("Microsoft XPS Document Writer");


//一直監視打印隊列,知道打印隊列中沒有打印任務時結束
do
{
// 等待處理
System.Threading.Thread.Sleep(1000);
// 讀取隊列的最新信息
queue.Refresh();
} while (queue.NumberOfJobs > 0);


MessageBox.Show("完成");
}
}
}

 

*注:如果是使用Adobe Read進行打印,可以參考下列的部分代碼

使用Adcro Readr的相關代碼

            //將前面的兩行代碼換成一下代碼
       psInfo.FileName = @"C:\Program Files\Adobe\Reader 8.0\Reader\AcroRd32.exe";
psInfo.Arguments = String.Format(@" /s /h /t {0} ""Microsoft XPS Document Writer""", pdf_filepath);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM