c# 调用Microsoft XPS Document Writer打印机,将Pdf文件转换成Xps文件
来源:互联网 发布:淘宝客域名注册技巧 编辑:程序博客网 时间:2024/04/25 20:54
最近碰到个项目,其中有个需要将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("完成"); } }}
使用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);
0 0
- c# 调用Microsoft XPS Document Writer打印机,将Pdf文件转换成Xps文件
- 无法找到打印机microsoft xps document writer
- Ubuntu下将XPS文件转换成PDF文件
- powerpoint2007 无法找到打印机“Microsoft XPS Document Writer"
- Power Point 打开显示 "无法找到打印机 Microsoft XPS Document Writer"
- Printing documents to Microsoft XPS Document Writer without user interaction
- 解决WIN7下添加Microsoft XPS Document Writer
- 在Windows 7上使用Microsoft XPS Document Writer无法打印出XPS文档的问题
- xps文件的查看及转换
- XPS文件管理
- XPS文件怎么打开
- XPS文件转换PDF小工具,集成了界面,方便大家使用
- android解析PDF、XPS文件的第三方类库muPdf
- VSTO导出文件为pdf、xps等格式
- image 图片生产XPS文件
- image 图片生产XPS文件
- xps
- 如何将HDL文件实例化到XPS中
- android switch模块
- C#学习之多线程开发技术(八)
- svn 图标所表示的意思
- 用SparseArray代替HashMap
- 一个简单的jsp+servlet实例,实现简单的登录
- c# 调用Microsoft XPS Document Writer打印机,将Pdf文件转换成Xps文件
- Linux Shell替换:转义字符,命令替换,变量替换
- 图解JSP与Servlet的关系
- LM算法入门
- Java的一些经验讲述
- IOS 键盘的显示与关闭,以及移动显示(UITextView处理完整版)
- [转]Linux中的内存管理
- /*捕食者与被捕食者求大神解题C++.*/
- 项目4——数值转换