zoukankan      html  css  js  c++  java
  • PDF转换成Word,ppt转换成word

    pdf与word我没找到直接转换的方式,不过可以用间接方式嘛!

    pdf ==》picture ==》word!ppt转word的原理也是先把ppt转成图片,再把图片插入word!

    先准备好几个程序集:fontbox-0.1.0-dev.dll,IKVM.GNU.Classpath.dll,IKVM.Runtime.dll,Interop.Microsoft.Office.Core.dll,PDFBox-0.7.3.dll,Spire.Doc.dll,Spire.License.dll,Spire.Pdf.dll

    一个小工具:gswin32.exe

    pdf转word主要代码:

            /// <summary>
            /// 将PDF文档转换成图片
            /// </summary>
            /// <param name="pdfFile">PDF文档物理路径</param>
            /// <param name="imgPath">转换成的图片文件的存放物理路径</param>
            /// <param name="isDeletePDF">转换成图片文件以后是否删除原PDF文档</param>
            /// <returns>返回转换成的图片文件物理路径的集合</returns>
            public List<string> PdfToImages(string pdfFile, string imgPath, bool isDeletePDF)
            {
                List<string> imgList = new List<string>();
    
                PDDocument doc = PDDocument.load(pdfFile);
    
                int pageCount = doc.getDocumentCatalog().getAllPages().size();//计算pdf文档的总页数
    
                string pdfFileName = Path.GetFileName("");
                int index = pdfFileName.LastIndexOf('.');
                if (index != -1)
                    pdfFileName = pdfFileName.Substring(0, index);
    
                string imgFile = Path.Combine(imgPath, pdfFileName);//转换成的图片文件
    
                if (pageCount == 0) return null;
                if (pageCount == 1)
                {
                    imgFile += ".jpg";
                    imgList.Add(imgFile);
                    if (File.Exists(imgFile)) File.Delete(imgFile);
                }
                else
                {
                    for (int i = 0; i < pageCount; i++)
                    {
                        string _imgFile = imgFile + (i + 1).ToString() + ".jpg";
                        imgList.Add(_imgFile);
                        if (File.Exists(_imgFile)) File.Delete(_imgFile);
                    }
                    imgFile += "%d.jpg";
                }
                //此调用方法有命令框出现
               // System.Diagnostics.Process.Start(System.Configuration.ConfigurationManager.AppSettings["GhostScriptView"] +"\gswin32.exe", System.Configuration.ConfigurationManager.AppSettings["GhostScriptArguments"] + @" -sOutputFile=" + imgFile + "  " + pdfFile);
    
                ProcessStartInfo startinfo = new ProcessStartInfo();
                startinfo.CreateNoWindow = true;
                startinfo.FileName = System.Configuration.ConfigurationManager.AppSettings["GhostScriptView"] + "\gswin32.exe";
                startinfo.Arguments = System.Configuration.ConfigurationManager.AppSettings["GhostScriptArguments"] + @" -sOutputFile=" + imgFile + "  " + pdfFile;
                startinfo.WindowStyle = ProcessWindowStyle.Hidden;
                startinfo.CreateNoWindow = true;
                startinfo.UseShellExecute = true;
                Process p = new Process();
                p.StartInfo = startinfo;
                p.Start();
                if (p.WaitForExit(60*1000*10))
                {
                    Thread.Sleep(1000);
                    p.Close();
                }
    
                if (isDeletePDF)
                {
                    File.Delete(pdfFile);
                }
    
                return imgList;
            }

    /// <summary>
    /// 图片插入word
    /// </summary>
    /// <param name="ImagesURL">图片路径集合</param>
    public void PicToWord(List<string> ImagesURL)
    {
      //Thread.Sleep(1000);
      //Process[] thepro = Process.GetProcessesByName("gswin32");
      //if (thepro.Length > 0)
      ////如果进程曾在或者不止一个 
      //{
        // //逐个结束 
        // for (int i = 0; i < thepro.Length; i++)
        // {
          // //如果还没有结束就关闭他 
          // if (!thepro[i].CloseMainWindow()) thepro[i].Kill();
        // }
      //}
    
    
      for (int i = 0; i < ImagesURL.Count; i++)
      {
        if (File.Exists(ImagesURL[i].ToString() + ".jpg"))
        {
        File.Delete(ImagesURL[i].ToString() + ".jpg");
        }
        if (File.Exists(ImagesURL[i].ToString()))
        {
        File.Copy(ImagesURL[i].ToString(), ImagesURL[i].ToString() + ".jpg");
        }
      }
     //ThreadPool.QueueUserWorkItem( //使用线程池 
     //(P_temp) => //使用lambda表达式 
     //{
          word.Application newapp = new word.Application(); //创建Word应用程序对象
          object nothing = System.Reflection.Missing.Value;
          word.Document newdoc = newapp.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);//生成一个word文档 
          if (newdoc != null)
          {
            try
              {
                for (int i = (ImagesURL.Count - 1); i >= 0; i--)
                {
                  string a = ImagesURL[i].ToString() + ".jpg";
                  newdoc.InlineShapes.AddPicture(a);
                  File.Delete(ImagesURL[i].ToString());
                  File.Delete(ImagesURL[i].ToString() + ".jpg");
                }
    
              }
              catch (Exception ex)
              {
                this.TextBox1.Text = ex.Message;
              }
    
              // newdoc.Close(); 
              newdoc.SaveAs("F:\Test.docx");
              newapp.Quit();
          }

      // });
    }

     

    ppt转word主要代码:

            /// <summary>
            /// PPT转Word
            /// </summary>
            /// <param name="pptPath">ppt文件路径</param>
            /// <param name="imgPath">图片路径</param>
            /// <param name="WordPath">存放word文档路径</param>
            /// <returns></returns>
            public bool PPTToWord(string pptPath,string imgPath,string WordPath)
            {
                bool bo=true;
                List<string> image = new List<string>();
                try
                {
                    var app = new PowerPoint.Application();
                    var ppt = app.Presentations.Open(pptPath, Core.MsoTriState.msoFalse, Core.MsoTriState.msoFalse, Core.MsoTriState.msoFalse);
                    var index = 0;
                    var fileName = System.IO.Path.GetFileNameWithoutExtension(pptPath);
                    foreach (PowerPoint.Slide slid in ppt.Slides)
                    {
                        ++index;
                        //设置图片大小
                        slid.Export(imgPath + string.Format("page{0}.png", index.ToString()), "png", 1024, 768);
                        image.Add(imgPath + string.Format("page{0}.png", index.ToString()));
                    }
                    //释放资源
                    ppt.Close();
                    app.Quit();
                    GC.Collect();
                }
                catch (Exception ex)
                {
                    bo = false ;
                }
    
                Word.Application newapp = new Word.Application();         //创建Word应用程序对象
                object nothing = System.Reflection.Missing.Value;
                Word.Document newdoc = newapp.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);//生成一个word文档 
                if (newdoc != null)
                {
                    try
                    {
                        for (int i = (image.Count - 1); i >= 0; i--)
                        {
                            string a = image[i].ToString();
                            newdoc.InlineShapes.AddPicture(a);
                            File.Delete(image[i].ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                        bo = false;
                    }
                    newdoc.SaveAs(WordPath);
                    newapp.Quit();
                }
                return bo;
            }

    这就ok了!

    但是!当小女子放到服务器(win2008)上发现,天呐!bug!bug!bug!pdf能转成图片,但是在插入图片的时候报错,未将对象引用到实例!再看看ppt转word。Oh my god!ppt不能open!

    奇怪,在我的电脑上没问题呀,这是怎么回事呢!仔细看看规律。pdf与office不是同一回事,出问题的都是office!抱着试试的心态,给office赋予权限去!

    comexp.msc -32,打开32位的组件服务。"组件服务"->"计算机"->"我的电脑"->"DCOM配置"。找到office,给每一个office配置都赋予权限。

    1、右键==》属性==》安全==》启动和激活权限==》自定义==》编辑==》添加Everyone==》本地启动、本地激活。

    2、右键==》属性==》安全==》访问权限==》自定义==》编辑==》添加Everyone==》本地访问。

    3、右键==》属性==》安全==》配置权限==》自定义==》编辑==》添加Everyone==》完全控制。

    4、右键==》属性==》标识==》交互使用户。

    看看我们的程序,这个时候已经完全成功了!!!!

     程序集与gswin32下载

  • 相关阅读:
    Vue学习之监听methods、watch及computed比较小结(十一)
    Vue学习之路由vue-router传参及嵌套小结(十)
    Vue学习之路由vue-router小结(九)
    Vue学习之组件切换及父子组件小结(八)
    Vue学习之全局和私有组件小结(七)
    Vue学习之动画小结(六)
    Vue学习之vue-resource小结(五)
    Vue学习之生命周期钩子小结(四)
    Vue学习之过滤器和自定义指令小结(三)
    Vue学习之品牌案例部分代码小结(二)
  • 原文地址:https://www.cnblogs.com/xielianghui/p/7660225.html
Copyright © 2011-2022 走看看