文件预览 word,xlsx,pptx 转 Pdf
#region 文件预览
/// <summary>
/// 文件预览
/// </summary>
/// <param name="fileurl">文件路径</param>
/// <returns></returns>
[HttpGet("PreviewLocal")]
[AllowAnonymous]
public string PreviewLocalFile(string fileurl)
{
if(string.IsNullOrWhiteSpace(fileurl))
{
return "";
}
else
{
var fileDir = Path.Combine(_hostingEnvironment.WebRootPath, "UploadPreview") + "\\";
string extension = Path.GetExtension(fileurl);//扩展名
string fileNameExtension = Path.GetFileName(fileurl);// 文件名
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileurl);//没有扩展名的文件名
var pdfSavePath = fileDir + fileNameWithoutExtension + ".pdf";//保存pdf的路径
if (extension == ".xlsx" || extension == ".xls")
{
Workbook excel = new Workbook(fileurl);
if (excel != null)
{
excel.Save(pdfSavePath, SaveFormat.Pdf);
}
}
else if (extension == ".docx" || extension == ".doc")
{
var document = new Aspose.Words.Document(fileurl);
if (document != null)
{
document.Save(pdfSavePath, Aspose.Words.SaveFormat.Pdf);
}
}
else if (extension == ".pptx" || extension == ".ppt")
{
Presentation presentation = new Presentation();
presentation.LoadFromFile(fileurl);
presentation.SaveToFile(fileurl.Substring(0, fileurl.LastIndexOf(".")) + ".pdf", FileFormat.PDF);
}
return "/Preview/wwwroot/UploadPreview/" + fileNameWithoutExtension + ".pdf";
}
}
#endregion