/// <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); }
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 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"); }
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 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() { // 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 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"); }
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); }
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 Run() { // The path to the documents directory. string dataDir = RunExamples.GetDataDir_WorkingWithImages(); string fileName = "TestFile.doc"; // 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 + fileName); // 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; } } } // Call UpdatePageLayout() method if file is to be saved as PDF or image format doc.UpdatePageLayout(); dataDir = dataDir + RunExamples.GetOutputFilePath(fileName); doc.Save(dataDir); Console.WriteLine("\nInserted images on each page of the document successfully.\nFile saved at " + dataDir); }
/// <summary> /// Retrieves 1-based index of a page that the node begins on. /// </summary> /// <param name="node"> /// The node. /// </param> /// <returns> /// Page index. /// </returns> public int GetPage(Node node) { return(nodeStartPageLookup.ContainsKey(node) ? nodeStartPageLookup[node] : collector.GetStartPageIndex(node)); }
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 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 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 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); }
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 }