coffeycathal_cp 16 роки тому
джерело
коміт
278d8161c5
7 змінених файлів з 2106 додано та 207 видалено
  1. 735
    119
      DocX/DocX.cs
  2. 7
    3
      DocX/DocX.csproj
  3. 7
    1
      DocX/Formatting.cs
  4. 708
    62
      DocX/Paragraph.cs
  5. 22
    13
      DocX/Picture.cs
  6. 2
    2
      DocX/Properties/AssemblyInfo.cs
  7. 625
    7
      DocX/Table.cs

+ 735
- 119
DocX/DocX.cs
Різницю між файлами не показано, бо вона завелика
Переглянути файл


+ 7
- 3
DocX/DocX.csproj Переглянути файл

@@ -18,6 +18,8 @@
<SccProvider>SAK</SccProvider>
<StartupObject>
</StartupObject>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>StrongNameKey.pfx</AssemblyOriginatorKeyFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@@ -58,6 +60,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CustomProperty.cs" />
<Compile Include="DocProperty.cs" />
<Compile Include="Table.cs" />
<Compile Include="Enumerations.cs" />
<Compile Include="Extensions.cs" />
@@ -71,12 +74,13 @@
<Compile Include="Text.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Help\DocX v1.0.0.5 - Documentation.chm" />
<None Include="Help\Documentation - DocX v 1.0.0.7.chm" />
<None Include="StrongNameKey.pfx" />
<EmbeddedResource Include="Resources\default_styles.xml.gz" />
<EmbeddedResource Include="Resources\styles.xml.gz" />
</ItemGroup>
<ItemGroup>
<Content Include="License\License.html" />
<Content Include="Resources\styles.xml" />
<EmbeddedResource Include="styles.xml" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

+ 7
- 1
DocX/Formatting.cs Переглянути файл

@@ -49,6 +49,8 @@ namespace Novacode
{
get
{
rPr = new XElement(XName.Get("rPr", DocX.w.NamespaceName));
if(spacing.HasValue)
rPr.Add(new XElement(XName.Get("spacing", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), spacing.Value * 20)));
@@ -325,7 +327,11 @@ namespace Novacode
/// <summary>
/// The font familt of this formatting.
/// </summary>
public FontFamily FontFamily { get { return FontFamily; } set { fontFamily = value; } }
/// <!--
/// Bug found and fixed by krugs525 on August 12 2009.
/// Use TFS compare to see exact code change.
/// -->
public FontFamily FontFamily { get { return fontFamily; } set { fontFamily = value; } }
}
}

+ 708
- 62
DocX/Paragraph.cs Переглянути файл

