Explorar el Código

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 hace 10 años
padre
commit
eea1d311af

+ 5
- 0
DocX/Bookmark.cs Ver fichero

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

+ 17
- 0
DocX/BookmarkCollection.cs Ver fichero

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 Ver fichero

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

+ 1
- 0
DocX/DocX.csproj Ver fichero

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

+ 42
- 0
DocX/Paragraph.cs Ver fichero

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

+ 2
- 2
DocX/Properties/AssemblyInfo.cs Ver fichero

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

BIN
Examples/DocumentWithBookmarks.docx Ver fichero


+ 3
- 0
Examples/Examples.csproj Ver fichero

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

+ 24
- 0
Examples/Program.cs Ver fichero

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>

Cargando…
Cancelar
Guardar