Code
1using oWord = Word;
2using System.Diagnostics;
3
4
5/**//// <summary>
6 /// WORD转PDF
7 /// </summary>
8 /// <param name="TagerFilePath">需要转换的WORD文件(包括路径和文件名如:C:\TEST.DOC)</param>
9 private void WordConvert(string TagerFilePath)
10 {
11
12 FilePath = TagerFilePath.Substring(0, TagerFilePath.Length - 4);
13 oWord.ApplicationClass word = new Word.ApplicationClass();
14 Type wordType = word.GetType();
15
16 //打开WORD文档
17 /**//*对应脚本中的
18 var word = new ActiveXObject("Word.Application");
19 var doc = word.Documents.Open(docfile);
20 */
21 oWord.Documents docs = word.Documents;
22 Type docsType = docs.GetType();
23 object objDocName = FilePath;
24 oWord.Document doc = (oWord.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { objDocName, true, true });
25
26 //打印输出到指定文件
27 //你可以使用 doc.PrintOut();方法,次方法调用中的参数设置较繁琐,建议使用 Type.InvokeMember 来调用时可以不用将PrintOut的参数设置全,只设置4个主要参数
28 Type docType = doc.GetType();
29 object printFileName = FilePath + ".ps";
30 docType.InvokeMember("PrintOut", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { false, false, oWord.WdPrintOutRange.wdPrintAllDocument, printFileName });
31 //new object[]{false,false,oWord.WdPrintOutRange.wdPrintAllDocument,printFileName}
32 //对应脚本中的word.PrintOut(false, false, 0, psfile);的参数
33
34 //退出WORD
35 //对应脚本中的word.Quit();
36 doc = null;
37 wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
38
39
40 object o1 = FilePath + ".ps";
41 object o2 = FilePath + ".pdf";
42 object o3 = "";
43
44 //引用将PS转换成PDF的对象
45 //try catch之间对应的是脚本中的 PDF.FileToPDF(psfile,pdffile,""); //你可以使用 pdfConvert.FileToPDF("c:\\test.ps","c:\\test.pdf","");这样的转换方法,本人只是为了保持与WORD相同的调用方式
46 try
47 {
48 ACRODISTXLib.PdfDistillerClass pdf = new ACRODISTXLib.PdfDistillerClass();
49 pdf.bShowWindow = 0;
50 pdf.bSpoolJobs = 0;
51 //pdf.FileToPDF(FilePath + ".ps", FilePath + ".pdf", @"Standard");
52 pdf.FileToPDF(FilePath + ".ps", FilePath + ".pdf", @"Standard");
53 //Type pdfType = pdfConvert.GetType();
54 //pdfType.InvokeMember("FileToPDF", System.Reflection.BindingFlags.InvokeMethod, null, pdf, new object[] { o1, o2, o3 });
55
56
57 FileStream fs = new FileStream(FilePath + ".pdf", FileMode.Open, FileAccess.Read);
58
59
60 BinaryReader br = new BinaryReader(fs);
61 byte[] data = br.ReadBytes((int)fs.Length);
62 br.Close();
63 fs.Close();
64
65 HttpContext.Current.Request.ContentType = "application/pdf";
66 HttpContext.Current.Response.BinaryWrite(data);
67 HttpContext.Current.Response.Flush();
68 // HttpContext.Current.Response.Close();
69 pdf = null;
70 }
71 catch { } //读者自己补写错误处理
72 finally
73 {
74 doc = null;
75 word = null;
76
77 }
78
79 //为防止本方法调用多次时发生错误,必须停止acrodist.exe进程
80 foreach (Process proc in System.Diagnostics.Process.GetProcesses())
81 {
82 int begpos;
83 int endpos;
84
85 string sProcName = proc.ToString();
86 begpos = sProcName.IndexOf("(") + 1;
87 endpos = sProcName.IndexOf(")");
88
89 sProcName = sProcName.Substring(begpos, endpos - begpos);
90
91 if (sProcName.ToLower().CompareTo("acrodist") == 0)
92 {
93 try
94 {
95 proc.Kill(); //停止进程
96 }
97 catch { } //读者自己补写错误处理
98 break;
99 }
100 }
101
102
103
104
1using oWord = Word;
2using System.Diagnostics;
3
4
5/**//// <summary>
6 /// WORD转PDF
7 /// </summary>
8 /// <param name="TagerFilePath">需要转换的WORD文件(包括路径和文件名如:C:\TEST.DOC)</param>
9 private void WordConvert(string TagerFilePath)
10 {
11
12 FilePath = TagerFilePath.Substring(0, TagerFilePath.Length - 4);
13 oWord.ApplicationClass word = new Word.ApplicationClass();
14 Type wordType = word.GetType();
15
16 //打开WORD文档
17 /**//*对应脚本中的
18 var word = new ActiveXObject("Word.Application");
19 var doc = word.Documents.Open(docfile);
20 */
21 oWord.Documents docs = word.Documents;
22 Type docsType = docs.GetType();
23 object objDocName = FilePath;
24 oWord.Document doc = (oWord.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { objDocName, true, true });
25
26 //打印输出到指定文件
27 //你可以使用 doc.PrintOut();方法,次方法调用中的参数设置较繁琐,建议使用 Type.InvokeMember 来调用时可以不用将PrintOut的参数设置全,只设置4个主要参数
28 Type docType = doc.GetType();
29 object printFileName = FilePath + ".ps";
30 docType.InvokeMember("PrintOut", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { false, false, oWord.WdPrintOutRange.wdPrintAllDocument, printFileName });
31 //new object[]{false,false,oWord.WdPrintOutRange.wdPrintAllDocument,printFileName}
32 //对应脚本中的word.PrintOut(false, false, 0, psfile);的参数
33
34 //退出WORD
35 //对应脚本中的word.Quit();
36 doc = null;
37 wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
38
39
40 object o1 = FilePath + ".ps";
41 object o2 = FilePath + ".pdf";
42 object o3 = "";
43
44 //引用将PS转换成PDF的对象
45 //try catch之间对应的是脚本中的 PDF.FileToPDF(psfile,pdffile,""); //你可以使用 pdfConvert.FileToPDF("c:\\test.ps","c:\\test.pdf","");这样的转换方法,本人只是为了保持与WORD相同的调用方式
46 try
47 {
48 ACRODISTXLib.PdfDistillerClass pdf = new ACRODISTXLib.PdfDistillerClass();
49 pdf.bShowWindow = 0;
50 pdf.bSpoolJobs = 0;
51 //pdf.FileToPDF(FilePath + ".ps", FilePath + ".pdf", @"Standard");
52 pdf.FileToPDF(FilePath + ".ps", FilePath + ".pdf", @"Standard");
53 //Type pdfType = pdfConvert.GetType();
54 //pdfType.InvokeMember("FileToPDF", System.Reflection.BindingFlags.InvokeMethod, null, pdf, new object[] { o1, o2, o3 });
55
56
57 FileStream fs = new FileStream(FilePath + ".pdf", FileMode.Open, FileAccess.Read);
58
59
60 BinaryReader br = new BinaryReader(fs);
61 byte[] data = br.ReadBytes((int)fs.Length);
62 br.Close();
63 fs.Close();
64
65 HttpContext.Current.Request.ContentType = "application/pdf";
66 HttpContext.Current.Response.BinaryWrite(data);
67 HttpContext.Current.Response.Flush();
68 // HttpContext.Current.Response.Close();
69 pdf = null;
70 }
71 catch { } //读者自己补写错误处理
72 finally
73 {
74 doc = null;
75 word = null;
76
77 }
78
79 //为防止本方法调用多次时发生错误,必须停止acrodist.exe进程
80 foreach (Process proc in System.Diagnostics.Process.GetProcesses())
81 {
82 int begpos;
83 int endpos;
84
85 string sProcName = proc.ToString();
86 begpos = sProcName.IndexOf("(") + 1;
87 endpos = sProcName.IndexOf(")");
88
89 sProcName = sProcName.Substring(begpos, endpos - begpos);
90
91 if (sProcName.ToLower().CompareTo("acrodist") == 0)
92 {
93 try
94 {
95 proc.Kill(); //停止进程
96 }
97 catch { } //读者自己补写错误处理
98 break;
99 }
100 }
101
102
103
104
注意:要把PDF虚拟打印机的“不要发送字体到 Adobe PDF”去掉打钓!
今天发现一问题,那就是,我用的是vs2005的时候,不用IIS能转过来,但用了IIS后发现不能转。不真怎么样回事,有谁知道还要设置些什么东西的么?2009-6-26