iT邦幫忙

0

C# excel ,word , powerpoint 轉成 圖檔 JPG

使用C#
要寫出一個 .exe 檔
它要有能把 excel ,word , powerpoint 轉成 圖檔 JPG
還可以調整解析度
有人有做過類似的嗎?
網路上 有免費的轉檔網頁啦
只是好像是不是沒人 寫過這一種code呢?

以下是我找資料寫出的code,已經可以把word轉成jpg了
ptt和excel 也大概改好 只是 很多錯誤

看更多先前的討論...收起先前的討論...
優悠 iT邦新手 3 級 ‧ 2018-04-30 17:45:01 檢舉
有紅色波浪,把滑鼠移動過去看解決辦法
優悠 iT邦新手 3 級 ‧ 2018-04-30 17:48:35 檢舉
把你錯誤清單點出來給我看看
ted8224 iT邦新手 5 級 ‧ 2018-05-02 09:32:39 檢舉
我有貼出來給您看看了
下面 有完整
code
小魚 iT邦大師 1 級 ‧ 2018-05-02 21:08:35 檢舉
很多錯誤,連ppt都寫錯了...
ted8224 iT邦新手 5 級 ‧ 2018-05-04 14:15:54 檢舉
有改好了 那些 ppt 打成 ptt...
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
暐翰
iT邦大師 1 級 ‧ 2018-04-26 16:36:49
ted8224 iT邦新手 5 級 ‧ 2018-04-27 08:07:42 檢舉

好的 謝謝...

0
sam0407
iT邦大師 1 級 ‧ 2018-04-29 08:25:33

沒作過...但若是我要作的話會先找PRN轉JPG的範例,先將這個元件作出來後,以後不管接到任何軟件有轉JPG的需求,就只要再實作該軟件打印PRN的功能就可以了~

看更多先前的回應...收起先前的回應...
ted8224 iT邦新手 5 級 ‧ 2018-04-30 08:21:15 檢舉

好的

ted8224 iT邦新手 5 級 ‧ 2018-05-02 09:28:56 檢舉
            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);
ted8224 iT邦新手 5 級 ‧ 2018-05-02 09:30:20 檢舉

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


    }
}

}

ted8224 iT邦新手 5 級 ‧ 2018-05-02 09:32:15 檢舉

https://ithelp.ithome.com.tw/upload/images/20180502/20109425sgayJC3dAi.jpghttps://ithelp.ithome.com.tw/upload/images/20180502/20109425rykcnklAAY.jpghttps://ithelp.ithome.com.tw/upload/images/20180502/20109425Yj3gaQ6fGt.jpghttps://ithelp.ithome.com.tw/upload/images/20180502/20109425dhsVMOvGNh.jpghttps://ithelp.ithome.com.tw/upload/images/20180502/20109425jDonMyDiFi.jpghttps://ithelp.ithome.com.tw/upload/images/20180502/20109425vMFCekxDtr.jpghttps://ithelp.ithome.com.tw/upload/images/20180502/20109425KLA1EpGhgE.jpghttps://ithelp.ithome.com.tw/upload/images/20180502/20109425s4c8sxo2Kv.jpghttps://ithelp.ithome.com.tw/upload/images/20180502/20109425gaSMVFKjz4.jpghttps://ithelp.ithome.com.tw/upload/images/20180502/20109425J82Mz5cHai.jpghttps://ithelp.ithome.com.tw/upload/images/20180502/201094250KS4lipEVK.jpghttps://ithelp.ithome.com.tw/upload/images/20180502/20109425t0Azt54wSd.jpg

小魚 iT邦大師 1 級 ‧ 2018-05-02 21:13:35 檢舉

我發現第一個錯誤,pttx應該是pptx吧

ted8224 iT邦新手 5 級 ‧ 2018-05-03 11:56:51 檢舉

我有改好一些了
msPptApp.Visible = true;
foreach (Microsoft.Office.Interop.PowerPoint window in appPPT.Windows)

這兩行 求解 它們是有問題的

0
小魚
iT邦大師 1 級 ‧ 2018-05-05 23:04:51

我有稍微試試看,
也是不能編譯的,
你列出來那個地方還好處理,
不過 pane.Pages 無解,
我覺得是專案參考的問題,
你是在哪個地方看到這些Code的?

