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);

 

posted @ 2012-02-04 17:05  白色的海  阅读(13169)  评论(2编辑  收藏  举报