- 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
| { | { | ||||
| public string Name { get; set; } | public string Name { get; set; } | ||||
| public Paragraph Paragraph { get; set; } | public Paragraph Paragraph { get; set; } | ||||
| public void SetText(string newText) | |||||
| { | |||||
| Paragraph.ReplaceAtBookmark(newText, Name); | |||||
| } | |||||
| } | } | ||||
| } | } |
| 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)); | |||||
| } | |||||
| } | |||||
| } | |||||
| } |
| } | } | ||||
| } | } | ||||
| public BookmarkCollection Bookmarks | |||||
| { | |||||
| get | |||||
| { | |||||
| BookmarkCollection bookmarks = new BookmarkCollection(); | |||||
| foreach (Paragraph paragraph in Paragraphs) | |||||
| bookmarks.AddRange(paragraph.GetBookmarks()); | |||||
| return bookmarks; | |||||
| } | |||||
| } | |||||
| public float MarginTop | public float MarginTop | ||||
| { | { | ||||
| get | get |
| </ItemGroup> | </ItemGroup> | ||||
| <ItemGroup> | <ItemGroup> | ||||
| <Compile Include="Bookmark.cs" /> | <Compile Include="Bookmark.cs" /> | ||||
| <Compile Include="BookmarkCollection.cs" /> | |||||
| <Compile Include="Border.cs" /> | <Compile Include="Border.cs" /> | ||||
| <Compile Include="Charts\Axis.cs" /> | <Compile Include="Charts\Axis.cs" /> | ||||
| <Compile Include="Charts\BarChart.cs" /> | <Compile Include="Charts\BarChart.cs" /> |
| } | } | ||||
| } | } | ||||
| 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) | internal string GetOrGenerateRel(Picture p) | ||||
| { | { | ||||
| string image_uri_string = p.img.pr.TargetUri.OriginalString; | string image_uri_string = p.img.pr.TargetUri.OriginalString; |
| // You can specify all the values or you can default the Build and Revision Numbers | // You can specify all the values or you can default the Build and Revision Numbers | ||||
| // by using the '*' as shown below: | // by using the '*' as shown below: | ||||
| // [assembly: AssemblyVersion("1.0.*")] | // [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")] |
| </ItemGroup> | </ItemGroup> | ||||
| <ItemGroup> | <ItemGroup> | ||||
| <None Include="app.config" /> | <None Include="app.config" /> | ||||
| <None Include="DocumentWithBookmarks.docx"> | |||||
| <CopyToOutputDirectory>Always</CopyToOutputDirectory> | |||||
| </None> | |||||
| <None Include="Input.docx"> | <None Include="Input.docx"> | ||||
| <CopyToOutputDirectory>Always</CopyToOutputDirectory> | <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||||
| </None> | </None> |
| AddList(); | AddList(); | ||||
| Equations(); | Equations(); | ||||
| Bookmarks(); | Bookmarks(); | ||||
| BookmarksReplaceTextOfBookmarkKeepingFormat(); | |||||
| BarChart(); | BarChart(); | ||||
| PieChart(); | PieChart(); | ||||
| LineChart(); | LineChart(); | ||||
| } | } | ||||
| } | } | ||||
| /// <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> | /// <summary> | ||||
| /// Create a document with a Paragraph whos first line is indented. | /// Create a document with a Paragraph whos first line is indented. | ||||
| /// </summary> | /// </summary> |