看更多先前的回應...收起先前的回應...
ted8224 iT邦新手 5 級 ‧ 2018-05-07 08:07:28 檢舉
小魚 iT邦大師 1 級 ‧ 2018-05-07 11:56:45 檢舉

那是Word吧,
你不能直接把word的直接改,
要去找一下Powerpoint跟Excel的,
而且Excel應該問題會比較多,
問題是到底要怎麼分頁這是很大的問題...

小魚 iT邦大師 1 級 ‧ 2018-05-07 12:01:43 檢舉

Powerpoint可以參考這一篇
C#實現 word、pdf、ppt 轉為圖片
暐翰 寫的那個也能用,
不過我目前只試成功PPt的, PPtx不能讀取,
不知道是不是因為我還在用2003的Office...

ted8224 iT邦新手 5 級 ‧ 2018-05-07 13:27:06 檢舉

我要問人他寫法是這樣
https://dotblogs.com.tw/eganblog/2018/05/06/doc_ppt_convert_to_img

只是我不懂裡面為什麼會有
///
/// Microsoft.Office.Interop.PowerPoint 將PPT轉圖片
///
/// PPT檔案位置(FullPath)
/// PPT圖檔輸出位置

這是web裡的前端?還是不太懂.....
他public 前面加那幾行是?

ted8224 iT邦新手 5 級 ‧ 2018-05-07 16:09:47 檢舉

pttPath = @"D:\DocToImg\55.pptx"; //Ptt檔案路徑
outPutPath = @"D:"; //ppt圖片輸出路徑 by Office

請問 我路徑 加這兩行 是錯嗎?
可以run 但感覺好像沒效果

小魚 iT邦大師 1 級 ‧ 2018-05-07 19:02:16 檢舉

我比較好奇的是你那個Word的

object bits = page.EnhMetaFileBits;

我執行到這行就失敗了,不知道你怎麼成功的?

小魚 iT邦大師 1 級 ‧ 2018-05-07 19:04:43 檢舉

Microsoft.Office.Interop.PowerPoint

要加入參考,組件的擴充功能那邊

可以run 但感覺好像沒效果

你有去呼叫那個函式嗎?
有沒有拋出錯誤還是有成功執行了?

ted8224 iT邦新手 5 級 ‧ 2018-05-08 08:18:42 檢舉

Microsoft.Office.Interop.PowerPoint 有加入參考的
小魚大大你word那個我給你網址:
https://dotblogs.com.tw/eganblog/2016/11/06/doc_convert_to_img
我直接複製code 直接貼上
有加一些參考進去

ted8224 iT邦新手 5 級 ‧ 2018-05-08 08:20:57 檢舉

沒事了小魚大大
我執行成功了
我也是複製上面code去run
您有興趣看以下網址
這是ppt轉 圖檔
然後我是路徑2個
要自己在寫兩段這樣
https://dotblogs.com.tw/eganblog/2018/05/06/doc_ppt_convert_to_img

ted8224 iT邦新手 5 級 ‧ 2018-05-08 08:21:37 檢舉

剩下 最後 excel 轉圖檔囉... 繼續加油

小魚 iT邦大師 1 級 ‧ 2018-05-08 12:01:49 檢舉

Doc那個我執行到這行就跳記憶體錯誤

object bits = page.EnhMetaFileBits;

我也不知道為什麼...

小魚 iT邦大師 1 級 ‧ 2018-05-08 12:04:24 檢舉

Powerpoint第二個方法是方便,
不過會有浮水印,
正式用可能要給錢吧...

ted8224 iT邦新手 5 級 ‧ 2018-05-08 13:27:19 檢舉

喔喔 我ppt轉 是使用第一個方法
我word 跟其他 都是 2013 版本

小魚 iT邦大師 1 級 ‧ 2018-05-08 19:37:50 檢舉

跟我猜的一樣,我安裝了Word 2007之後就可以用了...
看來是Word開啟檔案的問題。

ted8224 iT邦新手 5 級 ‧ 2018-05-09 08:10:56 檢舉

讚喔!!~~ 剩下 excel 轉圖檔了...

小魚 iT邦大師 1 級 ‧ 2018-05-18 11:55:51 檢舉

所以目前進度到哪裡了?

ted8224 iT邦新手 5 級 ‧ 2018-05-18 13:11:47 檢舉

一樣 卡Excel 轉 jpG .. 我code 寫好 run 沒辦法

我要發表回答

立即登入回答