Acrobat 转换pdf到png的另一种方法

此方法效率较低,大概2秒转3页pdf成png图片,但是可以保证图片质量很高,分辨率很高。有优化的地方,但没时间研究。先放代码吧。

 前提是安装 acrobat 11(即acrobat xi)

 

  CAcroApp mApp = new AcroApp();
            mApp.Hide();
            String szPdfPathConst = AppDomain.CurrentDomain.BaseDirectory + @"深入浅出WPF.pdf";
            AcroAVDoc g_AVDoc = new AcroAVDoc();
            //open the PDF
            if (g_AVDoc.Open(szPdfPathConst, ""))
            {
                int currentIndex = 0;
                bool formDataExists = true;


                Object newDoc = null;


                if (g_AVDoc.IsValid())
                {

                    CAcroPDDoc pdDoc = (CAcroPDDoc)g_AVDoc.GetPDDoc();
                    int iNum = pdDoc.GetNumPages();
                    Object jsObj = pdDoc.GetJSObject();
                    if (formDataExists)
                    {
                        Type T = jsObj.GetType();

                        object[] extractPagesParam = { currentIndex,iNum-1 };
                        newDoc = T.InvokeMember(
                            "extractPages",
                            BindingFlags.InvokeMethod |
                            BindingFlags.Public |
                            BindingFlags.Instance,
                            null, jsObj, extractPagesParam); 

                        String newFileName = AppDomain.CurrentDomain.BaseDirectory + "data\\" + currentIndex + ".png";
                        Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "data");


                        object[] saveAsParam = { newFileName, "com.adobe.acrobat.png" };
                        T.InvokeMember(
                        "saveAs",
                        BindingFlags.InvokeMethod |
                        BindingFlags.Public |
                        BindingFlags.Instance,
                        null, newDoc, saveAsParam);
                        formDataExists = currentIndex != iNum - 1;


                        object[] closeDocParam = { true };
                        T.InvokeMember(
                                "closeDoc",
                                BindingFlags.InvokeMethod |
                                BindingFlags.Public |
                                BindingFlags.Instance,
                                null, newDoc, closeDocParam);

                        currentIndex++;
                    }
                }
            }
            mApp.CloseAllDocs();
            mApp.Exit();

 

  感谢阅读的人,希望可以帮到你。 

 

posted @ 2017-02-22 15:57  灰主流  阅读(517)  评论(0编辑  收藏  举报