c#实现Word转换PNG图片

由于项目需要,经过一些大神的指导以及github,stackOverflow找资料,写了个这么个程序。

主要是因为word文档有特殊字体,特殊字体处理就要用到EnhMetaFileBits,即获取一页内容的增强图元信息。类型属于 EmfPlusDual。

在此,特别感谢github,stackOverflow,csdn这三个网站,能够分享技术的人,乐于帮助别人的人。

虽然大部分代码能够在网上找到,但里面也有一丁点自己的思想,也算是自己对代码业的一丁点贡献吧。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InteropWord = Microsoft.Office.Interop.Word;
using System.IO;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using Microsoft.Office.Interop.Word;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Word2Png w = new Word2Png();
            w.SaveToImages("d:\\");
        }
    }
    class Word2Png
    {
        Application wordApp = null;
        private InteropWord.Document document = null;
        public void SaveToImages(string directory)
        {
            this.wordApp = new Application();
           this.document =  this.wordApp.Documents.Open("c:\\2.docx");
             
            InteropWord.Windows windows = this.document.Windows;
            int windowCount = windows.Count;
            for (var i = 1; i <= windowCount; i++)
            {
                InteropWord.Window win = windows[i];
                InteropWord.View windowsView = win.View;
 
                // Pages can only be retrieved in print layout view.
                windowsView.Type = InteropWord.WdViewType.wdPrintView;
 
                InteropWord.Panes panes = win.Panes;
                int paneCount = panes.Count;
                for (var j = 1; j <= paneCount; j++)
                {
                    InteropWord.Pane pane = panes[j];
                    var pages = pane.Pages;
                    var pageCount = pages.Count;
                    for (var k = 1; k <= pageCount; )
                    {
                        InteropWord.Page p = null;
 
                        try
                        {
                            p = pages[k];
                        }
                        catch
                        {
                            // pages[k] sometimes throws exception: 'System.Runtime.InteropServices.COMException: The requested member of the collection does not exist'.
                            // This is a workaround for this issue.
                            continue;
                        }
 
                        var bits = p.EnhMetaFileBits;
                        var target =directory+  string.Format(@"\{0}_image.doc", k);
                        using (var ms = new MemoryStream((byte[])(bits)))
                        {
                            var image = System.Drawing.Image.FromStream(ms);
                            var imageTarget = Path.ChangeExtension(target, "png");
                            image.Save(imageTarget, ImageFormat.Png);
                        }
 
                        Marshal.ReleaseComObject(p);
                        p = null;
 
                        k++;
                    }
 
                    Marshal.ReleaseComObject(pages);
                    pages = null;
 
                    Marshal.ReleaseComObject(windowsView);
                    windowsView = null;
 
                    Marshal.ReleaseComObject(pane);
                    pane = null;
                }
 
                Marshal.ReleaseComObject(panes);
                panes = null;
 
                Marshal.ReleaseComObject(win);
                win = null;
            }
 
            Marshal.ReleaseComObject(windows);
            windows = null;
        }
 
        public void Close()
        {
            if (this.document != null)
            {
                ((InteropWord._Document)this.document).Close();
 
                Marshal.ReleaseComObject(this.document);
                this.document = null;
                 
            }
        }
    }
}

感谢每一位阅读此篇文章的人,希望可以帮到你。

posted @   灰主流  阅读(1684)  评论(10编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?
点击右上角即可分享
微信分享提示