+1 这个不错的 你那个代码太长了 而且估计还不能保证数据完整性using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; using Aspose.Words; //去百度下载Aspose.Words.dll public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Aspose.Words; //去百度下载Aspose.Words.dll
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string filename = FileUpload1.FileName; FileUpload1.PostedFile.SaveAs(Server.MapPath("upload/" + filename));
string sourcepath = Server.MapPath("upload/" + filename);
string x = filename.Substring(0, filename.LastIndexOf(".")) + ".pdf";
string targetpath = Server.MapPath("upload/" + x);
Aspose.Words.Document doc = new Aspose.Words.Document(sourcepath);
doc.Save(targetpath, SaveFormat.Pdf);
}
}
你确定那个可以转吗?怎么是错误的呀?确定,一直这么用,你看看你要转换的文件是不是标准的word文档,以及路径是否正确
public bool ConvertToPDF(object sourcePath, object targetPath)
{
bool result = false;
Object missing = System.Reflection.Missing.Value;
//创建一个名为WordApp的组件对象
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document doc = null;
try
{
//创建一个名为WordDoc的文档对象并打开
doc = wordApp.Documents.Open(ref sourcePath, ref missing,ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
//设置保存的格式
object filefarmat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
//保存为PDF
doc.SaveAs(ref targetPath, ref filefarmat, ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing);
result = true;
}
catch(Exception ex)
{
result = false;
_ErrMess = "\r\nError:" + ex.Message +
"\r\nSource:" + ex.Source +
"\r\nStackTrace:" + ex.StackTrace;
}
finally
{
//关闭文档对象
if (doc != null)
{
doc.Close(ref missing, ref missing, ref missing);
doc = null;
}
//推出组建
if (wordApp != null)
{
wordApp.Quit(ref missing, ref missing, ref missing);
wordApp = null;
}
}
return result;
} 試試