@@ -6,6 +6,8 @@ using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.Security.Principal;
using System.Collections;
using System.IO.Packaging;
using System.IO;
namespace Novacode
{
@@ -13,7 +15,7 @@ namespace Novacode
/// Represents a document paragraph.
/// </summary>
public class Paragraph
{
{
// This paragraphs text alignment
private Alignment alignment;
@@ -32,9 +34,22 @@ namespace Novacode
/// </summary>
public List<Picture> Pictures { get { return pictures; } }
DocX document;
internal Paragraph(DocX document, int startIndex, XElement p)
// A collection of field type DocProperty.
private List<DocProperty> docProperties;
internal List<XElement> styles = new List<XElement>();
/// <summary>
/// Returns a list of field type DocProperty in this document.
/// </summary>
public List<DocProperty> DocumentProperties
{
get { return docProperties; }
}
internal DocX document;
internal Paragraph(DocX document, int startIndex, XElement p)
{
this.document = document;
this.startIndex = startIndex;
this.endIndex = startIndex + GetElementTextLength(p);
@@ -45,6 +60,501 @@ namespace Novacode
// Get all of the images in this document
pictures = (from i in p.Descendants(XName.Get("drawing", DocX.w.NamespaceName))
select new Picture(i)).ToList();
RebuildDocProperties();
#region It's possible that a Paragraph may have pStyle references
// Check if this Paragraph references any pStyle elements.
var stylesElements = xml.Descendants(XName.Get("pStyle", DocX.w.NamespaceName));
// If one or more pStyles are referenced.
if (stylesElements.Count() > 0)
{
Uri style_package_uri = new Uri("/word/styles.xml", UriKind.Relative);
PackagePart styles_document = document.package.GetPart(style_package_uri);
using (TextReader tr = new StreamReader(styles_document.GetStream()))
{
XDocument style_document = XDocument.Load(tr);
XElement styles_element = style_document.Element(XName.Get("styles", DocX.w.NamespaceName));
var styles_element_ids = stylesElements.Select(e => e.Attribute(XName.Get("val", DocX.w.NamespaceName)).Value);
foreach(string id in styles_element_ids)
{
var style =
(
from d in styles_element.Descendants()
let styleId = d.Attribute(XName.Get("styleId", DocX.w.NamespaceName))
where styleId != null && styleId.Value == id
select d
).Single();
styles.Add(style);
}
}
}
#endregion
#region Pictures
// Check if this Paragraph contains any Pictures
List<string> pictureElementIDs =
(
from d in xml.Descendants()
let embed = d.Attribute(XName.Get("embed", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"))
where embed != null
select embed.Value
).ToList();
#endregion
}
/// <summary>
/// Insert a new Table before this Paragraph, this Table can be from this document or another document.
/// </summary>
/// <param name="t">The Table t to be inserted.</param>
/// <returns>A new Table inserted before this Paragraph.</returns>
/// <example>
/// Insert a new Table before this Paragraph.
/// <code>
/// // Place holder for a Table.
/// Table t;
///
/// // Load document a.
/// using (DocX documentA = DocX.Load(@"a.docx"))
/// {
/// // Get the first Table from this document.
/// t = documentA.Tables[0];
/// }
///
/// // Load document b.
/// using (DocX documentB = DocX.Load(@"b.docx"))
/// {
/// // Get the first Paragraph in document b.
/// Paragraph p2 = documentB.Paragraphs[0];
///
/// // Insert the Table from document a before this Paragraph.
/// Table newTable = p2.InsertTableBeforeSelf(t);
///
/// // Save all changes made to document b.
/// documentB.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
public Table InsertTableBeforeSelf(Table t)
{
xml.AddBeforeSelf(t.xml);
XElement newlyInserted = xml.ElementsBeforeSelf().First();
t.xml = newlyInserted;
DocX.RebuildTables(document);
DocX.RebuildParagraphs(document);
return t;
}
/// <summary>
/// Insert a new Table into this document before this Paragraph.
/// </summary>
/// <param name="rowCount">The number of rows this Table should have.</param>
/// <param name="coloumnCount">The number of coloumns this Table should have.</param>
/// <returns>A new Table inserted before this Paragraph.</returns>
/// <example>
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create(@"Test.docx"))
/// {
/// //Insert a Paragraph into this document.
/// Paragraph p = document.InsertParagraph("Hello World", false);
///
/// // Insert a new Table before this Paragraph.
/// Table newTable = p.InsertTableBeforeSelf(2, 2);
/// newTable.Design = TableDesign.LightShadingAccent2;
/// newTable.Alignment = Alignment.center;
///
/// // Save all changes made to this document.
/// document.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
public Table InsertTableBeforeSelf(int rowCount, int coloumnCount)
{
XElement newTable = DocX.CreateTable(rowCount, coloumnCount);
xml.AddBeforeSelf(newTable);
XElement newlyInserted = xml.ElementsBeforeSelf().First();
DocX.RebuildTables(document);
DocX.RebuildParagraphs(document);
return new Table(document, newlyInserted);
}
/// <summary>
/// Insert a new Table after this Paragraph.
/// </summary>
/// <param name="t">The Table t to be inserted.</param>
/// <returns>A new Table inserted after this Paragraph.</returns>
/// <example>
/// Insert a new Table after this Paragraph.
/// <code>
/// // Place holder for a Table.
/// Table t;
///
/// // Load document a.
/// using (DocX documentA = DocX.Load(@"a.docx"))
/// {
/// // Get the first Table from this document.
/// t = documentA.Tables[0];
/// }
///
/// // Load document b.
/// using (DocX documentB = DocX.Load(@"b.docx"))
/// {
/// // Get the first Paragraph in document b.
/// Paragraph p2 = documentB.Paragraphs[0];
///
/// // Insert the Table from document a after this Paragraph.
/// Table newTable = p2.InsertTableAfterSelf(t);
///
/// // Save all changes made to document b.
/// documentB.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
public Table InsertTableAfterSelf(Table t)
{
xml.AddAfterSelf(t.xml);
XElement newlyInserted = xml.ElementsAfterSelf().First();
t.xml = newlyInserted;
DocX.RebuildTables(document);
DocX.RebuildParagraphs(document);
return t;
}
/// <summary>
/// Insert a new Table into this document after this Paragraph.
/// </summary>
/// <param name="rowCount">The number of rows this Table should have.</param>
/// <param name="coloumnCount">The number of coloumns this Table should have.</param>
/// <returns>A new Table inserted after this Paragraph.</returns>
/// <example>
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create(@"Test.docx"))
/// {
/// //Insert a Paragraph into this document.
/// Paragraph p = document.InsertParagraph("Hello World", false);
///
/// // Insert a new Table after this Paragraph.
/// Table newTable = p.InsertTableAfterSelf(2, 2);
/// newTable.Design = TableDesign.LightShadingAccent2;
/// newTable.Alignment = Alignment.center;
///
/// // Save all changes made to this document.
/// document.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
public Table InsertTableAfterSelf(int rowCount, int coloumnCount)
{
XElement newTable = DocX.CreateTable(rowCount, coloumnCount);
xml.AddAfterSelf(newTable);
XElement newlyInserted = xml.ElementsAfterSelf().First();
DocX.RebuildTables(document);
DocX.RebuildParagraphs(document);
return new Table(document, newlyInserted);
}
/// <summary>
/// Insert a Paragraph before this Paragraph, this Paragraph may have come from the same or another document.
/// </summary>
/// <param name="p">The Paragraph to insert.</param>
/// <returns>The Paragraph now associated with this document.</returns>
/// <example>
/// Take a Paragraph from document a, and insert it into document b before this Paragraph.
/// <code>
/// // Place holder for a Paragraph.
/// Paragraph p;
///
/// // Load document a.
/// using (DocX documentA = DocX.Load(@"a.docx"))
/// {
/// // Get the first paragraph from this document.
/// p = documentA.Paragraphs[0];
/// }
///
/// // Load document b.
/// using (DocX documentB = DocX.Load(@"b.docx"))
/// {
/// // Get the first Paragraph in document b.
/// Paragraph p2 = documentB.Paragraphs[0];
///
/// // Insert the Paragraph from document a before this Paragraph.
/// Paragraph newParagraph = p2.InsertParagraphBeforeSelf(p);
///
/// // Save all changes made to document b.
/// documentB.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
public Paragraph InsertParagraphBeforeSelf(Paragraph p)
{
xml.AddBeforeSelf(p.xml);
XElement newlyInserted = xml.ElementsBeforeSelf().First();
p.xml = newlyInserted;
DocX.RebuildParagraphs(document);
return p;
}
/// <summary>
/// Insert a new Paragraph before this Paragraph.
/// </summary>
/// <param name="text">The initial text for this new Paragraph.</param>
/// <returns>A new Paragraph inserted before this Paragraph.</returns>
/// <example>
/// Insert a new paragraph before the first Paragraph in this document.
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create(@"Test.docx"))
/// {
/// // Insert a Paragraph into this document.
/// Paragraph p = document.InsertParagraph("I am a Paragraph", false);
///
/// p.InsertParagraphBeforeSelf("I was inserted before the next Paragraph.");
///
/// // Save all changes made to this new document.
/// document.Save();
/// }// Release this new document form memory.
/// </code>
/// </example>
public Paragraph InsertParagraphBeforeSelf(string text)
{
return InsertParagraphBeforeSelf(text, false, new Formatting());
}
/// <summary>
/// Insert a new Paragraph before this Paragraph.
/// </summary>
/// <param name="text">The initial text for this new Paragraph.</param>
/// <param name="trackChanges">Should this insertion be tracked as a change?</param>
/// <returns>A new Paragraph inserted before this Paragraph.</returns>
/// <example>
/// Insert a new paragraph before the first Paragraph in this document.
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create(@"Test.docx"))
/// {
/// // Insert a Paragraph into this document.
/// Paragraph p = document.InsertParagraph("I am a Paragraph", false);
///
/// p.InsertParagraphBeforeSelf("I was inserted before the next Paragraph.", false);
///
/// // Save all changes made to this new document.
/// document.Save();
/// }// Release this new document form memory.
/// </code>
/// </example>
public Paragraph InsertParagraphBeforeSelf(string text, bool trackChanges)
{
return InsertParagraphBeforeSelf(text, trackChanges, new Formatting());
}
/// <summary>
/// Insert a new Paragraph before this Paragraph.
/// </summary>
/// <param name="text">The initial text for this new Paragraph.</param>
/// <param name="trackChanges">Should this insertion be tracked as a change?</param>
/// <param name="formatting">The formatting to apply to this insertion.</param>
/// <returns>A new Paragraph inserted before this Paragraph.</returns>
/// <example>
/// Insert a new paragraph before the first Paragraph in this document.
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create(@"Test.docx"))
/// {
/// // Insert a Paragraph into this document.
/// Paragraph p = document.InsertParagraph("I am a Paragraph", false);
///
/// Formatting boldFormatting = new Formatting();
/// boldFormatting.Bold = true;
///
/// p.InsertParagraphBeforeSelf("I was inserted before the next Paragraph.", false, boldFormatting);
///
/// // Save all changes made to this new document.
/// document.Save();
/// }// Release this new document form memory.
/// </code>
/// </example>
public Paragraph InsertParagraphBeforeSelf(string text, bool trackChanges, Formatting formatting)
{
XElement newParagraph = new XElement
(
XName.Get("p", DocX.w.NamespaceName), new XElement(XName.Get("pPr", DocX.w.NamespaceName)), DocX.FormatInput(text, formatting.Xml)
);
if (trackChanges)
newParagraph = CreateEdit(EditType.ins, DateTime.Now, newParagraph);
xml.AddBeforeSelf(newParagraph);
XElement newlyInserted = xml.ElementsBeforeSelf().First();
Paragraph p = new Paragraph(document, -1, newlyInserted);
DocX.RebuildParagraphs(document);
return p;
}
/// <summary>
/// Insert a Paragraph after this Paragraph, this Paragraph may have come from the same or another document.
/// </summary>
/// <param name="p">The Paragraph to insert.</param>
/// <returns>The Paragraph now associated with this document.</returns>
/// <example>
/// Take a Paragraph from document a, and insert it into document b after this Paragraph.
/// <code>
/// // Place holder for a Paragraph.
/// Paragraph p;
///
/// // Load document a.
/// using (DocX documentA = DocX.Load(@"a.docx"))
/// {
/// // Get the first paragraph from this document.
/// p = documentA.Paragraphs[0];
/// }
///
/// // Load document b.
/// using (DocX documentB = DocX.Load(@"b.docx"))
/// {
/// // Get the first Paragraph in document b.
/// Paragraph p2 = documentB.Paragraphs[0];
///
/// // Insert the Paragraph from document a after this Paragraph.
/// Paragraph newParagraph = p2.InsertParagraphAfterSelf(p);
///
/// // Save all changes made to document b.
/// documentB.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
public Paragraph InsertParagraphAfterSelf(Paragraph p)
{
xml.AddAfterSelf(p.xml);
XElement newlyInserted = xml.ElementsAfterSelf().First();
p.xml = newlyInserted;
DocX.RebuildParagraphs(document);
return p;
}
/// <summary>
/// Insert a new Paragraph after this Paragraph.
/// </summary>
/// <param name="text">The initial text for this new Paragraph.</param>
/// <param name="trackChanges">Should this insertion be tracked as a change?</param>
/// <param name="formatting">The formatting to apply to this insertion.</param>
/// <returns>A new Paragraph inserted after this Paragraph.</returns>
/// <example>
/// Insert a new paragraph after the first Paragraph in this document.
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create(@"Test.docx"))
/// {
/// // Insert a Paragraph into this document.
/// Paragraph p = document.InsertParagraph("I am a Paragraph", false);
///
/// Formatting boldFormatting = new Formatting();
/// boldFormatting.Bold = true;
///
/// p.InsertParagraphAfterSelf("I was inserted after the previous Paragraph.", false, boldFormatting);
///
/// // Save all changes made to this new document.
/// document.Save();
/// }// Release this new document form memory.
/// </code>
/// </example>
public Paragraph InsertParagraphAfterSelf(string text, bool trackChanges, Formatting formatting)
{
XElement newParagraph = new XElement
(
XName.Get("p", DocX.w.NamespaceName), new XElement(XName.Get("pPr", DocX.w.NamespaceName)), DocX.FormatInput(text, formatting.Xml)
);
if (trackChanges)
newParagraph = CreateEdit(EditType.ins, DateTime.Now, newParagraph);
xml.AddAfterSelf(newParagraph);
XElement newlyInserted = xml.ElementsAfterSelf().First();
Paragraph p = new Paragraph(document, -1, newlyInserted);
DocX.RebuildParagraphs(document);
return p;
}
/// <summary>
/// Insert a new Paragraph after this Paragraph.
/// </summary>
/// <param name="text">The initial text for this new Paragraph.</param>
/// <param name="trackChanges">Should this insertion be tracked as a change?</param>
/// <returns>A new Paragraph inserted after this Paragraph.</returns>
/// <example>
/// Insert a new paragraph after the first Paragraph in this document.
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create(@"Test.docx"))
/// {
/// // Insert a Paragraph into this document.
/// Paragraph p = document.InsertParagraph("I am a Paragraph", false);
///
/// p.InsertParagraphAfterSelf("I was inserted after the previous Paragraph.", false);
///
/// // Save all changes made to this new document.
/// document.Save();
/// }// Release this new document form memory.
/// </code>
/// </example>
public Paragraph InsertParagraphAfterSelf(string text, bool trackChanges)
{
return InsertParagraphAfterSelf(text, trackChanges, new Formatting());
}
/// <summary>
/// Insert a new Paragraph after this Paragraph.
/// </summary>
/// <param name="text">The initial text for this new Paragraph.</param>
/// <returns>A new Paragraph inserted after this Paragraph.</returns>
/// <example>
/// Insert a new paragraph after the first Paragraph in this document.
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create(@"Test.docx"))
/// {
/// // Insert a Paragraph into this document.
/// Paragraph p = document.InsertParagraph("I am a Paragraph", false);
///
/// p.InsertParagraphAfterSelf("I was inserted after the previous Paragraph.");
///
/// // Save all changes made to this new document.
/// document.Save();
/// }// Release this new document form memory.
/// </code>
/// </example>
public Paragraph InsertParagraphAfterSelf(string text)
{
return InsertParagraphAfterSelf(text, false, new Formatting());
}
private void RebuildDocProperties()
{
docProperties =
(
from dp in xml.Descendants(XName.Get("fldSimple", DocX.w.NamespaceName))
select new DocProperty(dp)
).ToList();
}
/// <summary>
@@ -161,7 +671,7 @@ namespace Novacode
DocX.RebuildParagraphs(document);
}
private void BuildRunLookup(XElement p)
internal void BuildRunLookup(XElement p)
{
// Get the runs in this paragraph
IEnumerable<XElement> runs = p.Descendants(XName.Get("r", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"));
@@ -271,12 +781,16 @@ namespace Novacode
public Picture InsertPicture(string imageID, string name, string description)
{
Picture p = new Picture(document, imageID, name, description);
xml.Add(p.i);
xml.Add(p.xml);
pictures.Add(p);
return p;
}
public Picture InsertPicture(string imageID)
{
return InsertPicture(imageID, string.Empty, string.Empty);
}
//public Picture InsertPicture(int index, Picture picture)
//{
// Picture p = picture;
@@ -354,7 +868,7 @@ namespace Novacode
Run run = GetFirstRunEffectedByEdit(index);
if (run == null)
xml.Add(picture.i);
xml.Add(picture.xml);
else
{
// Split this run at the point you want to insert
@@ -364,7 +878,7 @@ namespace Novacode
run.xml.ReplaceWith
(
splitRun[0],
picture.i,
picture.xml,
splitRun[1]
);
}
@@ -376,6 +890,11 @@ namespace Novacode
return picture;
}
public Picture InsertPicture(int index, string imageID)
{
return InsertPicture(index, imageID, string.Empty, string.Empty);
}
/// <summary>
/// Creates an Edit either a ins or a del with the specified content and date
/// </summary>
@@ -383,7 +902,7 @@ namespace Novacode
/// <param name="edit_time">The time stamp to use for this edit</param>
/// <param name="content">The initial content of this edit</param>
/// <returns></returns>
private XElement CreateEdit(EditType t, DateTime edit_time, object content)
internal static XElement CreateEdit(EditType t, DateTime edit_time, object content)
{
if (t == EditType.del)
{
@@ -442,55 +961,14 @@ namespace Novacode
throw new ArgumentOutOfRangeException();
}
private List<XElement> FormatInput(string text, XElement rPr)
{
// Need to support /n as non breaking space
List<XElement> newRuns = new List<XElement>();
XElement tabRun = new XElement(DocX.w + "tab");
string[] runTexts = text.Split('\t');
XElement firstRun;
if (runTexts[0] != String.Empty)
{
XElement firstText = new XElement(DocX.w + "t", runTexts[0]);
Novacode.Text.PreserveSpace(firstText);
firstRun = new XElement(DocX.w + "r", rPr, firstText);
newRuns.Add(firstRun);
}
if (runTexts.Length > 1)
{
for (int k = 1; k < runTexts.Length; k++)
{
XElement newText = new XElement(DocX.w + "t", runTexts[k]);
XElement newRun;
if (runTexts[k] == String.Empty)
newRun = new XElement(DocX.w + "r", tabRun);
else
{
// Value begins or ends with a space
Novacode.Text.PreserveSpace(newText);
newRun = new XElement(DocX.w + "r", rPr, tabRun, newText);
}
newRuns.Add(newRun);
}
}
return newRuns;
}
/// <!--
/// Bug found and fixed by krugs525 on August 12 2009.
/// Use TFS compare to see exact code change.
/// -->
static internal int GetElementTextLength(XElement run)
{
int count = 0;
if (run == null)
return count;
@@ -498,7 +976,9 @@ namespace Novacode
{
switch (d.Name.LocalName)
{
case "tab": goto case "br";
case "tab":
if (d.Parent.Name.LocalName != "tabs")
goto case "br"; break;
case "br": count++; break;
case "t": goto case "delText";
case "delText": count += d.Value.Length; break;
@@ -632,7 +1112,7 @@ namespace Novacode
/// <param name="trackChanges">Flag this insert as a change.</param>
public void InsertText(string value, bool trackChanges)
{
List<XElement> newRuns = FormatInput(value, null);
List<XElement> newRuns = DocX.FormatInput(value, null);
xml.Add(newRuns);
runLookup.Clear();
@@ -697,12 +1177,13 @@ namespace Novacode
/// <param name="formatting">The text formatting.</param>
public void InsertText(string value, bool trackChanges, Formatting formatting)
{
List<XElement> newRuns = FormatInput(value, formatting.Xml);
List<XElement> newRuns = DocX.FormatInput(value, formatting.Xml);
xml.Add(newRuns);
runLookup.Clear();
BuildRunLookup(xml);
DocX.RenumberIDs(document);
DocX.RebuildParagraphs(document);
}
/// <summary>
@@ -774,9 +1255,9 @@ namespace Novacode
{
object insert;
if (formatting != null)
insert = FormatInput(value, formatting.Xml);
insert = DocX.FormatInput(value, formatting.Xml);
else
insert = FormatInput(value, null);
insert = DocX.FormatInput(value, null);
if (trackChanges)
insert = CreateEdit(EditType.ins, insert_datetime, insert);
@@ -787,9 +1268,9 @@ namespace Novacode
{
object newRuns;
if (formatting != null)
newRuns = FormatInput(value, formatting.Xml);
newRuns = DocX.FormatInput(value, formatting.Xml);
else
newRuns = FormatInput(value, run.xml.Element(XName.Get("rPr", DocX.w.NamespaceName)));
newRuns = DocX.FormatInput(value, run.xml.Element(XName.Get("rPr", DocX.w.NamespaceName)));
// The parent of this Run
XElement parentElement = run.xml.Parent;
@@ -867,6 +1348,84 @@ namespace Novacode
BuildRunLookup(xml);
DocX.RenumberIDs(document);
}
/// <summary>
/// Insert a field of type document property, this field will display the custom property cp, at the end of this paragraph.
/// </summary>
/// <param name="cp">The custom property to display.</param>
/// <param name="f">The formatting to use for this text.</param>
/// <example>
/// Create, add and display a custom property in a document.
/// <code>
/// // 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);
///
/// // Create a text formatting.
/// Formatting f = new Formatting();
/// f.Bold = true;
/// f.Size = 14;
/// f.StrikeThrough = StrickThrough.strike;
///
/// // Insert a new paragraph.
/// Paragraph p = document.InsertParagraph("Author: ", false, f);
///
/// // Insert a field of type document property to display the custom property name
/// p.InsertDocProperty(name, f);
///
/// // Save all changes made to this document.
/// document.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
public void InsertDocProperty(CustomProperty cp, Formatting f)
{
XElement e = new XElement
(
XName.Get("fldSimple", DocX.w.NamespaceName),
new XAttribute(XName.Get("instr", DocX.w.NamespaceName), string.Format(@"DOCPROPERTY {0} \* MERGEFORMAT", cp.Name)),
new XElement(XName.Get("r", DocX.w.NamespaceName),
new XElement(XName.Get("t", DocX.w.NamespaceName), f.Xml, cp.Value))
);
xml.Add(e);
}
/// <summary>
/// Insert a field of type document property, this field will display the custom property cp, at the end of this paragraph.
/// </summary>
/// <param name="cp">The custom property to display.</param>
/// <example>
/// Create, add and display a custom property in a document.
/// <code>
/// // 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.
/// </code>
/// </example>
public void InsertDocProperty(CustomProperty cp)
{
InsertDocProperty(cp, new Formatting());
}
/// <summary>
/// Removes characters from a Novacode.DocX.Paragraph.
@@ -1067,6 +1626,93 @@ namespace Novacode
}
}
/// <summary>
/// Find all instances of a string in this paragraph and return their indexes in a List.
/// </summary>
/// <param name="str">The string to find</param>
/// <returns>A list of indexes.</returns>
/// <example>
/// Find all instances of Hello in this document and insert 'don't' in frount of them.
/// <code>
/// // Load a document
/// 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&lt;int&gt; gos = document.FindAll("go");
///
/// /*
/// * Insert 'don't' in frount 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 ", false);
/// }
/// }
///
/// // Save all changes made to this document.
/// document.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
public List<int> FindAll(string str)
{
return FindAll(str, RegexOptions.None);
}
/// <summary>
/// Find all instances of a string in this paragraph and return their indexes in a List.
/// </summary>
/// <param name="str">The string to find</param>
/// <param name="options">The options to use when finding a string match.</param>
/// <returns>A list of indexes.</returns>
/// <example>
/// Find all instances of Hello in this document and insert 'don't' in frount of them.
/// <code>
/// // Load a document
/// 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 (Ignore case).
/// List&lt;int&gt; gos = document.FindAll("go", RegexOptions.IgnoreCase);
///
/// /*
/// * Insert 'don't' in frount 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 ", false);
/// }
/// }
///
/// // Save all changes made to this document.
/// document.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
public List<int> FindAll(string str, RegexOptions options)
{
MatchCollection mc = Regex.Matches(this.Text, Regex.Escape(str), options);
var query =
(
from m in mc.Cast<Match>()
select m.Index
).ToList();
return query;
}
/// <summary>
/// Replaces all occurrences of a specified System.String in this instance, with another specified System.String.
/// </summary>

+ 22
- 13
DocX/Picture.cs Переглянути файл

@@ -22,7 +22,7 @@ namespace Novacode
private object pictureShape;
// The underlying XElement which this Image wraps
internal XElement i;
internal XElement xml;
private XElement xfrm;
private XElement prstGeom;
@@ -44,13 +44,13 @@ namespace Novacode
using (System.Drawing.Image img = System.Drawing.Image.FromStream(part.GetStream()))
{
this.cx = img.Width * 4156;
this.cy = img.Height * 4156;
this.cx = img.Width * 9526;
this.cy = img.Height * 9526;
}
XElement e = new XElement(DocX.w + "drawing");
i = XElement.Parse
xml = XElement.Parse
(string.Format(@"
<drawing xmlns = ""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">
<wp:inline distT=""0"" distB=""0"" distL=""0"" distR=""0"" xmlns:wp=""http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"">
@@ -91,14 +91,14 @@ namespace Novacode
this.xfrm =
(
from d in i.Descendants()
from d in xml.Descendants()
where d.Name.LocalName.Equals("xfrm")
select d
).Single();
this.prstGeom =
(
from d in i.Descendants()
from d in xml.Descendants()
where d.Name.LocalName.Equals("prstGeom")
select d
).Single();
@@ -106,13 +106,21 @@ namespace Novacode
this.rotation = xfrm.Attribute(XName.Get("rot")) == null ? 0 : uint.Parse(xfrm.Attribute(XName.Get("rot")).Value);
}
/// <summary>
/// Remove this Picture from this document.
/// </summary>
public void Remove()
{
xml.Remove();
}
/// <summary>
/// Wraps an XElement as an Image
/// </summary>
/// <param name="i">The XElement i to wrap</param>
internal Picture(XElement i)
{
this.i = i;
this.xml = i;
this.id =
(
@@ -129,13 +137,14 @@ namespace Novacode
select a.Value
).First();
this.descr =
(
from e in i.Descendants()
let a = e.Attribute(XName.Get("descr"))
where (a != null)
select a.Value
).First();
).FirstOrDefault();
this.cx =
(
@@ -301,7 +310,7 @@ namespace Novacode
{
rotation = (value % 360) * 60000;
XElement xfrm =
(from d in i.Descendants()
(from d in xml.Descendants()
where d.Name.LocalName.Equals("xfrm")
select d).Single();
@@ -324,7 +333,7 @@ namespace Novacode
{
name = value;
foreach (XAttribute a in i.Descendants().Attributes(XName.Get("name")))
foreach (XAttribute a in xml.Descendants().Attributes(XName.Get("name")))
a.Value = name;
}
}
@@ -340,7 +349,7 @@ namespace Novacode
{
descr = value;
foreach (XAttribute a in i.Descendants().Attributes(XName.Get("descr")))
foreach (XAttribute a in xml.Descendants().Attributes(XName.Get("descr")))
a.Value = descr;
}
}
@@ -356,7 +365,7 @@ namespace Novacode
{
cx = value;
foreach (XAttribute a in i.Descendants().Attributes(XName.Get("cx")))
foreach (XAttribute a in xml.Descendants().Attributes(XName.Get("cx")))
a.Value = (cx * 4156).ToString();
}
}
@@ -372,7 +381,7 @@ namespace Novacode
{
cy = value;
foreach (XAttribute a in i.Descendants().Attributes(XName.Get("cy")))
foreach (XAttribute a in xml.Descendants().Attributes(XName.Get("cy")))
a.Value = (cy * 4156).ToString();
}
}

+ 2
- 2
DocX/Properties/AssemblyInfo.cs Переглянути файл

@@ -32,5 +32,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.5")]
[assembly: AssemblyFileVersion("1.0.0.5")]
[assembly: AssemblyVersion("1.0.0.7")]
[assembly: AssemblyFileVersion("1.0.0.7")]

+ 625
- 7
DocX/Table.cs Переглянути файл

@@ -13,13 +13,16 @@ namespace Novacode
/// <summary>
/// Designs\Styles that can be applied to a table.
/// </summary>
public enum TableDesign { TableNormal, TableGrid, LightShading, LightShadingAccent1, LightShadingAccent2, LightShadingAccent3, LightShadingAccent4, LightShadingAccent5, LightShadingAccent6, LightList, LightListAccent1, LightListAccent2, LightListAccent3, LightListAccent4, LightListAccent5, LightListAccent6, LightGrid, LightGridAccent1, LightGridAccent2, LightGridAccent3, LightGridAccent4, LightGridAccent5, LightGridAccent6, MediumShading1, MediumShading1Accent1, MediumShading1Accent2, MediumShading1Accent3, MediumShading1Accent4, MediumShading1Accent5, MediumShading1Accent6, MediumShading2, MediumShading2Accent1, MediumShading2Accent2, MediumShading2Accent3, MediumShading2Accent4, MediumShading2Accent5, MediumShading2Accent6, MediumList1, MediumList1Accent1, MediumList1Accent2, MediumList1Accent3, MediumList1Accent4, MediumList1Accent5, MediumList1Accent6, MediumList2, MediumList2Accent1, MediumList2Accent2, MediumList2Accent3, MediumList2Accent4, MediumList2Accent5, MediumList2Accent6, MediumGrid1, MediumGrid1Accent1, MediumGrid1Accent2, MediumGrid1Accent3, MediumGrid1Accent4, MediumGrid1Accent5, MediumGrid1Accent6, MediumGrid2, MediumGrid2Accent1, MediumGrid2Accent2, MediumGrid2Accent3, MediumGrid2Accent4, MediumGrid2Accent5, MediumGrid2Accent6, MediumGrid3, MediumGrid3Accent1, MediumGrid3Accent2, MediumGrid3Accent3, MediumGrid3Accent4, MediumGrid3Accent5, MediumGrid3Accent6, DarkList, DarkListAccent1, DarkListAccent2, DarkListAccent3, DarkListAccent4, DarkListAccent5, DarkListAccent6, ColorfulShading, ColorfulShadingAccent1, ColorfulShadingAccent2, ColorfulShadingAccent3, ColorfulShadingAccent4, ColorfulShadingAccent5, ColorfulShadingAccent6, ColorfulList, ColorfulListAccent1, ColorfulListAccent2, ColorfulListAccent3, ColorfulListAccent4, ColorfulListAccent5, ColorfulListAccent6, ColorfulGrid, ColorfulGridAccent1, ColorfulGridAccent2, ColorfulGridAccent3, ColorfulGridAccent4, ColorfulGridAccent5, ColorfulGridAccent6};
public enum TableDesign { TableNormal, TableGrid, LightShading, LightShadingAccent1, LightShadingAccent2, LightShadingAccent3, LightShadingAccent4, LightShadingAccent5, LightShadingAccent6, LightList, LightListAccent1, LightListAccent2, LightListAccent3, LightListAccent4, LightListAccent5, LightListAccent6, LightGrid, LightGridAccent1, LightGridAccent2, LightGridAccent3, LightGridAccent4, LightGridAccent5, LightGridAccent6, MediumShading1, MediumShading1Accent1, MediumShading1Accent2, MediumShading1Accent3, MediumShading1Accent4, MediumShading1Accent5, MediumShading1Accent6, MediumShading2, MediumShading2Accent1, MediumShading2Accent2, MediumShading2Accent3, MediumShading2Accent4, MediumShading2Accent5, MediumShading2Accent6, MediumList1, MediumList1Accent1, MediumList1Accent2, MediumList1Accent3, MediumList1Accent4, MediumList1Accent5, MediumList1Accent6, MediumList2, MediumList2Accent1, MediumList2Accent2, MediumList2Accent3, MediumList2Accent4, MediumList2Accent5, MediumList2Accent6, MediumGrid1, MediumGrid1Accent1, MediumGrid1Accent2, MediumGrid1Accent3, MediumGrid1Accent4, MediumGrid1Accent5, MediumGrid1Accent6, MediumGrid2, MediumGrid2Accent1, MediumGrid2Accent2, MediumGrid2Accent3, MediumGrid2Accent4, MediumGrid2Accent5, MediumGrid2Accent6, MediumGrid3, MediumGrid3Accent1, MediumGrid3Accent2, MediumGrid3Accent3, MediumGrid3Accent4, MediumGrid3Accent5, MediumGrid3Accent6, DarkList, DarkListAccent1, DarkListAccent2, DarkListAccent3, DarkListAccent4, DarkListAccent5, DarkListAccent6, ColorfulShading, ColorfulShadingAccent1, ColorfulShadingAccent2, ColorfulShadingAccent3, ColorfulShadingAccent4, ColorfulShadingAccent5, ColorfulShadingAccent6, ColorfulList, ColorfulListAccent1, ColorfulListAccent2, ColorfulListAccent3, ColorfulListAccent4, ColorfulListAccent5, ColorfulListAccent6, ColorfulGrid, ColorfulGridAccent1, ColorfulGridAccent2, ColorfulGridAccent3, ColorfulGridAccent4, ColorfulGridAccent5, ColorfulGridAccent6, None};
public enum AutoFit{Contents, Window, ColoumnWidth};
/// <summary>
/// Represents a Table in a document.
/// </summary>
public class Table
{
private Alignment alignment;
private AutoFit autofit;
private List<Row> rows;
private int rowCount, columnCount;
internal XElement xml;
@@ -43,6 +46,7 @@ namespace Novacode
internal Table(DocX document, XElement xml)
{
autofit = AutoFit.ColoumnWidth;
this.xml = xml;
this.document = document;
@@ -58,10 +62,108 @@ namespace Novacode
columnCount = rows[0].Cells.Count;
XElement style = properties.Element(XName.Get("tblStyle", DocX.w.NamespaceName));
XAttribute val = style.Attribute(XName.Get("val", DocX.w.NamespaceName));
design = (TableDesign)Enum.Parse(typeof(TableDesign), val.Value.Replace("-", string.Empty));
if (style != null)
{
XAttribute val = style.Attribute(XName.Get("val", DocX.w.NamespaceName));
if (val != null)
design = (TableDesign)Enum.Parse(typeof(TableDesign), val.Value.Replace("-", string.Empty));
else
design = TableDesign.None;
}
else
design = TableDesign.None;
}
public Alignment Alignment
{
get { return alignment; }
set
{
string alignmentString = string.Empty;
switch (value)
{
case Alignment.left:
{
alignmentString = "left";
break;
}
case Alignment.both:
{
alignmentString = "both";
break;
}
case Alignment.right:
{
alignmentString = "right";
break;
}
case Alignment.center:
{
alignmentString = "center";
break;
}
}
XElement tblPr = xml.Descendants(XName.Get("tblPr", DocX.w.NamespaceName)).First();
XElement jc = tblPr.Descendants(XName.Get("jc", DocX.w.NamespaceName)).FirstOrDefault();
if(jc != null)
jc.Remove();
jc = new XElement(XName.Get("jc", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), alignmentString));
tblPr.Add(jc);
alignment = value;
}
}
/// <summary>
/// Auto size this table according to some rule.
/// </summary>
public AutoFit AutoFit
{
get{return autofit;}
set
{
string attributeValue = string.Empty;
switch(value)
{
case AutoFit.ColoumnWidth:
{
attributeValue = "dxa";
break;
}
case AutoFit.Contents:
{
attributeValue = "auto";
break;
}
case AutoFit.Window:
{
attributeValue = "pct";
break;
}
}
var query = from d in xml.Descendants()
let type = d.Attribute(XName.Get("type", DocX.w.NamespaceName))
where (d.Name.LocalName == "tcW" || d.Name.LocalName == "tblW") && type != null
select type;
foreach (XAttribute type in query)
type.Value = attributeValue;
autofit = value;
}
}
/// <summary>
/// The design\style to apply to this table.
/// </summary>
@@ -87,6 +189,12 @@ namespace Novacode
design = value;
if (design == TableDesign.None)
{
if (style != null)
style.Remove();
}
switch (design)
{
case TableDesign.TableNormal: val.Value = "TableNormal"; break;
@@ -212,9 +320,7 @@ namespace Novacode
if (tableStyle == null)
{
Assembly _assembly = Assembly.GetExecutingAssembly();
TextReader _textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("Novacode.styles.xml"));
XDocument external_style_doc = XDocument.Load(_textStreamReader);
XDocument external_style_doc = DocX.DecompressXMLResource("Novacode.Resources.styles.xml.gz");
var styleElement =
(
@@ -264,6 +370,74 @@ namespace Novacode
return InsertRow(rows.Count);
}
/// <summary>
/// Returns the index of this Table.
/// </summary>
/// <example>
/// Replace the first table in this document with a new Table.
/// <code>
/// // Load a document into memory.
/// using (DocX document = DocX.Load(@"Test.docx"))
/// {
/// // Get the first Table in this document.
/// Table t = document.Tables[0];
///
/// // Get the character index of Table t in this document.
/// int index = t.Index;
///
/// // Remove Table t.
/// t.Remove();
///
/// // Insert a new Table at the original index of Table t.
/// Table newTable = document.InsertTable(index, 4, 4);
///
/// // Set the design of this new Table, so that we can see it.
/// newTable.Design = TableDesign.LightShadingAccent1;
///
/// // Save all changes made to the document.
/// document.Save();
/// } // Release this document from memory.
/// </code>
/// </example>
public int Index
{
get
{
int index = 0;
IEnumerable<XElement> previous = xml.ElementsBeforeSelf();
foreach (XElement e in previous)
index += Paragraph.GetElementTextLength(e);
return index;
}
}
/// <summary>
/// Remove this Table from this document.
/// </summary>
/// <example>
/// Remove the first Table from this document.
/// <code>
/// // Load a document into memory.
/// using (DocX document = DocX.Load(@"Test.docx"))
/// {
/// // Get the first Table in this document.
/// Table t = d.Tables[0];
///
/// // Remove this Table.
/// t.Remove();
///
/// // Save all changes made to the document.
/// document.Save();
/// } // Release this document from memory.
/// </code>
/// </example>
public void Remove()
{
xml.Remove();
}
/// <summary>
/// Insert a column to the right of a Table.
/// </summary>
@@ -535,6 +709,450 @@ namespace Novacode
columnCount = rows[0].Cells.Count;
}
}
/// <summary>
/// Insert a new Table before this Table, this Table can be from this document or another document.
/// </summary>
/// <param name="t">The Table t to be inserted</param>
/// <returns>A new Table inserted before this Table.</returns>
/// <example>
/// Insert a new Table before this Table.
/// <code>
/// // Place holder for a Table.
/// Table t;
///
/// // Load document a.
/// using (DocX documentA = DocX.Load(@"a.docx"))
/// {
/// // Get the first Table from this document.
/// t = documentA.Tables[0];
/// }
///
/// // Load document b.
/// using (DocX documentB = DocX.Load(@"b.docx"))
/// {
/// // Get the first Table in document b.
/// Table t2 = documentB.Tables[0];
///
/// // Insert the Table from document a before this Table.
/// Table newTable = t2.InsertTableBeforeSelf(t);
///
/// // Save all changes made to document b.
/// documentB.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
public Table InsertTableBeforeSelf(Table t)
{
xml.AddBeforeSelf(t.xml);
XElement newlyInserted = xml.ElementsBeforeSelf().First();
t.xml = newlyInserted;
DocX.RebuildTables(document);
DocX.RebuildParagraphs(document);
return t;
}
/// <summary>
/// Insert a new Table into this document before this Table.
/// </summary>
/// <param name="rowCount">The number of rows this Table should have.</param>
/// <param name="coloumnCount">The number of coloumns this Table should have.</param>
/// <returns>A new Table inserted before this Table.</returns>
/// <example>
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create(@"Test.docx"))
/// {
/// //Insert a Table into this document.
/// Table t = document.InsertTable(2, 2);
/// t.Design = TableDesign.LightShadingAccent1;
/// t.Alignment = Alignment.center;
///
/// // Insert a new Table before this Table.
/// Table newTable = t.InsertTableBeforeSelf(2, 2);
/// newTable.Design = TableDesign.LightShadingAccent2;
/// newTable.Alignment = Alignment.center;
///
/// // Save all changes made to this document.
/// document.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
public Table InsertTableBeforeSelf(int rowCount, int coloumnCount)
{
XElement newTable = DocX.CreateTable(rowCount, coloumnCount);
xml.AddBeforeSelf(newTable);
XElement newlyInserted = xml.ElementsBeforeSelf().First();
DocX.RebuildTables(document);
DocX.RebuildParagraphs(document);
return new Table(document, newlyInserted);
}
/// <summary>
/// Insert a new Table after this Table, this Table can be from this document or another document.
/// </summary>
/// <param name="t">The Table t to be inserted</param>
/// <returns>A new Table inserted after this Table.</returns>
/// <example>
/// Insert a new Table after this Table.
/// <code>
/// // Place holder for a Table.
/// Table t;
///
/// // Load document a.
/// using (DocX documentA = DocX.Load(@"a.docx"))
/// {
/// // Get the first Table from this document.
/// t = documentA.Tables[0];
/// }
///
/// // Load document b.
/// using (DocX documentB = DocX.Load(@"b.docx"))
/// {
/// // Get the first Table in document b.
/// Table t2 = documentB.Tables[0];
///
/// // Insert the Table from document a after this Table.
/// Table newTable = t2.InsertTableAfterSelf(t);
///
/// // Save all changes made to document b.
/// documentB.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
public Table InsertTableAfterSelf(Table t)
{
xml.AddAfterSelf(t.xml);
XElement newlyInserted = xml.ElementsAfterSelf().First();
t.xml = newlyInserted;
DocX.RebuildTables(document);
DocX.RebuildParagraphs(document);
return t;
}
/// <summary>
/// Insert a new Table into this document after this Table.
/// </summary>
/// <param name="rowCount">The number of rows this Table should have.</param>
/// <param name="coloumnCount">The number of coloumns this Table should have.</param>
/// <returns>A new Table inserted before this Table.</returns>
/// <example>
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create(@"Test.docx"))
/// {
/// //Insert a Table into this document.
/// Table t = document.InsertTable(2, 2);
/// t.Design = TableDesign.LightShadingAccent1;
/// t.Alignment = Alignment.center;
///
/// // Insert a new Table after this Table.
/// Table newTable = t.InsertTableAfterSelf(2, 2);
/// newTable.Design = TableDesign.LightShadingAccent2;
/// newTable.Alignment = Alignment.center;
///
/// // Save all changes made to this document.
/// document.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
public Table InsertTableAfterSelf(int rowCount, int coloumnCount)
{
XElement newTable = DocX.CreateTable(rowCount, coloumnCount);
xml.AddAfterSelf(newTable);
XElement newlyInserted = xml.ElementsAfterSelf().First();
DocX.RebuildTables(document);
DocX.RebuildParagraphs(document);
return new Table(document, newlyInserted);
}
/// <summary>
/// Insert a Paragraph before this Table, this Paragraph may have come from the same or another document.
/// </summary>
/// <param name="p">The Paragraph to insert.</param>
/// <returns>The Paragraph now associated with this document.</returns>
/// <example>
/// Take a Paragraph from document a, and insert it into document b before this Table.
/// <code>
/// // Place holder for a Paragraph.
/// Paragraph p;
///
/// // Load document a.
/// using (DocX documentA = DocX.Load(@"a.docx"))
/// {
/// // Get the first paragraph from this document.
/// p = documentA.Paragraphs[0];
/// }
///
/// // Load document b.
/// using (DocX documentB = DocX.Load(@"b.docx"))
/// {
/// // Get the first Table in document b.
/// Table t = documentB.Tables[0];
///
/// // Insert the Paragraph from document a before this Table.
/// Paragraph newParagraph = t.InsertParagraphBeforeSelf(p);
///
/// // Save all changes made to document b.
/// documentB.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
public Paragraph InsertParagraphBeforeSelf(Paragraph p)
{
xml.AddBeforeSelf(p.xml);
XElement newlyInserted = xml.ElementsBeforeSelf().First();
p.xml = newlyInserted;
DocX.RebuildParagraphs(document);
return p;
}
/// <summary>
/// Insert a new Paragraph before this Table.
/// </summary>
/// <param name="text">The initial text for this new Paragraph.</param>
/// <returns>A new Paragraph inserted before this Table.</returns>
/// <example>
/// Insert a new Paragraph before the first Table in this document.
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create(@"Test.docx"))
/// {
/// // Insert a Table into this document.
/// Table t = document.InsertTable(2, 2);
///
/// t.InsertParagraphBeforeSelf("I was inserted before the next Table.");
///
/// // Save all changes made to this new document.
/// document.Save();
/// }// Release this new document form memory.
/// </code>
/// </example>
public Paragraph InsertParagraphBeforeSelf(string text)
{
return InsertParagraphBeforeSelf(text, false, new Formatting());
}
/// <summary>
/// Insert a new Paragraph before this Table.
/// </summary>
/// <param name="text">The initial text for this new Paragraph.</param>
/// <param name="trackChanges">Should this insertion be tracked as a change?</param>
/// <returns>A new Paragraph inserted before this Table.</returns>
/// <example>
/// Insert a new paragraph before the first Table in this document.
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create(@"Test.docx"))
/// {
/// // Insert a Table into this document.
/// Table t = document.InsertTable(2, 2);
///
/// t.InsertParagraphBeforeSelf("I was inserted before the next Table.", false);
///
/// // Save all changes made to this new document.
/// document.Save();
/// }// Release this new document form memory.
/// </code>
/// </example>
public Paragraph InsertParagraphBeforeSelf(string text, bool trackChanges)
{
return InsertParagraphBeforeSelf(text, trackChanges, new Formatting());
}
/// <summary>
/// Insert a new Paragraph before this Table.
/// </summary>
/// <param name="text">The initial text for this new Paragraph.</param>
/// <param name="trackChanges">Should this insertion be tracked as a change?</param>
/// <param name="formatting">The formatting to apply to this insertion.</param>
/// <returns>A new Paragraph inserted before this Table.</returns>
/// <example>
/// Insert a new paragraph before the first Table in this document.
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create(@"Test.docx"))
/// {
/// // Insert a Table into this document.
/// Table t = document.InsertTable(2, 2);
///
/// Formatting boldFormatting = new Formatting();
/// boldFormatting.Bold = true;
///
/// t.InsertParagraphBeforeSelf("I was inserted before the next Table.", false, boldFormatting);
///
/// // Save all changes made to this new document.
/// document.Save();
/// }// Release this new document form memory.
/// </code>
/// </example>
public Paragraph InsertParagraphBeforeSelf(string text, bool trackChanges, Formatting formatting)
{
XElement newParagraph = new XElement
(
XName.Get("p", DocX.w.NamespaceName), new XElement(XName.Get("pPr", DocX.w.NamespaceName)), DocX.FormatInput(text, formatting.Xml)
);
if (trackChanges)
newParagraph = Paragraph.CreateEdit(EditType.ins, DateTime.Now, newParagraph);
xml.AddBeforeSelf(newParagraph);
XElement newlyInserted = xml.ElementsBeforeSelf().First();
Paragraph p = new Paragraph(document, -1, newlyInserted);
DocX.RebuildParagraphs(document);
return p;
}
/// <summary>
/// Insert a Paragraph after this Table, this Paragraph may have come from the same or another document.
/// </summary>
/// <param name="p">The Paragraph to insert.</param>
/// <returns>The Paragraph now associated with this document.</returns>
/// <example>
/// Take a Paragraph from document a, and insert it into document b after this Table.
/// <code>
/// // Place holder for a Paragraph.
/// Paragraph p;
///
/// // Load document a.
/// using (DocX documentA = DocX.Load(@"a.docx"))
/// {
/// // Get the first paragraph from this document.
/// p = documentA.Paragraphs[0];
/// }
///
/// // Load document b.
/// using (DocX documentB = DocX.Load(@"b.docx"))
/// {
/// // Get the first Table in document b.
/// Table t = documentB.Tables[0];
///
/// // Insert the Paragraph from document a after this Table.
/// Paragraph newParagraph = t.InsertParagraphAfterSelf(p);
///
/// // Save all changes made to document b.
/// documentB.Save();
/// }// Release this document from memory.
/// </code>
/// </example>
public Paragraph InsertParagraphAfterSelf(Paragraph p)
{
xml.AddAfterSelf(p.xml);
XElement newlyInserted = xml.ElementsAfterSelf().First();
p.xml = newlyInserted;
DocX.RebuildParagraphs(document);
return p;
}
/// <summary>
/// Insert a new Paragraph after this Table.
/// </summary>
/// <param name="text">The initial text for this new Paragraph.</param>
/// <param name="trackChanges">Should this insertion be tracked as a change?</param>
/// <param name="formatting">The formatting to apply to this insertion.</param>
/// <returns>A new Paragraph inserted after this Table.</returns>
/// <example>
/// Insert a new paragraph after the first Table in this document.
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create(@"Test.docx"))
/// {
/// // Insert a Table into this document.
/// Table t = document.InsertTable(2, 2);
///
/// Formatting boldFormatting = new Formatting();
/// boldFormatting.Bold = true;
///
/// t.InsertParagraphAfterSelf("I was inserted after the previous Table.", false, boldFormatting);
///
/// // Save all changes made to this new document.
/// document.Save();
/// }// Release this new document form memory.
/// </code>
/// </example>
public Paragraph InsertParagraphAfterSelf(string text, bool trackChanges, Formatting formatting)
{
XElement newParagraph = new XElement
(
XName.Get("p", DocX.w.NamespaceName), new XElement(XName.Get("pPr", DocX.w.NamespaceName)), DocX.FormatInput(text, formatting.Xml)
);
if (trackChanges)
newParagraph = Paragraph.CreateEdit(EditType.ins, DateTime.Now, newParagraph);
xml.AddAfterSelf(newParagraph);
XElement newlyInserted = xml.ElementsAfterSelf().First();
Paragraph p = new Paragraph(document, -1, newlyInserted);
DocX.RebuildParagraphs(document);
return p;
}
/// <summary>
/// Insert a new Paragraph after this Table.
/// </summary>
/// <param name="text">The initial text for this new Paragraph.</param>
/// <param name="trackChanges">Should this insertion be tracked as a change?</param>
/// <returns>A new Paragraph inserted after this Table.</returns>
/// <example>
/// Insert a new paragraph after the first Table in this document.
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create(@"Test.docx"))
/// {
/// // Insert a Table into this document.
/// Table t = document.InsertTable(2, 2);
///
/// t.InsertParagraphAfterSelf("I was inserted after the previous Table.", false);
///
/// // Save all changes made to this new document.
/// document.Save();
/// }// Release this new document form memory.
/// </code>
/// </example>
public Paragraph InsertParagraphAfterSelf(string text, bool trackChanges)
{
return InsertParagraphAfterSelf(text, trackChanges, new Formatting());
}
/// <summary>
/// Insert a new Paragraph after this Table.
/// </summary>
/// <param name="text">The initial text for this new Paragraph.</param>
/// <returns>A new Paragraph inserted after this Table.</returns>
/// <example>
/// Insert a new Paragraph after the first Table in this document.
/// <code>
/// // Create a new document.
/// using (DocX document = DocX.Create(@"Test.docx"))
/// {
/// // Insert a Table into this document.
/// Table t = document.InsertTable(2, 2);
///
/// t.InsertParagraphAfterSelf("I was inserted after the previous Table.");
///
/// // Save all changes made to this new document.
/// document.Save();
/// }// Release this new document form memory.
/// </code>
/// </example>
public Paragraph InsertParagraphAfterSelf(string text)
{
return InsertParagraphAfterSelf(text, false, new Formatting());
}
}
/// <summary>

Завантаження…
Відмінити
Зберегти