Added basic support for PageNumber and PageCount place holders.
When inserted into a Header or Footer Word will automatically display the correct value foreach page.
Word will not automatically update these place holders if inserted inside a document level Paragraph.
You need to right click and select update field. I therefore suggest that you only use these place holders inside Headers and Footers.
Added
-------
Paragraph.AppendPageNumber(PageNumberFormat)
Paragraph.InsertPageNumber(PageNumberFormat, int index)
Paragraph.AppendPageCount(PageNumberFormat)
Paragraph.InsertPageCount(PageNumberFormat, int index)
Example
-----------
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Add Headers to the document.
document.AddHeaders();
// Get the default Header.
Header header = document.Headers.odd;
// Insert a Paragraph into the Header.
Paragraph p0 = header.InsertParagraph();
// Append place holders for PageNumber and PageCount into the Header.
// Word will replace these with the correct value foreach Page.
p0.Append("Page (");
p0.AppendPageNumber(PageNumberFormat.normal);
p0.Append(" of ");
p0.AppendPageCount(PageNumberFormat.normal);
p0.Append(")");
// Save the document.
document.Save();
}
AddProtection() now calls RemoveProtection() as its first task. Otherwise a document could contain many protection elements and this is unallowed.
Below is a code example which uses the new Protection familiy of functions.
// Load a document.
using (DocX document = DocX.Load(@"Test.docx"))
{
if (document.isProtected)
{
Console.WriteLine("Document is protected.");
Console.WriteLine("\tProtection type is " + document.GetProtectionType().ToString());
document.RemoveProtection();
Console.WriteLine("\tProtected removed.");
// Save the document.
document.Save();
Console.WriteLine("\tDocument saved as Test.docx");
}
else
{
Console.WriteLine("Document is not protected.");
document.AddProtection(EditRestrictions.readOnly);
Console.WriteLine("\tProtected added.");
Console.WriteLine("\tProtection type is " + document.GetProtectionType().ToString());
// Save the document.
document.Save();
Console.WriteLine("\tDocument saved as Test.docx");
}
}
// Wait until a user presses a key before exiting.
Console.ReadKey();
After it is executed, RemoveText() deletes elements that have zero text length. This was removing elements which contained no text but still contained a drawing element such as a picture.
This fix checks that an element contains no drawing element and that its text length is zero before removing the element.
This projects contains many of the examples upload on my blog.
Each time docx is released, I will update these examples so as they still work regardless of API changes.
Added unit tests.
Depreciated overloads which confussed API.
Its now possible to create one Hyperlink and insert it into the main document, headers and footers.
Added unit tests.
Removed overloads which confussed API.
Its now possible to create one Picture and insert it into the main document, headers and footers.
2) Gave UnitTests internal access to DocX.dll
DocX AssemblyInfo.cs
[assembly: InternalsVisibleTo("UnitTests")]
3) Updated target framework to .NET 4.0.
4) Fixed bug with InsertText() Formatting object was set to null by default, should have been new Formatting()
Added support for document orientation (portrait or landscape), below is a code example of how to use this new feature.
// Create a new document.
using (DocX document = DocX.Create(@"C:\Users\Cathal\Desktop\Hello.docx"))
{
// Set this documents orientation to landscape and save.
document.PageLayout.Orientation = Orientation.Landscape;
document.SaveAs(@"Landscape.docx");
// Set this documents orientation to portrait and save.
document.PageLayout.Orientation = Orientation.Portrait;
document.SaveAs(@"Portrait.docx");
}
Save() now extracts the first <w:sectPr> tag instead of looking for one at the 'correct' document position.
Apparently <w:sectPr> tags can even be nested inside <w:Prp> tags and word will still open them.
Thanks to srwright for bringing this bug to my attention.
Rewrote the core behind (Insert and Remove)Text() functions. Most if not all issues with XElement splitting have been fixed by this changeset.
Major new functions are listed below.
Paragraph
-----------
GetFirstRunEffectedByInsert()
GetFirstRunEffectedByInsertRecursive()
Run
----
GetFirstTextEffectedByInsert()
GetFirstTextEffectedByInsertRecursive()
HelperFunctions
-----------------
GetText
GetTextRecursive
GetSize(XElement Xml)
This new recursive search method does not return the same Paragraph multiple times thus the problem with Textbox's has also been fixed.
Fixed a bug in Paragraph.AppendHyperlink(h) that caused any custom formatting to be applied to the runs before the Hyperlink instead of the Hyperlink itself. The below is now possible
Paragraph p = document.InsertParagraph();
p.Append("Check out this ").Color(Color.Red).AppendHyperlink(h).Color(Color.Green).Append(", it's really cool").Color(Color.Blue);
Removed Rebuild[Images, Pictures, Tables, CustomProperties], this work is now done by getter functions, this is a much cleaner approach.
Added Getter and Setter functions for Hyperlink. Also added Hyperlink properties for Document, Paragraph, Table, Row and Cell.
Added similar functions for Pictures.
----------
Created an abstract class called DocXElement and made every document element such as {Table, Row, Cell, Paragraph, Run, Text, etc} extend it.
Created an abstract class InsertBeforeOrAfter which is derived from DocXElement. This provides functions for inserting, page breaks, Paragraphs and Tables before and after self.
Moved Text and Run inside Paragraph, keep consistancy with Table which contains Row and Cell.
To Do: Investigated why the Picture class had two internal constructors. I removed one.
Added a _ to each .cs file that does not contain instanceable types, {Enumerations.cs, Extensions.cs, etc}
Bugs fixed
----------
Cell had a parameter Paragraph, this was incorrect as paragraphs can contain multiple Paragraphs. Now it returns a List<Paragraph>
InsertDocProperty now returns a DocProperty instead of void.
New features
------------
InsertDocProperty now contains overloads for the parameter track changes.
Cell now contains a Shading property, this can be used to set the background color of a cell.
Cathal: Added overloads to ReplaceText for Format checking and replacement.
Joel: Added Row.MergeCells
Joel: Added Row.Height property
Joel: Added Cell.Width property
A huge thanks to Joel: The first user to submit working & tested functions\properties for inclusion in DocX.