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(); }
/// <summary> /// Retrieves 1-based index of a page that the node ends on. /// </summary> /// <param name="node"> /// The node. /// </param> /// <returns> /// Page index. /// </returns> public int GetPageEnd(Node node) { return(nodeEndPageLookup.ContainsKey(node) ? nodeEndPageLookup[node] : collector.GetEndPageIndex(node)); }
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 }
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); } }
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 }