Parcourir la source

Few bookmarking additions by Umberto

- 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
MadBoy_cp il y a 10 ans
Parent
révision
eea1d311af

+ 5
- 0
DocX/Bookmark.cs Voir le fichier

@@ -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);
}
}
}

+ 17
- 0
DocX/BookmarkCollection.cs Voir le fichier

@@ -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));
}
}
}
}

+ 11
- 0
DocX/DocX.cs Voir le fichier

@@ -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

+ 1
- 0
DocX/DocX.csproj Voir le fichier

@@ -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" />

+ 42
- 0
DocX/Paragraph.cs Voir le fichier

@@ -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;

+ 2
- 2
DocX/Properties/AssemblyInfo.cs Voir le fichier

@@ -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")]

BIN
Examples/DocumentWithBookmarks.docx Voir le fichier


+ 3
- 0
Examples/Examples.csproj Voir le fichier

@@ -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>

+ 24
- 0
Examples/Program.cs Voir le fichier

@@ -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>

Chargement…
Annuler
Enregistrer