Esempio n. 1
0
        public override string ConvertToString(string path, string blockText)
        {
            Spire.Doc.Document doc  = null;
            string             text = null;

            try
            {
                doc = new Spire.Doc.Document();
                doc.LoadFromFile(path);
                try
                {
                    doc.Sections[0].HeadersFooters.Header.ChildObjects.Clear();
                    doc.Sections[0].HeadersFooters.Footer.ChildObjects.Clear();
                }
                catch
                { }
                // 这里使用了Spire Doc免费版,免费版有篇幅限制。在加载或操作Word文档时,要求Word文档不超过500个段落,25个表格。如您有更高的需求,请自行购买、升级使用付费版。
                text = doc.GetText();
                text = text.Replace("#", "").Replace('\r', '#').Replace('\n', '#');
                text = Regex.Replace(text, @"[^\u4e00-\u9fa5\《\》\(\)\——\;\,\。\“\”\!\#]", "");
                text = new Regex("[#]+").Replace(text, "@@").Trim();
                text = TextFormat(text, blockText);
            }
            catch (Exception e)
            {
            }
            finally
            {
                if (doc != null)
                {
                    doc.Close();
                }
            }
            return(text);
        }
        public override string ConvertToString(string path)
        {
            Spire.Doc.Document doc  = null;
            string             text = null;

            try
            {
                doc = new Spire.Doc.Document();
                doc.LoadFromFile(path);
                try
                {
                    doc.Sections[0].HeadersFooters.Header.ChildObjects.Clear();
                    doc.Sections[0].HeadersFooters.Footer.ChildObjects.Clear();
                }
                catch
                { }
                text = doc.GetText();
                text = text.Replace("#", "").Replace('\r', '#').Replace('\n', '#');
                text = Regex.Replace(text, @"[^\u4e00-\u9fa5\《\》\(\)\——\;\,\。\“\”\!\#]", "");
                text = new Regex("[#]+").Replace(text, "@@").Trim();
                text = TextFormat(text);
            }
            catch (Exception e)
            {
            }
            finally
            {
                if (doc != null)
                {
                    doc.Close();
                }
            }
            return(text);
        }
Esempio n. 3
0
File: Utils.cs Progetto: iamHXQ/Oy
        /// <summary>
        /// 查找替换文本
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="strOld">查找文本</param>
        /// <param name="strNew">替换文本</param>
        public void WordReplace(string[] strOld, string[] strNew)
        {
            string[] SampleFilePaths = { @".\Resources\外封面.docx", @".\Resources\报告书.docx", @".\Resources\委托书.docx", @".\Resources\管理表.docx", @".\Resources\流程单.docx" };
            string   ProjectName     = NamedObjectDictionary.ReadFromNOD(lib.AppConfig.ProjectInfoName[1]);
            string   FolderPath      = Interaction.GetFolderPath();

            if (strOld.Length == strNew.Length)
            {
                using (Spire.Doc.Document doc = new Spire.Doc.Document())
                {
                    foreach (string SampleFilePath in SampleFilePaths)
                    {
                        string filename     = System.IO.Path.GetFileNameWithoutExtension(SampleFilePath) + "-" + ProjectName; //获取文件名
                        string extension    = System.IO.Path.GetExtension(SampleFilePath);                                    //获取扩展名
                        string saveFilePath = FolderPath + @"\" + filename + extension;                                       //合并成完整路径

                        doc.LoadFromFile(SampleFilePath);
                        for (int i = 0; i < strOld.Length; i++)
                        {
                            doc.Replace("[<" + strOld[i] + ">]", strNew[i], false, false);
                        }
                        if (saveFilePath != null)
                        {
                            doc.SaveToFile(saveFilePath);
                            doc.Close();
                        }
                    }
                }
            }
            else
            {
                Forms.MessageBox.Show("要替换的新旧字符串数量不同,取消操作");
            }
        }
 /// <summary>
 /// 替换Word
 /// </summary>
 /// <param name="path"></param>
 /// <param name="dics"></param>
 /// <param name="savePath"></param>
 /// <returns></returns>
 public bool WordReplace(string path, string savePath, Dictionary <string, string> dics)
 {
     using (var doc = new Spire.Doc.Document())
     {
         doc.LoadFromFile(path);
         foreach (var item in dics)
         {
             doc.Replace(new Regex(item.Key), item.Value);
         }
         doc.SaveToFile(savePath);
         doc.Close();
     }
     return(true);
 }
