예제 #1
0
        public static void SplitDocumentToPages(string docName)
        {
            string folderName    = Path.GetDirectoryName(docName);
            string fileName      = Path.GetFileNameWithoutExtension(docName);
            string extensionName = Path.GetExtension(docName);
            string outFolder     = Path.Combine(folderName, "_out_");

            Document doc = new Document(docName);

            // Create and attach collector to the document before page layout is built.
            LayoutCollector layoutCollector = new LayoutCollector(doc);

            // This will build layout model and collect necessary information.
            doc.UpdatePageLayout();

            // Split nodes in the document into separate pages.
            DocumentPageSplitter splitter = new DocumentPageSplitter(layoutCollector);
            HtmlSaveOptions      option   = new HtmlSaveOptions();

            option.ExportHeadersFootersMode = ExportHeadersFootersMode.PerSection;
            option.PrettyFormat             = true;
            option.UseAntiAliasing          = true;
            // Save each page to the disk as a separate document.
            for (int page = 1; page <= doc.PageCount; page++)
            {
                Document       pageDoc = splitter.GetDocumentOfPage(page);
                NodeCollection paras   = pageDoc.GetChildNodes(NodeType.Paragraph, true);
                foreach (Paragraph para in paras)
                {
                    if (para.GetText().Contains("Evaluation Only. Created with Aspose.Words. Copyright 2003-2016 Aspose Pty Ltd."))
                    {
                        paras.Remove(para);
                        pageDoc.UpdatePageLayout();
                    }
                }
                var name = Path.Combine(outFolder, string.Format("{0}{1}{2}", fileName, page, ".html"));
                pageDoc.Save(name, option);
            }

            // Detach the collector from the document.
            layoutCollector.Document = null;
        }
예제 #2
0
        /// <summary>
        /// word生成html字符串
        /// </summary>
        /// <param name="path">word路径</param>
        /// <returns></returns>
        public override string ToHTML(string path)
        {
            //openxml引用
            //string htmlPath=WmlToHtmlConverterHelper.ConvertToHtml(path, this.FilePath);
            //aspose引用 api网址http://www.aspose.com
            if (!File.Exists(path))
            {
                return(string.Empty);
            }
            //PageNumberFinder
            Document doc       = new Document(path);
            var      paperSize = doc.FirstSection.PageSetup.PaperSize.ToString(); //纸张大小
            var      pageCount = doc.PageCount;                                   //获得页数

            doc.MailMerge.UseNonMergeFields         = false;
            doc.MailMerge.UseWholeParagraphAsRegion = true;
            //doc.MailMergeSettings.ViewMergedData = false;
            //doc.MailMergeSettings.LinkToQuery = true;
            //doc.MailMergeSettings.MailAsAttachment = true;
            //html设置
            ws.HtmlSaveOptions option = new ws.HtmlSaveOptions(SaveFormat.Html);
            option.ExportHeadersFootersMode   = ws.ExportHeadersFootersMode.PerSection;
            option.PrettyFormat               = true;
            option.UseAntiAliasing            = true;
            option.ExportTocPageNumbers       = false;
            option.ExportRoundtripInformation = true;
            //option.ExportListLabels = ExportListLabels.AsInlineText;
            string name = Path.GetFileNameWithoutExtension(path);

            //生成图片保存路径
            option.ImagesFolderAlias = MapPathReverse(path);
            //Path.Combine(Path.GetDirectoryName(path), name);
            //string.Format("/Resource/emw/UserFile/{0}/", name);
            var savePath = Path.Combine(this.FilePath, name + "\\");

            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(savePath);
            }
            LayoutCollector layoutCollector = new LayoutCollector(doc);

            // 构建布局模型,收集必要的信息。
            doc.UpdatePageLayout();

            // 节点在文档分割成单独的页面。
            DocumentPageSplitter splitter = new DocumentPageSplitter(layoutCollector);

            //for (int page = 1; page <= doc.PageCount; page++)
            //{
            //    Document pageDoc = splitter.GetDocumentOfPage(page);
            //    NodeCollection paras = pageDoc.GetChildNodes(NodeType.Paragraph, true);
            //    foreach (Paragraph para in paras)
            //    {
            //        if (para.GetText().Contains("Evaluation Only. Created with Aspose.Words. Copyright 2003-2016 Aspose Pty Ltd."))
            //        {
            //            paras.Remove(para);
            //        }
            //    }
            //    var pageFileName = Path.Combine(savePath, string.Format("{0}-page{1}{2}", name, page, ".html"));
            //    pageDoc.Save(pageFileName,option);
            //}
            // 从文档中分离收集器。
            layoutCollector.Document = null;
            //html保存地址
            string htmlPath = savePath + name + ".html";

            doc.Save(htmlPath, option);
            XElement xhtml     = XElement.Load(htmlPath);
            var      idCounter = 1000000;

            foreach (var d in xhtml.Descendants())
            {
                if (d.Name.LocalName == "head" || d.Name.LocalName == "meta" || d.Name.LocalName == "title" || d.Name.LocalName == "body")
                {
                    continue;
                }
                string     idName = d.Name.LocalName + "-" + idCounter.ToString().Substring(1);
                XAttribute id     = new XAttribute("data-uniqid", idName);
                if (d.Attribute("id") == null)
                {
                    d.Add(id);
                }
                idCounter++;
            }
            File.WriteAllText(htmlPath, xhtml.ToString(), Encoding.UTF8);
            string htmlString = xhtml.ToString();// File.ReadAllText(htmlPath);
            string jsonStr    = JsonConvert.SerializeObject(new { pageCount = pageCount, paperSize = paperSize, htmlString = htmlString, path = path });

            return(jsonStr);
        }