下载Aspose对应插件并添加引用
//文件名 string fileName; //文件类型 string fileType; //office文件路径,形如 E://test.docx string allPath=HttpContext.Current.Server.MapPath("\File\")+fileName+"."+fileType; //保存路径,形如 E://test.pdf string newPath=HttpContext.Current.Server.MapPath("\File\")+fileName+".pdf"; fileType = fileType.ToLower(); if (fileType.Contains("doc")) { //去除word文件的修改痕迹 Document doc = new Document(allPath); doc.AcceptAllRevisions(); //获取所有修订 doc.TrackRevisions = false; //不保留修订记录 doc.Save(newPath, Aspose.Words.SaveFormat.Pdf); } else if (fileType.Contains("xls")) { //如果表格大于A4宽度会出现截断情况,插件自带的一页显示方法没了,采用读取表格宽度进行缩放 Workbook document = new Workbook(allPath); Aspose.Cells.WorksheetCollection sheet = document.Worksheets; for (int i = 0; i < sheet.Count; i++) { Aspose.Cells.Worksheet ws = sheet[i]; Aspose.Cells.Cells cells = ws.Cells; int columnCount = cells.MaxColumn + 1; //获取表页的最大列数 double tableLength = 0; //表格总宽度 for (int col = 0; col < columnCount; col++) { tableLength += cells.GetColumnWidthPixel(col); } //24像素=0.635厘米 //A4纸宽度(21厘米)-左右边距(1.78 * 2厘米,应该还有哪里有所减,因为就这样计算出来还是不准确,所以我直接减5) / 0.635厘米 * 24像素 / 获取到的表格总宽度 * 100 = 缩放比例 double prevent = Math.Floor((21 - 5) / 0.635 * 24 / tableLength * 100); if (prevent < 100) { ws.PageSetup.Zoom = (int)prevent; } //缩小还好,放大会有误差,而且会失真,所以就不放大了 //else //{ // document.Save(newPath, Aspose.Cells.SaveFormat.Pdf); //} } PdfSaveOptions options = new PdfSaveOptions(); options.DefaultFont = "宋体"; document.Save(newPath, options); } else if (fileType.Contains("ppt")) { Presentation ppt = new Presentation(allPath); ppt.Save(newPath, Aspose.Slides.Export.SaveFormat.Pdf); }