Esempio n. 5
0
        /// <summary>
        /// Method that searches all the employees in a file
        /// </summary>
        /// <param name="file">
        /// File to be searched and added to the sql database
        /// </param>
        /// <param name="connectionString">
        /// Azure blob storage connection string
        /// </param>
        /// <param name="containerName">
        /// Azure blob storage container name
        /// </param>
        /// <returns>
        /// Dictionary with the results found
        /// </returns>
        public Dictionary <string, int> SearchEmployees(D.Models.File file, string connectionString, string containerName)
        {
            string filePath  = GeneratePath(file.Name);
            string extension = Path.GetExtension(file.Name).ToLower();
            string text;

            DownloadFile(file.Name, filePath, connectionString, containerName);

            if (extension == ".pdf")
            {
                PdfDocument doc = new PdfDocument();
                doc.LoadFromFile(filePath);
                StringBuilder buffer = new StringBuilder();
                foreach (PdfPageBase page in doc.Pages)
                {
                    buffer.Append(page.ExtractText());
                }

                doc.Close();
                text = buffer.ToString();
                text = text.Replace(Environment.NewLine, " ");
                text = DeleteRepeatedSpaces(text);
                return(SearchEmployeesAux1(file, filePath, text));
            }
            else if (extension == ".docx")
            {
                Spire.Doc.Document doc = new Spire.Doc.Document();
                doc.LoadFromFile(filePath);
                text = doc.GetText();
                doc.Close();
                return(SearchEmployeesAux1(file, filePath, text));
            }
            else
            {
                text = File.ReadAllText(filePath);
                return(SearchEmployeesAux1(file, filePath, text));
            }
        }
Esempio n. 6
0
        /// <summary>
        /// 文件上传
        /// </summary>
        public void UploadFiles()
        {
            bool result = false;

            if (Request.Form["option"].ToString() == "update")
            {
                string fn      = Request.Form["filename"].ToString();
                string keyword = Request.Form["keyword"].ToString();
                string status  = Request.Form["ispublic"].ToString() == "checked" ? "1" : "0";
                string id      = Request.Form["id"].ToString();
                string sql     = string.Format("update fileinfo set filename='{0}',keyword='{1}',status='{2}' where keyid={3}", fn, keyword, status, id);
                int    i       = DAL.Commons.Instance.chooseFactory("sql").ExecuteNonQuery(sql);
                result = i > 0 ? true : false;
            }
            else
            {
                if (Request.Files.Count > 0)
                {
                    loginUser lu  = Session["user"] as loginUser;
                    string    dir = string.Format("{0}//{1}//{2}", "upload", DateTime.Now.ToString("yyyy-MM-dd"), lu.LoginName);

                    string extName = Path.GetExtension(Request.Files[0].FileName);//.Split('.')[1].ToLower();
                    extName = extName.Substring(1);
                    string filename = DAL.Commons.Instance.GetUploadFileName(dir, extName);
                    Request.Files[0].SaveAs(AppDomain.CurrentDomain.BaseDirectory + filename);
                    string fn       = Request.Form["filename"].ToString();
                    string keyword  = Request.Form["keyword"].ToString();
                    string dirid    = Request.Form["dirid"].ToString();
                    string userid   = lu.ID;
                    string status   = Request.Form["ispublic"].ToString() == "checked" ? "1" : "0";
                    string f1       = "doc,docx";
                    string f2       = "xls,xlsx";
                    string f3       = "txt,jpeg,png";
                    string f4       = "pdf";
                    int    viewmode = 3;
                    string viewdir  = "";
                    if (f1.IndexOf(extName) >= 0)
                    {
                        viewmode = 1;
                        Spire.Doc.Document doc = new Spire.Doc.Document();
                        doc.LoadFromFile(AppDomain.CurrentDomain.BaseDirectory + filename);
                        string newname = DAL.Commons.Instance.GetUploadFileName(dir, "pdf");
                        doc.SaveToFile(AppDomain.CurrentDomain.BaseDirectory + newname, Spire.Doc.FileFormat.PDF);
                        viewdir = newname;
                        doc.Close();
                    }
                    else if (f2.IndexOf(extName) >= 0)
                    {
                        viewmode = 1;
                        Spire.Xls.Workbook book = new Spire.Xls.Workbook();
                        book.LoadFromFile(AppDomain.CurrentDomain.BaseDirectory + filename);
                        string newname = DAL.Commons.Instance.GetUploadFileName(dir, "pdf");
                        book.ConverterSetting.SheetFitToPage = true;
                        book.SaveToFile(AppDomain.CurrentDomain.BaseDirectory + newname, Spire.Xls.FileFormat.PDF);
                        viewdir = newname;
                    }
                    else if (f3.IndexOf(extName) >= 0)
                    {
                        viewmode = 2;
                        viewdir  = filename;
                    }
                    else if (f4.IndexOf(extName) >= 0)
                    {
                        viewmode = 1;
                        viewdir  = filename;
                    }
                    Spire.Pdf.PdfDocument pdf = new Spire.Pdf.PdfDocument();


                    string sql = string.Format("insert into fileinfo (filename,keyword,dirid,userid,status,localdir,viewdir,viewmode,extname) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}')", fn, keyword, dirid, userid, status, filename, viewdir, viewmode, extName);
                    int    i   = DAL.Commons.Instance.chooseFactory("sql").ExecuteNonQuery(sql);
                    result = i > 0 ? true : false;
                }
            }
            var par = new { sucess = result };

            Response.Write(DAL.Commons.Instance.ToJson(par));
        }
