使用C#
要寫出一個 .exe 檔
它要有能把 excel ,word , powerpoint 轉成 圖檔 JPG
還可以調整解析度
有人有做過類似的嗎?
網路上 有免費的轉檔網頁啦
只是好像是不是沒人 寫過這一種code呢?
以下是我找資料寫出的code,已經可以把word轉成jpg了
ptt和excel 也大概改好 只是 很多錯誤
沒作過...但若是我要作的話會先找PRN轉JPG的範例,先將這個元件作出來後,以後不管接到任何軟件有轉JPG的需求,就只要再實作該軟件打印PRN的功能就可以了~
好的
            string pttPath = @"D:\DocToImg\55555.pttx"; //Ptt檔案路徑
        string pttoutPutByOfficePath = @"D:\PttToImg\PrintByOffice_Page_"; //Doc圖片輸出路徑 by Office
        var msPttApp = new Microsoft.Office.Interop.PowerPoint.Application();
        msPttApp.Visible = true;
       
        //開啟該Ptt檔
        //           Microsoft.Office.Interop.Word.Document doc = msWordApp.Documents.Open(docPath);
        //           Microsoft.Office.Interop.PowerPoint.MediaBookmarks = msPttApp.Presentations.Open(pttPath);
        Microsoft.Office.Interop.PowerPoint.Application appPPT = new Microsoft.Office.Interop.PowerPoint.Application();
        var pptSlide = appPPT.Presentations.Open(pttPath);
        //Opens the word document and fetch each page and converts to image
  //          foreach (Microsoft.Office.Interop.Word.Window window in doc.Windows)
            foreach (Microsoft.Office.Interop.PowerPoint window in appPPT.Windows)
            {
                foreach (Microsoft.Office.Interop.PowerPoint.Pane pane in window.Panes)
                {
                    
                    for (var i = 1; i <= pane.Pages.Count; i++)
                    {
                        var page = pane.Pages[i];
                        object bits = page.EnhMetaFileBits;//Returns a Object that represents a picture 【Read-only】
                                                           //以下Try Catch區段為將圖片的背景填滿為白色,不然輸出的圖片背景會是透明的
                        try
                        {
                            using (var ms = new MemoryStream((byte[])bits))
                            {
                                System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
                                using (var backGroundWritePNG = new Bitmap(image.Width, image.Height))
                                {
                                    //設定圖片的解析度
                                    backGroundWritePNG.SetResolution(image.HorizontalResolution, image.VerticalResolution);
                                    using (Graphics graphis = Graphics.FromImage(backGroundWritePNG))
                                    {
                                        graphis.Clear(Color.White);
                                        graphis.DrawImageUnscaled(image, 0, 0);
                                    }
                                    string outPutFilePath = Path.ChangeExtension(pttoutPutByOfficePath + i, "png");
                                    backGroundWritePNG.Save(outPutFilePath, ImageFormat.Png);//輸出圖片
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.ToString());
                        }
                    }
                }
            }
        //關閉Ptt,釋放資源
        appPPT.Close(Type.Missing, Type.Missing, Type.Missing);
        msPttApp.Quit(Type.Missing, Type.Missing, Type.Missing);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(pttPath);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(msPttApp);
string xlsxPath = @"D:\DocToImg\Source.xlsx"; //Excel檔案路徑
string xlsxPutByOfficePath = @"D:\DocToImg\PrintByOffice_Page_"; //Excel圖片輸出路徑 by Office
        var msxlsxApp = new Microsoft.Office.Interop.Excel.Application();
        msxlsxApp.Visible = true;
        //         開啟該Excel檔,天啊! 真的開起Excel我真得是醉了
        Microsoft.Office.Interop.Excel.Document excel = msxlsxApp.Documents.Open(xlsxPath);
        //      Opens the word document and fetch each page and converts to image
        foreach (Microsoft.Office.Interop.Excel.Window window in excel.Windows)
        {
            foreach (Microsoft.Office.Interop.Excel.Pane pane in window.Panes)
            {
                for (var i = 1; i <= pane.Pages.Count; i++)
                {
                    var page = pane.Pages[i];
                    object bits = page.EnhMetaFileBits;//Returns a Object that represents a picture 【Read-only】
                                                       //           以下Try Catch區段為將圖片的背景填滿為白色,不然輸出的圖片背景會是透明的
                    try
                    {
                        using (var ms = new MemoryStream((byte[])bits))
                        {
                            System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
                            using (var backGroundWritePNG = new Bitmap(image.Width, image.Height))
                            {
                                //                       設定圖片的解析度
                                backGroundWritePNG.SetResolution(image.HorizontalResolution, image.VerticalResolution);
                                using (Graphics graphis = Graphics.FromImage(backGroundWritePNG))
                                {
                                    graphis.Clear(Color.White);
                                    graphis.DrawImageUnscaled(image, 0, 0);
                                }
                                string outPutFilePath = Path.ChangeExtension(xlsxPutByOfficePath + i, "png");
                                backGroundWritePNG.Save(outPutFilePath, ImageFormat.Png);//輸出圖片
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                }
            }
        }
        //        關閉Word,釋放資源
        excel.Close(Type.Missing, Type.Missing, Type.Missing);
        msxlsxApp.Quit(Type.Missing, Type.Missing, Type.Missing);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(msxlsxApp);
    }
}
}
我有稍微試試看,
也是不能編譯的,
你列出來那個地方還好處理,
不過 pane.Pages 無解,
我覺得是專案參考的問題,
你是在哪個地方看到這些Code的?
那是Word吧,
你不能直接把word的直接改,
要去找一下Powerpoint跟Excel的,
而且Excel應該問題會比較多,
問題是到底要怎麼分頁這是很大的問題...
Powerpoint可以參考這一篇
C#實現 word、pdf、ppt 轉為圖片
暐翰 寫的那個也能用,
不過我目前只試成功PPt的, PPtx不能讀取,
不知道是不是因為我還在用2003的Office...
我要問人他寫法是這樣
https://dotblogs.com.tw/eganblog/2018/05/06/doc_ppt_convert_to_img
只是我不懂裡面為什麼會有
/// 
/// Microsoft.Office.Interop.PowerPoint  將PPT轉圖片
/// 
/// PPT檔案位置(FullPath)
/// PPT圖檔輸出位置
這是web裡的前端?還是不太懂.....
他public 前面加那幾行是?
pttPath = @"D:\DocToImg\55.pptx"; //Ptt檔案路徑
outPutPath = @"D:"; //ppt圖片輸出路徑 by Office
請問 我路徑 加這兩行 是錯嗎?
可以run 但感覺好像沒效果
我比較好奇的是你那個Word的
object bits = page.EnhMetaFileBits;
我執行到這行就失敗了,不知道你怎麼成功的?
Microsoft.Office.Interop.PowerPoint
要加入參考,組件的擴充功能那邊
可以run 但感覺好像沒效果
你有去呼叫那個函式嗎?
有沒有拋出錯誤還是有成功執行了?
Microsoft.Office.Interop.PowerPoint 有加入參考的
小魚大大你word那個我給你網址:
https://dotblogs.com.tw/eganblog/2016/11/06/doc_convert_to_img
我直接複製code 直接貼上
有加一些參考進去
沒事了小魚大大
我執行成功了
我也是複製上面code去run
您有興趣看以下網址
這是ppt轉 圖檔
然後我是路徑2個
要自己在寫兩段這樣
https://dotblogs.com.tw/eganblog/2018/05/06/doc_ppt_convert_to_img
剩下 最後 excel 轉圖檔囉... 繼續加油
Doc那個我執行到這行就跳記憶體錯誤
object bits = page.EnhMetaFileBits;
我也不知道為什麼...
Powerpoint第二個方法是方便,
不過會有浮水印,
正式用可能要給錢吧...
喔喔 我ppt轉 是使用第一個方法
我word 跟其他 都是 2013 版本
跟我猜的一樣,我安裝了Word 2007之後就可以用了...
看來是Word開啟檔案的問題。