- added a whole bookmarks collection so one can access it from DocX and get the bookmark by name
- added a SetText method to the Bookmark so one can set the text from it using something like this: docX.Bookmarks["MyBookmark"].SetText("MyText")
- Added example BookmarksReplaceTextOfBookmarkKeepingFormat
master
| @@ -8,5 +8,10 @@ namespace Novacode | |||
| { | |||
| public string Name { get; set; } | |||
| public Paragraph Paragraph { get; set; } | |||
| public void SetText(string newText) | |||
| { | |||
| Paragraph.ReplaceAtBookmark(newText, Name); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,17 @@ | |||
| using System; | |||
| using System.Collections.Generic; | |||
| using System.Linq; | |||
| namespace Novacode | |||
| { | |||
| public class BookmarkCollection : List<Bookmark> | |||
| { | |||
| public Bookmark this[string name] | |||
| { | |||
| get | |||
| { | |||
| return this.FirstOrDefault(bookmark => string.Equals(bookmark.Name, name, StringComparison.CurrentCultureIgnoreCase)); | |||
| } | |||
| } | |||
| } | |||
| } | |||
| @@ -75,6 +75,17 @@ namespace Novacode | |||
| } | |||
| } | |||
| public BookmarkCollection Bookmarks | |||
| { | |||
| get | |||
| { | |||
| BookmarkCollection bookmarks = new BookmarkCollection(); | |||
| foreach (Paragraph paragraph in Paragraphs) | |||
| bookmarks.AddRange(paragraph.GetBookmarks()); | |||
| return bookmarks; | |||
| } | |||
| } | |||
| public float MarginTop | |||
| { | |||
| get | |||
| @@ -88,6 +88,7 @@ | |||
| </ItemGroup> | |||
| <ItemGroup> | |||
| <Compile Include="Bookmark.cs" /> | |||
| <Compile Include="BookmarkCollection.cs" /> | |||
| <Compile Include="Border.cs" /> | |||
| <Compile Include="Charts\Axis.cs" /> | |||
| <Compile Include="Charts\BarChart.cs" /> | |||
| @@ -2388,6 +2388,48 @@ namespace Novacode | |||
| } | |||
| } | |||
| public void ReplaceAtBookmark(string toInsert, string bookmarkName) | |||
| { | |||
| XElement bookmark = Xml.Descendants(XName.Get("bookmarkStart", DocX.w.NamespaceName)) | |||
| .Where(x => x.Attribute(XName.Get("name", DocX.w.NamespaceName)).Value == bookmarkName) | |||
| .SingleOrDefault(); | |||
| if (bookmark == null) | |||
| return; | |||
| XNode nextNode = bookmark.NextNode; | |||
| XElement nextElement = nextNode as XElement; | |||
| while (nextElement == null || nextElement.Name.NamespaceName != DocX.w.NamespaceName || (nextElement.Name.LocalName != "r" && nextElement.Name.LocalName != "bookmarkEnd")) | |||
| { | |||
| nextNode = nextNode.NextNode; | |||
| nextElement = nextNode as XElement; | |||
| } | |||
| // Check if next element is a bookmarkEnd | |||
| if (nextElement.Name.LocalName == "bookmarkEnd") | |||
| { | |||
| ReplaceAtBookmark_Add(toInsert, bookmark); | |||
| return; | |||
| } | |||
| XElement contentElement = nextElement.Elements(XName.Get("t", DocX.w.NamespaceName)).FirstOrDefault(); | |||
| if (contentElement == null) | |||
| { | |||
| ReplaceAtBookmark_Add(toInsert, bookmark); | |||
| return; | |||
| } | |||
| contentElement.Value = toInsert; | |||
| } | |||
| private void ReplaceAtBookmark_Add(string toInsert, XElement bookmark) | |||
| { | |||
| var run = HelperFunctions.FormatInput(toInsert, null); | |||
| bookmark.AddAfterSelf(run); | |||
| runs = Xml.Elements(XName.Get("r", DocX.w.NamespaceName)).ToList(); | |||
| HelperFunctions.RenumberIDs(Document); | |||
| } | |||
| internal string GetOrGenerateRel(Picture p) | |||
| { | |||
| string image_uri_string = p.img.pr.TargetUri.OriginalString; | |||
| @@ -35,5 +35,5 @@ using System.Runtime.InteropServices; | |||
| // You can specify all the values or you can default the Build and Revision Numbers | |||
| // by using the '*' as shown below: | |||
| // [assembly: AssemblyVersion("1.0.*")] | |||
| [assembly: AssemblyVersion("1.0.0.19")] | |||
| [assembly: AssemblyFileVersion("1.0.0.19")] | |||
| [assembly: AssemblyVersion("1.0.0.20")] | |||
| [assembly: AssemblyFileVersion("1.0.0.20")] | |||
| @@ -67,6 +67,9 @@ | |||
| </ItemGroup> | |||
| <ItemGroup> | |||
| <None Include="app.config" /> | |||
| <None Include="DocumentWithBookmarks.docx"> | |||
| <CopyToOutputDirectory>Always</CopyToOutputDirectory> | |||
| </None> | |||
| <None Include="Input.docx"> | |||
| <CopyToOutputDirectory>Always</CopyToOutputDirectory> | |||
| </None> | |||
| @@ -30,6 +30,7 @@ namespace Examples | |||
| AddList(); | |||
| Equations(); | |||
| Bookmarks(); | |||
| BookmarksReplaceTextOfBookmarkKeepingFormat(); | |||
| BarChart(); | |||
| PieChart(); | |||
| LineChart(); | |||
| @@ -272,6 +273,29 @@ namespace Examples | |||
| } | |||
| } | |||
| /// <summary> | |||
| /// Loads a document 'DocumentWithBookmarks.docx' and changes text inside bookmark keeping formatting the same. | |||
| /// This code creates the file 'BookmarksReplaceTextOfBookmarkKeepingFormat.docx'. | |||
| /// </summary> | |||
| private static void BookmarksReplaceTextOfBookmarkKeepingFormat() | |||
| { | |||
| Console.WriteLine("\tBookmarksReplaceTextOfBookmarkKeepingFormat()"); | |||
| using (DocX docX = DocX.Load("DocumentWithBookmarks.docx")) | |||
| { | |||
| foreach (Bookmark bookmark in docX.Bookmarks) | |||
| Console.WriteLine("\t\tFound bookmark {0}", bookmark.Name); | |||
| // Replace bookmars content | |||
| docX.Bookmarks["bmkNoContent"].SetText("Here there was a bookmark"); | |||
| docX.Bookmarks["bmkContent"].SetText("Here there was a bookmark with a previous content"); | |||
| docX.Bookmarks["bmkFormattedContent"].SetText("Here there was a formatted bookmark"); | |||
| docX.SaveAs(@"docs\BookmarksReplaceTextOfBookmarkKeepingFormat.docx"); | |||
| } | |||
| Console.WriteLine("\tCreated: docs\\BookmarksReplaceTextOfBookmarkKeepingFormat.docx"); | |||
| } | |||
| /// <summary> | |||
| /// Create a document with a Paragraph whos first line is indented. | |||
| /// </summary> | |||