一:打开管理NuGet程序包进行安装Aspose.Pdf.dll
二:实践
(1)获取PDF页数
/// <summary> /// 获取PDF文件页数 /// </summary> /// <param name="file"></param> /// <returns></returns> public int GetPdfNumber(HttpPostedFile file) { int pageCount; //上传临时文件 var picName = Guid.NewGuid().ToString(); //获取图片保存路径 var path = Config.FilePath + "\\" + "tmppic\\"; //图片的路径及名称 var fileinfo = new FileInfo(file.FileName); var ext = fileinfo.Extension; var filePath = path + picName + ext; //创建路径 var dir = new DirectoryInfo(path); if (!dir.Exists) { dir.Create(); } file.SaveAs(filePath); var reader = new PdfReader(filePath); pageCount = reader.NumberOfPages; reader.Close(); File.Delete(filePath); return pageCount; }
(2) PDF转JPG
{ //保存pdf信息到附件表 var device = new Aspose.Pdf.Devices.JpegDevice(80); var path = Config.FilePath + "\\" + item.FilePath; //pdf转为图片 using (var document = new Aspose.Pdf.Document(path)) { for (var t = 1; t <= document.Pages.Count; t++) { //图片名称 var newName = Guid.NewGuid().ToString("N"); //保存图片 var lastIndex = item.FilePath.LastIndexOf(‘\\‘); var newPath = item.FilePath.Substring(0, lastIndex + 1); var filePath = Config.FilePath + "\\" + newPath + newName + ".jpg"; var temp_path = newPath + newName + ".jpg"; var fileInfo = new FileInfo(filePath); using (var fs = new FileStream(filePath, FileMode.OpenOrCreate)) { device.Process(document.Pages[t], fs); fs.Close(); } } }