PDF/WORD/EXCEL/PPT 文档在线阅读

时间:2022-05-21 06:08:47

查资料看了2种解决方法:

1.通过办公软件dll转换,用flans去看

2.通过Aspose转换成pdf格式,在用js前台读pdf(我用的pdf.js)

今天我解决的就是WORD/EXCEL/PPT 转化成 PDF ,然后 PDF在线阅读

1.WORD/PPT/EXCEL转PPT类(很简单的)

using Aspose.Words;
using Aspose.Cells;
using Aspose.Slides; /// <summary>
/// Office2Pdf 将Office文档转化为pdf
/// </summary>
public class Office2Pdf
{
public Office2Pdf()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// Word转换成pdf
/// </summary>
/// <param name="sourcePath">源文件路径</param>
/// <param name="targetPath">目标文件路径</param>
/// <returns>true=转换成功</returns>
public bool DOCConvertToPDF(string sourcePath, string targetPath)
{
bool result = false;
try
{
Document doc = new Document(sourcePath);
doc.Save(targetPath, Aspose.Words.SaveFormat.Pdf);
result = true;
}
catch
{
result = false;
}
finally
{
}
return result;
} /// <summary>
/// 把Excel文件转换成PDF格式文件
/// </summary>
/// <param name="sourcePath">源文件路径</param>
/// <param name="targetPath">目标文件路径</param>
/// <returns>true=转换成功</returns>
public bool XLSConvertToPDF(string sourcePath, string targetPath)
{
bool result = false; try
{
//Excel
Workbook excel = new Workbook(sourcePath);
excel.Save(targetPath, Aspose.Cells.SaveFormat.Pdf);
result = true;
}
catch
{
result = false;
}
finally
{ }
return result;
}
///<summary>
/// 把PowerPoint文件转换成PDF格式文件
///</summary>
///<param name="sourcePath">源文件路径</param>
///<param name="targetPath">目标文件路径</param>
///<returns>true=转换成功</returns>
public bool PPTConvertToPDF(string sourcePath, string targetPath)
{
bool result;
try
{
//PPT
Presentation ppt = new Presentation(sourcePath);
ppt.Save(targetPath, Aspose.Slides.Export.SaveFormat.Pdf);
result = true;
}
catch
{
result = false;
}
finally
{
}
return result;
}
}

2.做一个上传页面(核心代码)

        private string upFile()
{
int iTotal = Request.Files.Count; if (iTotal == )
{
_msg = "没有数据";
}
else
{
HttpPostedFile file = Request.Files[];
string path = "file\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\";
string viewPath = "PDF\\web\\" + path;
string ArticlePath = System.Web.HttpContext.Current.Server.MapPath("~") + viewPath;
if (file.ContentLength > || !string.IsNullOrEmpty(file.FileName))
{
//建立图片主文件夹
if (!Directory.Exists(ArticlePath))
{
Directory.CreateDirectory(ArticlePath);
}
saveName = Path.GetFileName(file.FileName);
string extension = Path.GetExtension(file.FileName).ToLower();
string fileName = DateTime.Now.ToString("HH-mm-ss") + extension;
ArticlePath += fileName;
//保存文件
file.SaveAs(ArticlePath);
string pdfpath = ArticlePath.Substring(, ArticlePath.Length - extension.Length) + ".pdf";
if (extension == ".doc" || extension == ".docx")
{
office2pdf.DOCConvertToPDF(ArticlePath, pdfpath);
}
else if (extension == ".ppt" || extension == ".pptx")
{
office2pdf.PPTConvertToPDF(ArticlePath, pdfpath);
}
else if (extension == ".xls" || extension == ".xlsx")
{
office2pdf.XLSConvertToPDF(ArticlePath, pdfpath);
}
else if (extension == ".pdf")
{
}
else
{
this.RegisterStartupScript("newWindow", "<script language='javascript'>alert('非法文件!')</script>");
return "";
}
DelFile(ArticlePath);
savePath = path + fileName.Substring(, fileName.Length - extension.Length) + ".pdf";
return savePath;
}
}
return ""; }

3.效果如下

PPT    :

PDF/WORD/EXCEL/PPT 文档在线阅读

WORD:

PDF/WORD/EXCEL/PPT 文档在线阅读

4. DEMO: 下载 (新手上路,希望大家多多指点  )