Office文件上传自动生成缩略图

来源:微信公众号CodeL

前不久产品经理提出一个X的需求,说上传office文件的时候需要将首页自动截图,用于显示文件列表的时候将文件第一页缩略图展示给用户。
实现的方式有多种,这里给大家介绍一个简单实用的方案,用起来非常方便。

1.Aspose.Pdf实现将pdf转换为图片功能,获取pdf文件流 通过aspose读取第一页保存为图片

  //filestream为pdf文件流

Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(filestream);

//saveDirectory为保存文件的位置

using (FileStream imageStream = new FileStream(saveDirectory, FileMode.Create)){

Resolution resolution = new Resolution(300);

JpegDevice jpegDevice = new JpegDevice(resolution, 100);//将第一页保存为图片

jpegDevice.Process(pdfDocument.Pages[1], imageStream);

imageStream.Close();

}

1.Aspose.Cells实现将excel转换为图片功能,获取excel[Sheet1] ,通过aspose读取保存为图片

Workbook workbook = new Workbook(filestream);

Worksheet sheet = workbook.Worksheets[0];

sheet.PageSetup.LeftMargin = 0;

sheet.PageSetup.RightMargin = 0;

sheet.PageSetup.BottomMargin = 0;

sheet.PageSetup.TopMargin = 0;

ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();

imgOptions.ImageFormat = ImageFormat.Jpeg;

imgOptions.OnePagePerSheet = true;

imgOptions.PrintingPage = PrintingPageType.IgnoreBlank;

SheetRender sr = new SheetRender(sheet, imgOptions);

sr.ToImage(0,saveDirectory);

3.Aspose.Words实现将word转换为图片功能,获取word文件流 通过aspose读取保存为图片

Aspose.Words.Document doc = new Aspose.Words.Document(filestream);

doc.Save(saveDirectory,Aspose.Words.SaveFormat.Jpeg);

是不是很方便,只要把上传文件时文件流获取然后通过Aspose即可保存为图片,当然Aspose拥有更强大的功能,这里只是用它来保存了一张缩略图,它可以将相应的文件相互灵活的转换,不过Aspose的dll是付费的 破解版只能转换3页内容,不过做个首页截图功能破解版的已经够了!

原文链接:http://mp.weixin.qq.com/s?__biz=MzIzNTE2OTk1MA==&mid=402768932&idx=1&sn=0f091d8a9ebf006f5e5d128304d06e63#rd
相关资源获取或其他疑问可在扫码添加CodeL公众号留言。(微信公众号: codelir)

微信扫一扫获取更多开发资源:

原文地址:https://www.cnblogs.com/codelir/p/5135644.html