我有一个网站,用户上传Word文档,我想显示这些Word文档的缩略图。如果您知道如何使用C#将Word文件的第一页显示为图像,请告诉我。
另外,如果您知道一个可信的.NET库,可以将Word文件转换为图像,而无需使用Office互操作性,那就太好了。
另外,如果您知道一个可信的.NET库,可以将Word文件转换为图像,而无需使用Office互操作性,那就太好了。
ShellFile shellFile = ShellFile.FromFilePath(pathToYourFile);
Bitmap shellThumb = shellFile.Thumbnail.ExtraLargeBitmap;
2jpeg.exe -src "c:\files\myfile.docx" -dst "c:\files" -oper Resize size:"100 200" fmode:fit_width -options pages:"1" scansf:no overwrite:yes template:"{Title}_thumb.jpg" silent:yes
使用名为Free Spire.Doc for .NET的工具(一种完全免费的用于商业和个人使用的Word API),可以轻松创建.doc或.docx文档第一页的预览图像。我发现它快速而准确。
开发者页面的说明:
“特色功能转换允许将Word文档(Word 97-2003、Word 2007、Word 2010、Word 2013、Word 2016和Word 2019)转换为常用文件格式,如XML、RTF、TXT、PDF、XPS、EPUB、HTML和图像等。 友情提示:免费版本限制为500段落和25个表格……”
以下C#代码创建了一个System.Drawing.Image对象,该对象来自.docx文件的第一页:
using Spire.Doc
byte[] docContent = File.ReadAllBytes(@"C:\Temp\word_document.docx");
using (MemoryStream ms = new MemoryStream(docContent))
{
// Creates a Spire.Doc object to work with
Spire.Doc.Document doc = new Spire.Doc.Document(ms, Spire.Doc.FileFormat.Auto);
// SaveToImages creates an array of System.Drawing.Image, we take only the 1st element
System.Drawing.Image img = doc.SaveToImages(0, 1, Spire.Doc.Documents.ImageType.Bitmap)[0];
}
using Spire.Doc
byte[] docContent = File.ReadAllBytes(@"C:\Temp\word_document.docx");
using (MemoryStream ms = new MemoryStream(docContent))
{
// Creates a Spire.Doc object to work with
Spire.Doc.Document doc = new Spire.Doc.Document(ms, Spire.Doc.FileFormat.Auto);
// SaveToImages creates an array of System.Drawing.Image, we take only the 1st element
System.Drawing.Image img = doc.SaveToImages(0, 1, Spire.Doc.Documents.ImageType.Bitmap)[0];
using (var ms2 = new MemoryStream())
{
// Auxiliary object needed for GetThumbnailImage
System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
// We create a thumbnail (0.5 width and height = 50%)
img.GetThumbnailImage((int)(img.Width * 0.5), (int)(img.Height * 0.5), myCallback, IntPtr.Zero).Save(ms2, System.Drawing.Imaging.ImageFormat.Png);
// Convert to Base64 string representation of the image
return Convert.ToBase64String(ms2.ToArray());
}
}
doc.SaveToFile("resulting_file_name.svg", Spire.Doc.FileFormat.SVG);