生成 Word 文档的缩略图

发布于 2024-07-24 03:34:06 字数 162 浏览 4 评论 0原文

我有一个网站,用户可以在其中上传 Word 文档,我想显示这些 Word 文档的缩略图。 如果有人知道如何使用 C# 将 Word 文件的第一页显示为图像,请告诉我。

另外,如果您知道一个值得信赖的 .NET 库可以将 Word 文件转换为图像且无需 Office 互操作,那就太好了。

I have a website where users upload Word documents and I want to display thumbnails of these word documents. If anyone of you knows how to display the first page of a Word file as an image using C# please tell me.

Also if you know a trusted .NET library to convert word files to images that requires no office interop that would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甲如呢乙后呢 2024-07-31 03:34:06

http ://blogs.msdn.com/windowssdk/archive/2009/06/12/windows-api-code-pack-for-microsoft-net-framework.aspx

ShellFile shellFile = ShellFile.FromFilePath(pathToYourFile);
Bitmap shellThumb = shellFile.Thumbnail.ExtraLargeBitmap;

这是微软的API代码包

http://blogs.msdn.com/windowssdk/archive/2009/06/12/windows-api-code-pack-for-microsoft-net-framework.aspx

ShellFile shellFile = ShellFile.FromFilePath(pathToYourFile);
Bitmap shellThumb = shellFile.Thumbnail.ExtraLargeBitmap;

It's Microsoft's API Code Pack

南城旧梦 2024-07-31 03:34:06

我在寻找类似的解决方案时发现了这个问题(7年后)。 我正在评估 2JPEG,它似乎支持 275 种格式,包括 Word、Excel、Publisher 和 幻灯片文件。 fCoder 建议将 2JPEG 作为计划后台任务运行。 命令行语法非常全面。

以下是生成特定文件缩略图的示例片段:

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

I found this question (7 yrs later) while searching for a similar solution. I'm evaluating 2JPEG and it appears to support 275 formats including Word, Excel, Publisher & Powerpoint files. fCoder recommends running 2JPEG as a scheduled background task. The command line syntax is pretty comprehensive.

Here's a sample snippet to generate a thumbnail for a specific file:

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
飘然心甜 2024-07-31 03:34:06

.doc 或 .docx 文档第一页的预览图像可以使用名为 Free Spire.Doc for .NET 的工具(用于商业和个人用途的完全免费的 Word API)轻松创建。 我发现它既快速又准确。

开发者页面的注释:

“转换功能允许将 Word 文档(Word 97-2003、Word 2007、Word 2010、Word 2013、Word 2016 和 Word 2019)转换为常用的文件格式,例如 XML、 RTF、TXT、PDF、XPS、EPUB、HTML 和图像等
温馨的提示:
免费版本仅限于 500 个段落和 25 个表格..."

此 C# 代码创建 .docx 文件第一页的 System.Drawing.Image 对象:

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];
}

创建缩略图图片,以下 C# 示例包含第二个 using 块来执行此操作,然后转换为 Base64 字符串:

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());
    }
}

此外,该库还可以以其他方式进行转换,例如此函数返回 .SVG 文件每页:

doc.SaveToFile("resulting_file_name.svg", Spire.Doc.FileFormat.SVG);

A preview image of the 1st page of a .doc or .docx document can easily be created with a tool called Free Spire.Doc for .NET (a totally free word API for commercial and personal use). I found it to be fast and accurate.

Note from the developer's page:

"The featured function, conversion allows converting Word documents (Word 97-2003, Word 2007, Word 2010, Word 2013, Word 2016 and Word 2019) to commonly used file format, such as XML, RTF, TXT, PDF, XPS, EPUB, HTML and Image etc.
Friendly Reminder:
Free version is limited to 500 paragraphs and 25 tables... "

This C# code creates a System.Drawing.Image object of the 1st page of a .docx file:

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];
}

To create the thumbnail image, the following C# example includes a second using block to do it, and then converts to a base64 string:

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());
    }
}

In addition, the library can also convert in other ways, for instance this function returns .SVG files with each page:

doc.SaveToFile("resulting_file_name.svg", Spire.Doc.FileFormat.SVG);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文