c#實現Word轉換PNG圖片


由於項目需要,經過一些大神的指導以及github,stackOverflow找資料,寫了個這么個程序。

主要是因為word文檔有特殊字體,特殊字體處理就要用到EnhMetaFileBits,即獲取一頁內容的增強圖元信息。類型屬於 EmfPlusDual。

在此,特別感謝github,stackOverflow,csdn這三個網站,能夠分享技術的人,樂於幫助別人的人。

雖然大部分代碼能夠在網上找到,但里面也有一丁點自己的思想,也算是自己對代碼業的一丁點貢獻吧。

代碼:

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

感謝每一位閱讀此篇文章的人,希望可以幫到你。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM