Hi again, sorry it has been so long since my last post. My progress in the physical world has slowed my progress in the digital.
This newest version of DocX brings many bug fixes and also two new features. Firstly, I would like to thank the following people for taking the time to email me with bugs and feature requests. Without your interest and feedback, DocX would not progress.
Many thanks to: John Marsh, Francesco, Johan Laagland, Gaspar, Brian (chicken), Jianbang, Christopher W. Steelman, Peter Thompson, Brian Minert.
New features
1) InsertDocProperty(CustomProperty cp)
CustomProperties are displayed in a document using fields of type DocProperty. Until this version, DocX was capable of adding a CustomProperty to a document, but it was not capable of displaying these CustomProperties. As of DocX version 1.0.0.6, this is now possible, below is an example.
// Load a document
using (DocX document = DocX.Create(@"Test.docx"))
{
// Create a custom property.
CustomProperty name = new CustomProperty("name", "Cathal Coffey");
// Add this custom property to this document.
document.AddCustomProperty(name);
// Insert a new paragraph.
Paragraph p = document.InsertParagraph("Author: ", false);
// Insert a field of type document property to display the custom property name
p.InsertDocProperty(name);
// Save all changes made to this document.
document.Save();
}// Release this document from memory.
The above code example will create the following document. Please note that the text 'Cathal Coffey'
is being displayed by a field, this field's value is controlled by the CustomProperty 'name'.
2) FindAll(string str)
This version of DocX allows you to search a document for a string. The function
FindAll(string str) returns a list containing all of the start indexes of the found string.
Below is an example of this new function.
// Load a documentIf the above code is run on this document (Drawing in red is to illustrate the index's of each instance of 'go').
using (DocX document = DocX.Load(@"Test.docx"))
{
// Loop through the paragraphs in this document.
foreach (Paragraph p in document.Paragraphs)
{
// Find all instances of 'go' in this paragraph.
List<int> gos = document.FindAll("go");
/*
* Insert 'don't' in front of every instance of 'go' in this document to produce * 'don't go'. An important trick here is to do the inserting in reverse document * order. If you inserted in document order, every insert would shift the index * of the remaining matches.
*/
gos.Reverse();
foreach (int index in gos)
{
p.InsertText(index, "don't ", true);
}
}
// Save all changes made to this document.
document.Save();
}// Release this document from memory.
Then the following document is produced.
As you can see, 'don't' was inserted at the original index of each instance of 'go'.
DocX is completely free, but if you have found it useful and you would like to make a donation you can do so via paypal.
happy coding,
Cathal
Hi, is it possible to calculate also the page number on which the string was found?
ReplyDeleteThank you for bringing more information to this topic for me. I’m truly grateful and really impressed.
ReplyDeletexml data conversion