Esempio n. 7
0
        public static byte[] ConvertToPdf(string fileName)
        {
            string       contentType = MimeMapping.GetMimeMapping(fileName);
            MemoryStream mem         = new MemoryStream();

            switch (contentType.ToLower())
            {
            case "image/gif":
            case "image/jpg":
            case "image/png":
            case "image/jpeg":
            case "application/BMP":
            case "image/bmp":
            case "image/tiff":
                if (!string.IsNullOrEmpty(fileName))
                {
                    Document document = new Document(PageSize.LETTER, 10, 10, 10, 10);
                    //using ( var stream = new MemoryStream() )
                    //{
                    PdfWriter.GetInstance(document, mem);

                    document.Open();
                    using (var imageStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        var image = Image.GetInstance(imageStream);
                        image.ScaleAbsolute(585, 750);
                        //image.ScaleAbsoluteHeight(PageSize.A4.Height - 20);
                        //image.ScaleToFit(PageSize.LETTER);

                        document.Add(image);
                    }
                    document.Close();

                    // return stream.ToArray();
                    //}
                }

                break;

            case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
            case "application/vnd.ms-excel":
            case "application/xlsx":
            case "application/xls":
            case "application/vnd.ms-excel.sheet.macroEnabled.12":
            case "application/XLSM":
                if (!string.IsNullOrEmpty(fileName))
                {
                    Spire.Xls.Workbook workbook = new Spire.Xls.Workbook();
                    workbook.LoadFromFile(fileName, true);
                    workbook.SaveToStream(mem, Spire.Xls.FileFormat.PDF);
                }

                break;

            case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
            case "application/msword":
            case "application/docx":
            case "application/doc":
                if (!string.IsNullOrEmpty(fileName))
                {
                    Spire.Doc.Document document = new Spire.Doc.Document();
                    document.LoadFromFile(fileName, Spire.Doc.FileFormat.Auto);
                    document.SaveToStream(mem, Spire.Doc.FileFormat.PDF);
                    document.Close();
                }

                break;

            case "application/html":
            case "application/txt":
            case "application/htm":
            case "text/html":
            case "application/xml":
            case "text/plain":
                if (!string.IsNullOrEmpty(fileName))
                {
                    Spire.Pdf.PdfDocument pdf = new Spire.Pdf.PdfDocument();
                    PdfHtmlLayoutFormat   htmlLayoutFormat = new PdfHtmlLayoutFormat
                    {
                        Layout          = PdfLayoutType.Paginate,
                        FitToPage       = Clip.Width,
                        LoadHtmlTimeout = 60 * 1000
                    };
                    htmlLayoutFormat.IsWaiting = true;
                    PdfPageSettings setting = new PdfPageSettings();
                    setting.Size = PdfPageSize.A4;
                    Thread thread = new Thread(() => { pdf.LoadFromHTML(fileName, true, true, true); });
                    thread.SetApartmentState(ApartmentState.STA);
                    thread.Start();
                    thread.Join();
                    pdf.SaveToStream(mem, Spire.Pdf.FileFormat.PDF);
                }
                break;

            case "application/msg":
            case "application/octet-stream":
            case "multipart/related":
            case "application/ZIP":
            case "application/VCF":
            default:
                break;
            }

            return(mem.ToArray());
        }
