BOSS们求助HELP.HELP
下面是一段word转换pdf的代码using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using word = Microsoft.Office;
using System.Diagnostics;
//using System.ComponentModel;
namespace WordswitchPdf
{
/// <summary>
/// WebForm1 的摘要说明。5
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
WordConvert();
}
private void WordConvert()
{
Word.ApplicationClass word1= new Word.ApplicationClass();
Type wordType= word1.GetType();
//打开WORD文档
/*对应脚本中的
var word = new ActiveXObject("Word.Application");
var doc = word.Documents.Open(docfile);
*/
Word.Documents docs = word1.Documents;
Type docsType = word1.GetType();
object objDocName = @"d:\ww\test.doc";
object ss= new object();
//ss = docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] {objDocName, true, true});
Word.Document doc = (Word.Document)(ss); //打印输出到指定文件
//你可以使用 doc.PrintOut();方法,次方法调用中的参数设置较繁琐,建议使用 Type.InvokeMember 来调用时可以不用将PrintOut的参数设置全,只设置4个主要参数
Type docType = doc.GetType();
object printFileName = @"d:\ww\test.ps";
docType.InvokeMember("PrintOut",System.Reflection.BindingFlags.InvokeMethod,null,doc,new object[]{false,false,Word.WdPrintOutRange.wdPrintAllDocument,printFileName});
//new object[]{false,false,oWord.WdPrintOutRange.wdPrintAllDocument,printFileName}
//对应脚本中的word.PrintOut(false, false, 0, psfile);的参数
//退出WORD
//对应脚本中的word.Quit();
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod,null, null, null);
object o1= "d:\\ww\\test.ps";
object o2= "d:\\ww\\test.pdf";
object o3= "";
//引用将PS转换成PDF的对象
//try catch之间对应的是脚本中的 PDF.FileToPDF(psfile,pdffile,""); //你可以使用 pdfConvert.FileToPDF("c:\\test.ps","c:\\test.pdf","");这样的转换方法,本人只是为了保持与WORD相同的调用方式
try
{
ACRODISTXLib.PdfDistillerClass ar = new ACRODISTXLib.PdfDistillerClass();
wordType.InvokeMember("FileToPDF",System.Reflection.BindingFlags.InvokeMethod,null,word1,new object[]{o1,o2,o3});
wordType = null;
}
catch{} //读者自己补写错误处理
//为防止本方法调用多次时发生错误,必须停止acrodist.exe进程
foreach(Process proc in System.Diagnostics.Process.GetProcesses())
{
int begpos;
int endpos;
string sProcName = proc.ToString();
begpos = sProcName.IndexOf("(")+1;
endpos = sProcName.IndexOf(")");
sProcName=sProcName.Substring(begpos,endpos-begpos);
if(sProcName.ToLower().CompareTo("acrodist")==0)
{
try
{
proc.Kill(); //停止进程
}
catch{} //读者自己补写错误处理
break;
}
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
运行的时候报
对象与目标类型不匹配。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Reflection.TargetException: 对象与目标类型不匹配。
源错误:
行 47: Type docsType = word1.GetType();
行 48: object objDocName = @"d:\ww\test.doc";
行 49: Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] {objDocName, true, true});
这个错误 反射的时候找不到 (Word.Document) 急求解决办法啊!
--------------------编程问答-------------------- .net call com 已经是比较麻烦了,尤其是调用office , 我感觉office有些方法简直变态,20几个参数,vb 调用还好些,有些可以不添,c++ , 调用简直是灾难。
这里你还非用反射,有些自找麻烦的味道
--------------------编程问答-------------------- 我是把网上的一片文章给转过来 看看 结果没想到 那么复杂有没有解决的办法啊 老大们 公司里急着要 WORD转换PDF --------------------编程问答-------------------- using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using oWord = Microsoft.Office.Interop.Word;
using System.Threading;
using System.Diagnostics;
using Ytcinfo.Utils;
namespace Ytcinfo.Applications.ArchivesFlow.Utils
{
public class ThreadCreatePDF
{
private static ThreadCreatePDF _instance = null;
private ThreadCreatePDF(){
}
private string FilePath
{
get;
set;
}
private Stream Content
{
get;
set;
}
public static ThreadCreatePDF getInstance()
{
if (_instance == null)
{
_instance = new ThreadCreatePDF();
}
return _instance;
}
/// <summary>
/// 把指定路径的word文件转化为PDF文件
/// </summary>
/// <param name="_filePath"></param>
/// <param name="_content"></param>
public void saveFileToDisk(string _filePath,Stream _content)
{
this.FilePath = dealFileLastName(_filePath);
this.Content = _content;
Thread _thread = new Thread(new ThreadStart(saveFileToDisk));
_thread.Start();
}
/// <summary>
/// 去除文件的后缀名称
/// </summary>
/// <param name="?"></param>
/// <returns></returns>
private string dealFileLastName(string _filePath)
{
if(_filePath.Contains("."))
{
_filePath = _filePath.Substring(0, _filePath.LastIndexOf("."));
}
return _filePath;
}
private void saveFileToDisk()
{
lock (this)
{
FileInfo fi = new FileInfo(FilePath);
if (!fi.Directory.Exists)
fi.Directory.Create();
using (FileStream fs = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
{
byte[] buffer = new byte[256 * 1024];
int bytes;
Content.Position = 0;
while ((bytes = Content.Read(buffer, 0, 256 * 1024)) > 0)
fs.Write(buffer, 0, bytes);
fs.Close();
Content.Close();
}
WordConvert();
delOtherFile();
}
}
private void WordConvert()
{
oWord.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
Type wordType = word.GetType();
//打开WORD文档
/*对应脚本中的
var word = new ActiveXObject("Word.Application");
var doc = word.Documents.Open(docfile);
*/
oWord.Documents docs = word.Documents;
Type docsType = docs.GetType();
object objDocName = FilePath;
oWord.Document doc = (oWord.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { objDocName, true, true });
//打印输出到指定文件
//你可以使用 doc.PrintOut();方法,次方法调用中的参数设置较繁琐,建议使用 Type.InvokeMember 来调用时可以不用将PrintOut的参数设置全,只设置4个主要参数
Type docType = doc.GetType();
object printFileName = FilePath + ".ps";
docType.InvokeMember("PrintOut", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { false, false, oWord.WdPrintOutRange.wdPrintAllDocument, printFileName });
//new object[]{false,false,oWord.WdPrintOutRange.wdPrintAllDocument,printFileName}
//对应脚本中的word.PrintOut(false, false, 0, psfile);的参数
//退出WORD
//对应脚本中的word.Quit();
doc = null;
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
object o1 = FilePath + ".ps";
object o2 = FilePath + ".pdf";
object o3 = "";
//引用将PS转换成PDF的对象
//try catch之间对应的是脚本中的 PDF.FileToPDF(psfile,pdffile,""); //你可以使用 pdfConvert.FileToPDF("c:\\test.ps","c:\\test.pdf","");这样的转换方法,本人只是为了保持与WORD相同的调用方式
try
{
ACRODISTXLib.PdfDistillerClass pdf = new ACRODISTXLib.PdfDistillerClass();
pdf.bShowWindow = 0;
pdf.bSpoolJobs = 0;
pdf.FileToPDF(FilePath + ".ps", FilePath + ".pdf", @"Standard");
//Type pdfType = pdfConvert.GetType();
//pdfType.InvokeMember("FileToPDF", System.Reflection.BindingFlags.InvokeMethod, null, pdf, new object[] { o1, o2, o3 });
pdf = null;
}
catch { } //读者自己补写错误处理
//为防止本方法调用多次时发生错误,必须停止acrodist.exe进程
foreach (Process proc in System.Diagnostics.Process.GetProcesses())
{
int begpos;
int endpos;
string sProcName = proc.ToString();
begpos = sProcName.IndexOf("(") + 1;
endpos = sProcName.IndexOf(")");
sProcName = sProcName.Substring(begpos, endpos - begpos);
if (sProcName.ToLower().CompareTo("acrodist") == 0)
{
try
{
proc.Kill(); //停止进程
}
catch { } //读者自己补写错误处理
break;
}
}
}
private void delOtherFile()
{
File.Delete(FilePath);
File.Delete(FilePath + ".log");
File.Delete(FilePath + ".ps");
}
}
}
--------------------编程问答--------------------
补充:.NET技术 , C#