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"); Console.WriteLine("Processing document: " + fileName + extensionName); 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); // Save each page to the disk as a separate document. for (int page = 1; page <= doc.PageCount; page++) { Document pageDoc = splitter.GetDocumentOfPage(page); pageDoc.Save(Path.Combine(outFolder, string.Format("{0} - page{1} Out{2}", fileName, page, extensionName))); } // Detach the collector from the document. layoutCollector.Document = null; }
public static void Run() { // The path to the documents directory. string dataDir = RunExamples.GetDataDir_WorkingWithDocument(); Document doc = new Document(dataDir + "TestFile.docx"); // Create and attach collector before the document before page layout is built. LayoutCollector layoutCollector = new LayoutCollector(doc); // This will build layout model and collect necessary information. doc.UpdatePageLayout(); // Print the details of each document node including the page numbers. foreach (Node node in doc.FirstSection.Body.GetChildNodes(NodeType.Any, true)) { Console.WriteLine(" --------- "); Console.WriteLine("NodeType: " + Node.NodeTypeToString(node.NodeType)); Console.WriteLine("Text: \"" + node.ToString(SaveFormat.Text).Trim() + "\""); Console.WriteLine("Page Start: " + layoutCollector.GetStartPageIndex(node)); Console.WriteLine("Page End: " + layoutCollector.GetEndPageIndex(node)); Console.WriteLine(" --------- "); Console.WriteLine(); } // Detatch the collector from the document. layoutCollector.Document = null; Console.WriteLine("\nFound the page numbers of all nodes successfully."); }
public static void Main() { string dataDir = Path.GetFullPath("../../../Data/"); Document doc = new Document(dataDir + "TestFile.docx"); // Create and attach collector before the document before page layout is built. LayoutCollector layoutCollector = new LayoutCollector(doc); // This will build layout model and collect necessary information. doc.UpdatePageLayout(); // Print the details of each document node including the page numbers. foreach (Node node in doc.FirstSection.Body.GetChildNodes(NodeType.Any, true)) { Console.WriteLine(" --------- "); Console.WriteLine("NodeType: " + Node.NodeTypeToString(node.NodeType)); Console.WriteLine("Text: \"" + node.ToString(SaveFormat.Text).Trim() + "\""); Console.WriteLine("Page Start: " + layoutCollector.GetStartPageIndex(node)); Console.WriteLine("Page End: " + layoutCollector.GetEndPageIndex(node)); Console.WriteLine(" --------- "); Console.WriteLine(); } // Detatch the collector from the document. layoutCollector.Document = null; Console.ReadLine(); }
public static void Main() { // This a document that we want to add an image and custom text for each page without using the header or footer. Document doc = new Document(gDataDir + "TestFile.doc"); // Create and attach collector before the document before page layout is built. LayoutCollector layoutCollector = new LayoutCollector(doc); // Images in a document are added to paragraphs, so to add an image to every page we need to find at any paragraph // belonging to each page. IEnumerator enumerator = doc.SelectNodes("//Body/Paragraph").GetEnumerator(); // Loop through each document page. for (int page = 1; page <= doc.PageCount; page++) { while (enumerator.MoveNext()) { // Check if the current paragraph belongs to the target page. Paragraph paragraph = (Paragraph)enumerator.Current; if (layoutCollector.GetStartPageIndex(paragraph) == page) { AddImageToPage(paragraph, page); break; } } } doc.Save(gDataDir + "TestFile Out.docx"); }
public void AddImageToEachPage() { Document doc = new Document(MyDir + "Document.docx"); // Create and attach collector before the document before page layout is built. LayoutCollector layoutCollector = new LayoutCollector(doc); // Images in a document are added to paragraphs to add an image to every page we need // to find at any paragraph belonging to each page. IEnumerator enumerator = doc.SelectNodes("// Body/Paragraph").GetEnumerator(); for (int page = 1; page <= doc.PageCount; page++) { while (enumerator.MoveNext()) { // Check if the current paragraph belongs to the target page. Paragraph paragraph = (Paragraph)enumerator.Current; if (layoutCollector.GetStartPageIndex(paragraph) == page) { AddImageToPage(paragraph, page, ImagesDir); break; } } } // If we need to save the document as a PDF or image, call UpdatePageLayout() method. doc.UpdatePageLayout(); doc.Save(ArtifactsDir + "WorkingWithImages.AddImageToEachPage.docx"); }
static void Main(string[] args) { ApplyLicense(); Document doc = new Document("eSignature.Test.02.docx"); //Find the text between <<>> and insert bookmark doc.Range.Replace(new Regex(@"\<<.*?\>>"), "", new FindReplaceOptions() { ReplacingCallback = new FindAndInsertBookmark() }); LayoutCollector layoutCollector = new LayoutCollector(doc); LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc); //Display the left top position of text between angle bracket. foreach (Bookmark bookmark in doc.Range.Bookmarks) { if (bookmark.Name.StartsWith("bookmark_")) { layoutEnumerator.Current = layoutCollector.GetEntity(bookmark.BookmarkStart); Console.WriteLine(" --> Left : " + layoutEnumerator.Rectangle.Left + " Top : " + layoutEnumerator.Rectangle.Top); } } doc.Save("20.10.docx"); System.Diagnostics.Process.Start("20.10.docx"); }
private List <NavigationPaneItem> PrepareNavigationPaneList(Document doc, LayoutCollector lc) { try { var lst = new List <NavigationPaneItem>(); foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true)) { switch (para.ParagraphFormat.StyleIdentifier) { case StyleIdentifier.Subtitle: case StyleIdentifier.Title: case StyleIdentifier.Heading3: case StyleIdentifier.Heading2: case StyleIdentifier.Heading1: var text = para.Range.Text; if (text.Count(char.IsLetterOrDigit) > 0) { lst.Add(new NavigationPaneItem(text, para.ParagraphFormat.StyleIdentifier, lc.GetStartPageIndex(para))); } break; } } return(lst); } catch (Exception ex) { Console.WriteLine(ex.Message); return(null); } }
public static void Run() { // The path to the documents directory. string dataDir = RunExamples.GetDataDir_WorkingWithImages(); // This a document that we want to add an image and custom text for each page without using the header or footer. Document doc = new Document(dataDir + "TestFile.doc"); // Create and attach collector before the document before page layout is built. LayoutCollector layoutCollector = new LayoutCollector(doc); // Images in a document are added to paragraphs, so to add an image to every page we need to find at any paragraph // belonging to each page. IEnumerator enumerator = doc.SelectNodes("//Body/Paragraph").GetEnumerator(); // Loop through each document page. for (int page = 1; page <= doc.PageCount; page++) { while (enumerator.MoveNext()) { // Check if the current paragraph belongs to the target page. Paragraph paragraph = (Paragraph)enumerator.Current; if (layoutCollector.GetStartPageIndex(paragraph) == page) { AddImageToPage(paragraph, page, dataDir); break; } } } doc.Save(dataDir + "TestFile Out.docx"); Console.WriteLine("\nInserted images on each page of the document successfully.\nFile saved at " + dataDir + "TestFile Out.docx"); }
/// <summary> /// Creates a new instance from the supplied Aspose.Words.Document class. /// </summary> /// <param name="document">A document whose page layout model to enumerate.</param> /// <remarks><para>If page layout model of the document hasn't been built the enumerator calls <see cref="Document.UpdatePageLayout"/> to build it.</para> /// <para>Whenever document is updated and new page layout model is created, a new RenderedDocument instance must be used to access the changes.</para></remarks> public RenderedDocument(Document doc) { mLayoutCollector = new LayoutCollector(doc); mEnumerator = new LayoutEnumerator(doc); ProcessLayoutElements(this); LinkLayoutMarkersToNodes(doc); CollectLinesAndAddToMarkers(); }
/// <summary> /// Creates a new instance from the supplied Document class. /// </summary> /// <param name="document">A document whose page layout model to enumerate.</param> /// <remarks><para>If page layout model of the document hasn't been built the enumerator calls <see cref="Document.UpdatePageLayout"/> to build it.</para> /// <para>Whenever document is updated and new page layout model is created, a new RenderedDocument instance must be used to access the changes.</para></remarks> public RenderedDocument(Document doc) { mLayoutCollector = new LayoutCollector(doc); mEnumerator = new LayoutEnumerator(doc); ProcessLayoutElements(this); LinkLayoutMarkersToNodes(doc); CollectLinesAndAddToMarkers(); }
public static PageNumberFinder Create(Document document) { LayoutCollector layoutCollector = new LayoutCollector(document); document.UpdatePageLayout(); PageNumberFinder pageNumberFinder = new PageNumberFinder(layoutCollector); pageNumberFinder.SplitNodesAcrossPages(); return(pageNumberFinder); }
/// <summary> /// Change hyperlinks to _page{pageNumber}_{...} /// </summary> /// <param name="doc"></param> private void PrepareInternalLinks(Document doc, LayoutCollector lc) { foreach (var field in doc.Range.Fields) { try { if (field.Type == FieldType.FieldHyperlink) { var link = (FieldHyperlink)field; if (!string.IsNullOrEmpty(link.SubAddress)) { var bookmark = doc.Range.Bookmarks[link.SubAddress]; if (bookmark == null) { continue; } var pageNumber = lc.GetStartPageIndex(bookmark.BookmarkStart); if (pageNumber == 0) { continue; } var name = $"_page{pageNumber}{link.SubAddress}"; bookmark.Name = name; link.SubAddress = name; } } else if (field.Type == FieldType.FieldRef) { var link = (FieldRef)field; var bookmark = doc.Range.Bookmarks[link.BookmarkName]; if (bookmark == null) { continue; } var pageNumber = lc.GetStartPageIndex(bookmark.BookmarkStart); if (pageNumber == 0) { continue; } var name = $"_page{pageNumber}{link.BookmarkName}"; bookmark.Name = name; link.BookmarkName = name; } } catch (Exception ex) { Console.WriteLine(ex.Message); } } var filename = Config.Configuration.OutputDirectory + Opts.FolderName + "/" + Path.GetFileNameWithoutExtension(Opts.FileName) + "_links.docx"; doc.Save(filename); }
void IImageSavingCallback.ImageSaving(ImageSavingArgs args) { args.KeepImageStreamOpen = false; Assert.True(args.IsImageAvailable); Console.WriteLine($"{args.Document.OriginalFileName.Split('\\').Last()} Image #{++mImageCount}"); LayoutCollector layoutCollector = new LayoutCollector(args.Document); Console.WriteLine($"\tOn page:\t{layoutCollector.GetStartPageIndex(args.CurrentShape)}"); Console.WriteLine($"\tDimensions:\t{args.CurrentShape.Bounds.ToString()}"); Console.WriteLine($"\tAlignment:\t{args.CurrentShape.VerticalAlignment}"); Console.WriteLine($"\tWrap type:\t{args.CurrentShape.WrapType}"); Console.WriteLine($"Output filename:\t{args.ImageFileName}\n"); }
public HttpResponseMessage Search(RequestData request) { Opts.AppName = "Viewer"; Opts.FileName = request.fileName; Opts.FolderName = request.folderName; Opts.MethodName = "Search"; try { if (Opts.FolderName.Contains("..")) { throw new Exception("Break-in attempt"); } if (string.IsNullOrEmpty(request.searchQuery)) { return(Request.CreateResponse(HttpStatusCode.OK, new int[] { })); } var doc = new Document(Opts.WorkingFileName); var lst = new HashSet <int>(); var findings = new AsposeWordsSearch.FindCallback(); var options = new FindReplaceOptions() { ReplacingCallback = findings, Direction = FindReplaceDirection.Forward, MatchCase = false }; doc.Range.Replace(new Regex(request.searchQuery, RegexOptions.IgnoreCase), "", options); var lc = new LayoutCollector(doc); foreach (var mathchedNode in findings.MatchedNodes) { foreach (var node in mathchedNode.Value.Select(x => x.MatchNode)) { var pageNumber = lc.GetStartPageIndex(node); lst.Add(pageNumber); } } return(Request.CreateResponse(HttpStatusCode.OK, lst)); } catch (Exception ex) { return(ExceptionResponse(ex)); } }
public static ArrayList GetNodesByPage(int page, Aspose.Words.Document document) { ArrayList nodes = new ArrayList(); LayoutCollector lc = new LayoutCollector(document); foreach (Paragraph para in document.GetChildNodes(NodeType.Paragraph, true)) { if (lc.GetStartPageIndex(para) == page) { nodes.Add(para); } } return(nodes); }
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; }
public void PageBreakBefore(bool pageBreakBefore) { //ExStart //ExFor:ParagraphFormat.PageBreakBefore //ExSummary:Shows how to create paragraphs with page breaks at the beginning. Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Set this flag to "true" to apply a page break to each paragraph's beginning // that the document builder will create under this ParagraphFormat configuration. // The first paragraph will not receive a page break. // Leave this flag as "false" to start each new paragraph on the same page // as the previous, provided there is sufficient space. builder.ParagraphFormat.PageBreakBefore = pageBreakBefore; builder.Writeln("Paragraph 1."); builder.Writeln("Paragraph 2."); LayoutCollector layoutCollector = new LayoutCollector(doc); ParagraphCollection paragraphs = doc.FirstSection.Body.Paragraphs; if (pageBreakBefore) { Assert.AreEqual(1, layoutCollector.GetStartPageIndex(paragraphs[0])); Assert.AreEqual(2, layoutCollector.GetStartPageIndex(paragraphs[1])); } else { Assert.AreEqual(1, layoutCollector.GetStartPageIndex(paragraphs[0])); Assert.AreEqual(1, layoutCollector.GetStartPageIndex(paragraphs[1])); } doc.Save(ArtifactsDir + "ParagraphFormat.PageBreakBefore.docx"); //ExEnd doc = new Document(ArtifactsDir + "ParagraphFormat.PageBreakBefore.docx"); paragraphs = doc.FirstSection.Body.Paragraphs; Assert.AreEqual(pageBreakBefore, paragraphs[0].ParagraphFormat.PageBreakBefore); Assert.AreEqual(pageBreakBefore, paragraphs[1].ParagraphFormat.PageBreakBefore); }
public HttpResponseMessage DocumentInfo(RequestData request) { Opts.AppName = "Viewer"; Opts.FileName = request.fileName; Opts.FolderName = request.folderName; Opts.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name; try { if (Opts.FolderName.Contains("..")) { throw new Exception("Break-in attempt"); } var doc = new Document(Opts.WorkingFileName); var lc = new LayoutCollector(doc); PrepareInternalLinks(doc, lc); var lst = new List <PageView>(doc.PageCount); for (int i = 0; i < doc.PageCount; i++) { var pageInfo = doc.GetPageInfo(i); var size = pageInfo.GetSizeInPixels(1, 72); lst.Add(new PageView() { width = size.Width, height = size.Height, angle = 0, number = i + 1 }); } return(Request.CreateResponse(HttpStatusCode.OK, new PageParametersResponse(request.fileName, lst, PrepareNavigationPaneList(doc, lc)))); } catch (Exception ex) { return(ExceptionResponse(ex)); } }
/// <summary> /// Initializes a new instance of the <see cref="PageNumberFinder"/> class. /// </summary> /// <param name="collector">A collector instance that has layout model records for the document.</param> public PageNumberFinder(LayoutCollector collector) { this.collector = collector; }
/// <summary> /// Initializes new instance of this class. This method splits the document into sections so that each page /// begins and ends at a section boundary. It is recommended not to modify the document afterwards. /// </summary> /// <param name="collector">A collector instance which has layout model records for the document.</param> public DocumentPageSplitter(LayoutCollector collector) { mPageNumberFinder = new PageNumberFinder(collector); mPageNumberFinder.SplitNodesAcrossPages(); }
public void LayoutCollector() { //ExStart //ExFor:Layout.LayoutCollector //ExFor:Layout.LayoutCollector.#ctor(Document) //ExFor:Layout.LayoutCollector.Clear //ExFor:Layout.LayoutCollector.Document //ExFor:Layout.LayoutCollector.GetEndPageIndex(Node) //ExFor:Layout.LayoutCollector.GetEntity(Node) //ExFor:Layout.LayoutCollector.GetNumPagesSpanned(Node) //ExFor:Layout.LayoutCollector.GetStartPageIndex(Node) //ExFor:Layout.LayoutEnumerator.Current //ExSummary:Shows how to see the page spans of nodes. // Open a blank document and create a DocumentBuilder Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Create a LayoutCollector object for our document that will have information about the nodes we placed LayoutCollector layoutCollector = new LayoutCollector(doc); // The document itself is a node that contains everything, which currently spans 0 pages Assert.AreEqual(doc, layoutCollector.Document); Assert.AreEqual(0, layoutCollector.GetNumPagesSpanned(doc)); // Populate the document with sections and page breaks builder.Write("Section 1"); builder.InsertBreak(BreakType.PageBreak); builder.InsertBreak(BreakType.PageBreak); doc.AppendChild(new Section(doc)); doc.LastSection.AppendChild(new Body(doc)); builder.MoveToDocumentEnd(); builder.Write("Section 2"); builder.InsertBreak(BreakType.PageBreak); builder.InsertBreak(BreakType.PageBreak); // The collected layout data won't automatically keep up with the real document contents Assert.AreEqual(0, layoutCollector.GetNumPagesSpanned(doc)); // After we clear the layout collection and update it, the layout entity collection will be populated with up-to-date information about our nodes // The page span for the document now shows 5, which is what we would expect after placing 4 page breaks layoutCollector.Clear(); doc.UpdatePageLayout(); Assert.AreEqual(5, layoutCollector.GetNumPagesSpanned(doc)); // We can also see the start/end pages of any other node, and their overall page spans NodeCollection nodes = doc.GetChildNodes(NodeType.Any, true); foreach (Node node in nodes) { Console.WriteLine($"-> NodeType.{node.NodeType}: "); Console.WriteLine( $"\tStarts on page {layoutCollector.GetStartPageIndex(node)}, ends on page {layoutCollector.GetEndPageIndex(node)}," + $" spanning {layoutCollector.GetNumPagesSpanned(node)} pages."); } // We can iterate over the layout entities using a LayoutEnumerator LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc); Assert.AreEqual(LayoutEntityType.Page, layoutEnumerator.Type); // The LayoutEnumerator can traverse the collection of layout entities like a tree // We can also point it to any node's corresponding layout entity like this layoutEnumerator.Current = layoutCollector.GetEntity(doc.GetChild(NodeType.Paragraph, 1, true)); Assert.AreEqual(LayoutEntityType.Span, layoutEnumerator.Type); Assert.AreEqual("¶", layoutEnumerator.Text); //ExEnd }
public void LayoutCollector() { //ExStart //ExFor:Layout.LayoutCollector //ExFor:Layout.LayoutCollector.#ctor(Document) //ExFor:Layout.LayoutCollector.Clear //ExFor:Layout.LayoutCollector.Document //ExFor:Layout.LayoutCollector.GetEndPageIndex(Node) //ExFor:Layout.LayoutCollector.GetEntity(Node) //ExFor:Layout.LayoutCollector.GetNumPagesSpanned(Node) //ExFor:Layout.LayoutCollector.GetStartPageIndex(Node) //ExFor:Layout.LayoutEnumerator.Current //ExSummary:Shows how to see the the ranges of pages that a node spans. Document doc = new Document(); LayoutCollector layoutCollector = new LayoutCollector(doc); // Call the "GetNumPagesSpanned" method to count how many pages the content of our document spans. // Since the document is empty, that number of pages is currently zero. Assert.AreEqual(doc, layoutCollector.Document); Assert.AreEqual(0, layoutCollector.GetNumPagesSpanned(doc)); // Populate the document with 5 pages of content. DocumentBuilder builder = new DocumentBuilder(doc); builder.Write("Section 1"); builder.InsertBreak(BreakType.PageBreak); builder.InsertBreak(BreakType.PageBreak); builder.InsertBreak(BreakType.SectionBreakEvenPage); builder.Write("Section 2"); builder.InsertBreak(BreakType.PageBreak); builder.InsertBreak(BreakType.PageBreak); // Before the layout collector, we need to call the "UpdatePageLayout" method to give us // an accurate figure for any layout-related metric, such as the page count. Assert.AreEqual(0, layoutCollector.GetNumPagesSpanned(doc)); layoutCollector.Clear(); doc.UpdatePageLayout(); Assert.AreEqual(5, layoutCollector.GetNumPagesSpanned(doc)); // We can see the numbers of the start and end pages of any node and their overall page spans. NodeCollection nodes = doc.GetChildNodes(NodeType.Any, true); foreach (Node node in nodes) { Console.WriteLine($"-> NodeType.{node.NodeType}: "); Console.WriteLine( $"\tStarts on page {layoutCollector.GetStartPageIndex(node)}, ends on page {layoutCollector.GetEndPageIndex(node)}," + $" spanning {layoutCollector.GetNumPagesSpanned(node)} pages."); } // We can iterate over the layout entities using a LayoutEnumerator. LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc); Assert.AreEqual(LayoutEntityType.Page, layoutEnumerator.Type); // The LayoutEnumerator can traverse the collection of layout entities like a tree. // We can also apply it to any node's corresponding layout entity. layoutEnumerator.Current = layoutCollector.GetEntity(doc.GetChild(NodeType.Paragraph, 1, true)); Assert.AreEqual(LayoutEntityType.Span, layoutEnumerator.Type); Assert.AreEqual("¶", layoutEnumerator.Text); //ExEnd }
/// <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); }
/// <summary> /// Initializes new instance of this class. /// </summary> /// <param name="collector">A collector instance which has layout model records for the document.</param> public PageNumberFinder(LayoutCollector collector) { mCollector = collector; }
private static DocCompareModel Compare(Document original, Document current) { var docCompareModel = new DocCompareModel(); var pages = new List <DocumentPage>(); original.Compare(current, "Apose", DateTime.Now); LayoutCollector layout = new LayoutCollector(original); foreach (Revision revision in original.Revisions) { if (revision.RevisionType == RevisionType.Insertion) { var index = layout.GetStartPageIndex(revision.ParentNode); if (index == 0) { continue; } if (pages.Where(i => i.PageNumber == index).ToList().Any()) { var page = pages.Find(i => i.PageNumber == index); page.Added++; } else { pages.Add(new DocumentPage { PageNumber = index, Added = 1 }); } continue; } if (revision.RevisionType == RevisionType.Deletion) { var index = layout.GetStartPageIndex(revision.ParentNode); if (index == 0) { continue; } if (pages.Where(i => i.PageNumber == index).ToList().Any()) { var page = pages.Find(i => i.PageNumber == index); page.Deleted++; } else { pages.Add(new DocumentPage { PageNumber = index, Deleted = 1 }); } } } docCompareModel.Pages = pages; using (var stream = new MemoryStream()) { original.Save(stream, SaveFormat.Docx); var doc = new Document(stream); ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Jpeg) { PageCount = 1 }; var urls = new Dictionary <int, string>(); docCompareModel.PageCount = doc.PageCount; for (int i = 0; i < doc.PageCount; i++) { options.PageIndex = i; using (var imgStream = new MemoryStream()) { doc.Save(imgStream, options); var base64Image = new Base64Image { FileContents = imgStream.ToArray(), ContentType = "image/png" }; urls.Add(i, base64Image.ToString()); } } docCompareModel.PageImages = urls; } return(docCompareModel); }
private void FillUpWithEmptyLines() { try { int NoOfSections = doc.Sections.Count; int currentSection = 0; foreach (Section section in doc.Sections) { DocumentBuilder builder = new DocumentBuilder(doc); Node startNode = GenerateStartNode(section); Node node; int NoOfParagraphs = section.Body.ChildNodes.Count; int currentParagraph = 0; for (node = startNode; node != null; node = node.NextSibling) { if (node.NodeType == NodeType.Paragraph) { builder.MoveTo(node); Paragraph para = (Paragraph)node; // string test = para.GetText(); Aspose.Words.Font font; ParagraphFormat paragraphFormat; InitFormats(para, ref builder, out font, out paragraphFormat); Paragraph paraToInsert; int paraRuns; bool isEndOfFile; if (para.GetText().IndexOf(ControlChar.PageBreakChar) >= 0) { LayoutCollector layoutCollector = new LayoutCollector(doc); doc.UpdatePageLayout(); int origPage = layoutCollector.GetEndPageIndex(node); int newpage = origPage; while (newpage == origPage) { paraToInsert = (Paragraph)node; isEndOfFile = paraToInsert == section.Body.LastParagraph; paraRuns = paraToInsert.Runs.Count; if (paraRuns != 0 && !isEndOfFile) { builder.MoveTo(paraToInsert.Runs[paraRuns - 1]); } else { builder.MoveTo(paraToInsert); } builder.Writeln(ControlChar.Tab); if (builder.ListFormat.IsListItem) { builder.ListFormat.RemoveNumbers(); } layoutCollector = new LayoutCollector(doc); doc.UpdatePageLayout(); node = node.NextSibling; newpage = layoutCollector.GetEndPageIndex(node); } Node nodeToRemove = node.PreviousSibling; Paragraph paraToRemove = (Paragraph)nodeToRemove; string TextToRemove = paraToRemove.GetText(); if (TextToRemove == "\t\r") { paraToInsert = (Paragraph)node; nodeToRemove.Remove(); isEndOfFile = paraToInsert == section.Body.LastParagraph; paraRuns = paraToInsert.Runs.Count; if (paraRuns != 0 && !isEndOfFile) { builder.MoveTo(paraToInsert.Runs[paraRuns - 1]); } else if (paraRuns != 0 && isEndOfFile) { builder.MoveTo(paraToInsert.Runs[paraRuns]); } else { builder.MoveTo(paraToInsert); } builder.Write(ControlChar.Tab); } else { node = node.PreviousSibling; node.NextSibling.Remove(); } } else { builder.MoveTo(node); builder.Write(ControlChar.Tab); } } else if (node.NodeType == NodeType.Table) { Table table = (Table)node; LayoutCollector layoutCollector = new LayoutCollector(doc); foreach (Row row in table.Rows) { foreach (Cell cell in row.Cells) { Node tblStartNode = cell.FirstChild; for (Node tblNode = tblStartNode; tblNode != null; tblNode = tblNode.NextSibling) { builder.MoveTo(tblNode); if (tblNode.NodeType == NodeType.Paragraph) { Paragraph tblPara = (Paragraph)tblNode; Aspose.Words.Font font; ParagraphFormat paragraphFormat; InitFormats(tblPara, ref builder, out font, out paragraphFormat); builder.MoveTo(tblNode); builder.Write(ControlChar.Tab); } } } } } currentParagraph++; double dblKeszultseg = ((70.0 / Convert.ToDouble(NoOfSections)) / Convert.ToDouble(NoOfParagraphs)) * Convert.ToDouble(currentParagraph); Program.mainWindow.updateProgress(30 + Convert.ToInt32(Math.Round(dblKeszultseg))); } currentSection++; } } catch (Exception ex) { Log.AddLog("Adding empty lines error.\r\n" + ex.Message, true); } }