Esempio n. 8
0
        /// <summary>
        /// 对Word文档进行拆分
        /// </summary>
        /// <param name="wordPath">要拆分的Word文档路径</param>
        /// <param name="savePath">拆分后的Word存储路径</param>
        private void SplitWord(string wordPath, string savePath)
        {
            object missing = System.Reflection.Missing.Value; //作为方法传参中的缺省值

            if (!Directory.Exists(savePath))                  //判断Word存储路径是否存在
            {
                Directory.CreateDirectory(savePath);          //如果路径不存在,则创建
            }
            try
            {
                //判断拆分方式:0表示按书签拆分,1表示按分页符拆分,默认为按书签拆分
                switch (flag)
                {
                    #region  书签拆分
                case 0:
                default:
                    Document doc = null;                    //声明一个Word文档对象
                    if (doc == null)                        //判断Word文档对象是否为空
                    {
                        doc = ReadDocument(wordPath);       //调用方法读取指定Word内容,并实例化Word文档对象
                    }
                    int[,] positions = GetPosition(doc);    //获取指定Word中的所有书签的开始和结束位置
                    object oStart = 0;                      //定义变量,表示要拆分的开始位置
                    object oEnd   = 0;                      //定义变量,表示要拆分的结束位置
                    int    row    = positions.GetLength(0); //获取二维数组的行数
                    for (int i = 0; i < row; i++)           //遍历二维数组的所有行
                    {
                        if (i != row - 1)                   //判断是否为最后一行
                        {
                            oEnd = positions[i, 1];         //如果不是最后一行,记录值,作为拆分的结束位置
                        }
                        else
                        {
                            oEnd = doc.Content.End;                     //如果是最后一行,则直接将Word文档的最后位置作为拆分的结束位置
                        }
                        Range tocopy = doc.Range(ref oStart, ref oEnd); //使用开始和结束位置锁定要拆分的范围
                        tocopy.Copy();                                  //复制要拆分范围内容的所有内容
                        Document docto = CreateDocument(textBox1.Text); //调用自定义方法创建一个新的Word文档
                        docto.Content.Paste();                          //将复制的内容粘贴到新创建的Word文档中
                        //设置Word文档的保存路径及文件名(以编号命名)
                        object filename = savePath + "\\" + i.ToString("000") + ".docx";
                        //保存Word文档
                        docto.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing,
                                     ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                                     ref missing, ref missing, ref missing, ref missing, ref missing);
                        docto.Close(ref missing, ref missing, ref missing); //关闭Word文档
                        oStart = oEnd;                                      //将本次拆分的结束位置作为下一次拆分的开始位置
                    }
                    break;
                    #endregion

                    #region  分页符拆分
                case 1:
                    Spire.Doc.Document original = new Spire.Doc.Document();             //使用Spire插件创建Word文档对象
                    original.LoadFromFile(wordPath);                                    //加载要拆分的Word文档
                    Spire.Doc.Document newWord = new Spire.Doc.Document(textBox1.Text); //使用选择的模板创建一个新的Word文档对象
                    Spire.Doc.Section  section = newWord.AddSection();                  //为新创建的Word文档添加一节
                    int index = 0;                                                      //根据分页来拆分文档
                    foreach (Spire.Doc.Section sec in original.Sections)                //遍历文档所有节
                    {
                        //遍历文档所有子对象
                        foreach (Spire.Doc.DocumentObject obj in sec.Body.ChildObjects)
                        {
                            if (obj is Spire.Doc.Documents.Paragraph)    //判断是否为段落
                            {
                                //创建Word文档中的段落对象
                                Spire.Doc.Documents.Paragraph para = obj as Spire.Doc.Documents.Paragraph;
                                //复制并添加原有段落对象到新文档
                                section.Body.ChildObjects.Add(para.Clone());
                                //遍历所有段落子对象
                                foreach (Spire.Doc.DocumentObject parobj in para.ChildObjects)
                                {
                                    //判断是否为分页符
                                    if (parobj is Spire.Doc.Break && (parobj as Spire.Doc.Break).BreakType == Spire.Doc.Documents.BreakType.PageBreak)
                                    {
                                        //获取段落分页并移除,保存新文档到文件夹
                                        int i = para.ChildObjects.IndexOf(parobj);
                                        section.Body.LastParagraph.ChildObjects.RemoveAt(i);
                                        newWord.SaveToFile(savePath + "\\" + index.ToString("000") + ".docx", Spire.Doc.FileFormat.Docx);
                                        index++;
                                        //实例化Document类对象,添加section,将原文档段落的子对象复制到新文档
                                        newWord = new Spire.Doc.Document(textBox1.Text);
                                        section = newWord.AddSection();
                                        section.Body.ChildObjects.Add(para.Clone());
                                        if (section.Paragraphs[0].ChildObjects.Count == 0)
                                        {
                                            section.Body.ChildObjects.RemoveAt(0);    //移除第一个空白段落
                                        }
                                        else
                                        {
                                            //删除分页符前的子对象
                                            while (i >= 0)
                                            {
                                                section.Paragraphs[0].ChildObjects.RemoveAt(i);
                                                i--;
                                            }
                                        }
                                    }
                                }
                            }
                            //若对象为表格,则添加表格对象到新文档
                            if (obj is Spire.Doc.Table)
                            {
                                section.Body.ChildObjects.Add(obj.Clone());
                            }
                        }
                    }
                    //拆分后的新文档保存至指定文档
                    newWord.SaveToFile(savePath + "\\" + index.ToString("00") + ".docx", Spire.Doc.FileFormat.Docx);
                    original.Close();
                    original.Dispose();
                    newWord.Close();
                    newWord.Dispose();
                    foreach (string file in Directory.GetFiles(savePath)) //遍历拆分完的所有Word文档
                    {
                        deletePagesInFile(file, 1, 1);                    //删除第一页
                    }
                    break;
                    #endregion
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }//异常跳过
        }