码迷,mamicode.com
首页 > 其他好文 > 详细

将PDF转化为wrod

时间:2018-04-10 13:26:53      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:value   ||   搜索   system   nts   false   tput   rdd   for   

  public static void CreateWord(string HtmlPath, string WordSavePath)
        {
            string inputName = HtmlPath;
            //string ext = Path.GetExtension(inputName);
            string outputName = WordSavePath;
            if (File.Exists(inputName))
            {
                object oMissing = System.Reflection.Missing.Value;
                object oTrue = true;
                object oFalse = false;
                Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.Application();
                Microsoft.Office.Interop.Word._Document oWordDoc = new Microsoft.Office.Interop.Word.Document();
                try
                {

                    oWord.Visible = false;
                    // 这里是打开文件的扩展名
                    object openFormat = Microsoft.Office.Interop.Word.WdOpenFormat.wdOpenFormatWebPages;
                    object openName = inputName;

                    oWordDoc = oWord.Documents.Open(ref openName, ref oMissing, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref openFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                    object saveFileName = outputName;
                    oWord.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;  // 居中
                    oWord.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdWebView;//将web视图修改为默认视图,不然打开word的时候会以web视图去展示,而不是默认视图。(唯独这句代码是自己加的 = =|||)
                    //for (int i = 0; i < oWordDoc.Tables.Count; i++)
                    //{
                    //    oWordDoc.Tables[i].PreferredWidth = 600;
                    //}
          
                    // 这里是保存文件的扩展名
                    object saveFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocumentDefault;
                    //去掉软回车
                    oWordDoc.Content.Find.Text = "^l";
                    object FindText, ReplaceWith, Replace;// 
                    object MissingValue = Type.Missing;
                    FindText = "^l";//要查找的文本 
                    ReplaceWith = "^p";//替换文本 
                    Replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
                    /**/
                    /*wdReplaceAll - 替换找到的所有项。 
                    * wdReplaceNone - 不替换找到的任何项。 
                    * wdReplaceOne - 替换找到的第一项。 
                    * */
                    oWordDoc.Content.Find.ClearFormatting();
                    //移除Find的搜索文本和段落格式设置 
                    if (oWordDoc.Content.Find.Execute(
                    ref FindText, ref MissingValue,
                    ref MissingValue, ref MissingValue,
                    ref MissingValue, ref MissingValue,
                    ref MissingValue, ref MissingValue, ref MissingValue,
                    ref ReplaceWith, ref Replace,
                    ref MissingValue, ref MissingValue,
                    ref MissingValue, ref MissingValue))
                    {
                    }

                
                    oWordDoc.SaveAs(ref saveFileName, ref saveFormat, ref oMissing, ref oMissing, ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    oWordDoc.Close(ref oTrue, ref oMissing, ref oMissing);
                    oWord.Quit(ref oTrue, ref oMissing, ref oMissing);
                }
          
            }
        }

 

将PDF转化为wrod

标签:value   ||   搜索   system   nts   false   tput   rdd   for   

原文地址:https://www.cnblogs.com/Zhengxue/p/8778034.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!