#region office 转 PDF
/// <summary>
/// office转pdf
/// </summary>
/// <param name="sourcePath"></param>
/// <returns></returns>
[HttpPost]
public ActionResult Office2Pdf(string sourcePath)
{
var ext = Path.GetExtension(sourcePath).ToLower();
if (ext == ".docx" || ext == ".doc")
return ConvertWord2Pdf(sourcePath);
if (ext == ".xls" || ext == ".xlsx")
return ConvertExcel2Pdf(sourcePath);
if (ext == ".pptx")
return ConvertPowerPoint2Pdf(sourcePath);
return Json(new ApiModel { Code = 0, Message = "暂不支持在线预览" });
}
#region key值 消除印记
public const string Key =
"PExpY2Vuc2U+DQogIDxEYXRhPg0KICAgIDxMaWNlbnNlZFRvPkFzcG9zZSBTY290bGFuZCB" +
"UZWFtPC9MaWNlbnNlZFRvPg0KICAgIDxFbWFpbFRvPmJpbGx5Lmx1bmRpZUBhc3Bvc2UuY2" +
"9tPC9FbWFpbFRvPg0KICAgIDxMaWNlbnNlVHlwZT5EZXZlbG9wZXIgT0VNPC9MaWNlbnNlV" +
"HlwZT4NCiAgICA8TGljZW5zZU5vdGU+TGltaXRlZCB0byAxIGRldmVsb3BlciwgdW5saW1p" +
"dGVkIHBoeXNpY2FsIGxvY2F0aW9uczwvTGljZW5zZU5vdGU+DQogICAgPE9yZGVySUQ+MTQ" +
"wNDA4MDUyMzI0PC9PcmRlcklEPg0KICAgIDxVc2VySUQ+OTQyMzY8L1VzZXJJRD4NCiAgIC" +
"A8T0VNPlRoaXMgaXMgYSByZWRpc3RyaWJ1dGFibGUgbGljZW5zZTwvT0VNPg0KICAgIDxQc" +
"m9kdWN0cz4NCiAgICAgIDxQcm9kdWN0PkFzcG9zZS5Ub3RhbCBmb3IgLk5FVDwvUHJvZHVj" +
"dD4NCiAgICA8L1Byb2R1Y3RzPg0KICAgIDxFZGl0aW9uVHlwZT5FbnRlcnByaXNlPC9FZGl" +
"0aW9uVHlwZT4NCiAgICA8U2VyaWFsTnVtYmVyPjlhNTk1NDdjLTQxZjAtNDI4Yi1iYTcyLT" +
"djNDM2OGYxNTFkNzwvU2VyaWFsTnVtYmVyPg0KICAgIDxTdWJzY3JpcHRpb25FeHBpcnk+M" +
"jAxNTEyMzE8L1N1YnNjcmlwdGlvbkV4cGlyeT4NCiAgICA8TGljZW5zZVZlcnNpb24+My4w" +
"PC9MaWNlbnNlVmVyc2lvbj4NCiAgICA8TGljZW5zZUluc3RydWN0aW9ucz5odHRwOi8vd3d" +
"3LmFzcG9zZS5jb20vY29ycG9yYXRlL3B1cmNoYXNlL2xpY2Vuc2UtaW5zdHJ1Y3Rpb25zLm" +
"FzcHg8L0xpY2Vuc2VJbnN0cnVjdGlvbnM+DQogIDwvRGF0YT4NCiAgPFNpZ25hdHVyZT5GT" +
"zNQSHNibGdEdDhGNTlzTVQxbDFhbXlpOXFrMlY2RThkUWtJUDdMZFRKU3hEaWJORUZ1MXpP" +
"aW5RYnFGZkt2L3J1dHR2Y3hvUk9rYzF0VWUwRHRPNmNQMVpmNkowVmVtZ1NZOGkvTFpFQ1R" +
"Hc3pScUpWUVJaME1vVm5CaHVQQUprNWVsaTdmaFZjRjhoV2QzRTRYUTNMemZtSkN1YWoyTk" +
"V0ZVJpNUhyZmc9PC9TaWduYXR1cmU+DQo8L0xpY2Vuc2U+";
#endregion
[HttpPost]
public ActionResult ConvertWord2Pdf(string sourcePath)
{
string path = sourcePath.Substring(0, sourcePath.LastIndexOf('.') + 1) + "pdf";
sourcePath = Server.MapPath(sourcePath);
if (!System.IO.File.Exists(sourcePath))
return Json(new ApiModel { Code = 0, Message = "文件不存在" });
if (System.IO.File.Exists(Server.MapPath(path)))
return Json(new ApiModel { Code = 1, Data = path });
try
{
new Aspose.Words.License().SetLicense(new MemoryStream(Convert.FromBase64String(Key)));
Document doc = new Document(sourcePath);
doc.Save(Server.MapPath(path), Aspose.Words.SaveFormat.Pdf);
return Json(new ApiModel { Code = 1, Data = path });
}
catch (Exception ex)
{
return Json(new ApiModel { Code = 0, Message = "转换失败" });
}
}
[HttpPost]
public ActionResult ConvertExcel2Pdf(string sourcePath)
{
string path = sourcePath.Substring(0, sourcePath.LastIndexOf('.') + 1) + "pdf";
sourcePath = Server.MapPath(sourcePath);
if (!System.IO.File.Exists(sourcePath))
return Json(new ApiModel { Code = 0, Message = "文件不存在" });
if (System.IO.File.Exists(Server.MapPath(path)))
return Json(new ApiModel { Code = 1, Data = path });
try
{
new Aspose.Cells.License().SetLicense(new MemoryStream(Convert.FromBase64String(Key)));
Workbook excel = new Workbook(sourcePath);
excel.Save(Server.MapPath(path), Aspose.Cells.SaveFormat.Pdf);
return Json(new ApiModel { Code = 1, Data = path });
}
catch (Exception ex)
{
return Json(new ApiModel { Code = 0, Message = "转换失败" });
}
}
[HttpPost]
public ActionResult ConvertPowerPoint2Pdf(string sourcePath)
{
string path = sourcePath.Substring(0, sourcePath.LastIndexOf('.') + 1) + "pdf";
sourcePath = Server.MapPath(sourcePath);
if (!System.IO.File.Exists(sourcePath))
return Json(new ApiModel { Code = 0, Message = "文件不存在" });
if (System.IO.File.Exists(Server.MapPath(path)))
return Json(new ApiModel { Code = 1, Data = path });
try
{
new Aspose.Slides.License().SetLicense(new MemoryStream(Convert.FromBase64String(Key)));
//AsposeLicenseHelper.SetSlidesLicense();
Aspose.Slides.Presentation doc = new Aspose.Slides.Presentation(sourcePath);
doc.Save(Server.MapPath(path), Aspose.Slides.Export.SaveFormat.Pdf);
//doc.Save(outtopdf, Aspose.Words.SaveFormat.Pdf);
return Json(new ApiModel { Code = 1, Data = path });
}
catch (Exception ex)
{
return Json(new ApiModel { Code = 0, Message = "转换失败" });
}
}
#endregion