ソースを参照

Implementation refactored for consistency. Documentation (.chm) file added with lots of code examples.

master
coffeycathal_cp 17年前
コミット
1dfdfc17b7
16個のファイルの変更1236行の追加430行の削除
  1. 0
    6
      DocX.sln
  2. 0
    10
      DocX.vssscc
  3. 6
    10
      DocX/CustomProperty.cs
  4. 669
    199
      DocX/DocX.cs
  5. 5
    2
      DocX/DocX.csproj
  6. 7
    0
      DocX/Enumerations.cs
  7. 2
    2
      DocX/Extensions.cs
  8. 68
    9
      DocX/Formatting.cs
  9. バイナリ
      DocX/Help/Documentation.chm
  10. 9
    0
      DocX/Image.cs
  11. 7
    0
      DocX/License/License.html
  12. 388
    123
      DocX/Paragraph.cs
  13. 46
    0
      DocX/Picture.cs
  14. 2
    2
      DocX/Properties/AssemblyInfo.cs
  15. 18
    41
      DocX/Run.cs
  16. 9
    26
      DocX/Text.cs

+ 0
- 6
DocX.sln ファイルの表示

@@ -5,8 +5,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DocX", "DocX\DocX.csproj",
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{DF41F5CE-8BCB-40CC-835F-54A3DB7D802F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Examples", "Examples\Examples.csproj", "{EF2E13CC-B36B-4B5A-AE73-78F5DDCA094E}"
EndProject
Global
GlobalSection(TeamFoundationVersionControl) = preSolution
SccNumberOfProjects = 3
@@ -29,10 +27,6 @@ Global
{E863D072-AA8B-4108-B5F1-785241B37F67}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E863D072-AA8B-4108-B5F1-785241B37F67}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E863D072-AA8B-4108-B5F1-785241B37F67}.Release|Any CPU.Build.0 = Release|Any CPU
{EF2E13CC-B36B-4B5A-AE73-78F5DDCA094E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EF2E13CC-B36B-4B5A-AE73-78F5DDCA094E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EF2E13CC-B36B-4B5A-AE73-78F5DDCA094E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EF2E13CC-B36B-4B5A-AE73-78F5DDCA094E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

+ 0
- 10
DocX.vssscc ファイルの表示

@@ -1,10 +0,0 @@
""
{
"FILE_VERSION" = "9237"
"ENLISTMENT_CHOICE" = "NEVER"
"PROJECT_FILE_RELATIVE_PATH" = ""
"NUMBER_OF_EXCLUDED_FILES" = "0"
"ORIGINAL_PROJECT_FILE_PATH" = ""
"NUMBER_OF_NESTED_PROJECTS" = "0"
"SOURCE_CONTROL_SETTINGS_PROVIDER" = "PROJECT"
}

+ 6
- 10
DocX/CustomProperty.cs ファイルの表示

@@ -7,12 +7,12 @@ using System.Xml.Linq;
namespace Novacode
{
/// <summary>
/// Represents a .docx custom property.
/// Represents a document custom property.
/// </summary>
public class CustomProperty
{
// The underlying XElement which this CustomProperty wraps
private XElement cp;
private XElement xml;
// This customPropertys name
private string name;
// This customPropertys type
@@ -35,16 +35,12 @@ namespace Novacode
/// </summary>
public object Value { get { return value; } }
/// <summary>
/// Wraps a System.Xml.Linq.XElement as an instance of Novacode.DocX.CustomProperty
/// </summary>
/// <param name="cp">The XElement to wrap</param>
public CustomProperty(XElement cp)
internal CustomProperty(XElement xml)
{
this.cp = cp;
name = cp.Attribute(XName.Get("name")).Value;
this.xml = xml;
name = xml.Attribute(XName.Get("name")).Value;
XElement p = cp.Elements().SingleOrDefault();
XElement p = xml.Elements().SingleOrDefault();
switch (p.Name.LocalName)
{

+ 669
- 199
DocX/DocX.cs
ファイル差分が大きすぎるため省略します
ファイルの表示


+ 5
- 2
DocX/DocX.csproj ファイルの表示

@@ -27,7 +27,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Debug\DocX.XML</DocumentationFile>
<DocumentationFile>bin\Debug\DocX.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
@@ -73,7 +73,10 @@
<Compile Include="Text.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="Help\Documentation.chm" />
</ItemGroup>
<ItemGroup>
<Content Include="License\License.html" />
</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
- 0
DocX/Enumerations.cs ファイルの表示

@@ -5,6 +5,13 @@ using System.Text;
namespace Novacode
{
public enum Script { superscript, subscript, none }
public enum Highlight { yellow, green, cyan, magenta, blue, red, darkBlue, darkCyan, darkGreen, darkMagenta, darkRed, darkYellow, darkGray, lightGray, black, none };
public enum UnderlineStyle { none, singleLine, doubleLine, thick, dotted, dottedHeavy, dash, dashedHeavy, dashLong, dashLongHeavy, dotDash, dashDotHeavy, dotDotDash, dashDotDotHeavy, wave, wavyHeavy, wavyDouble, words };
public enum StrickThrough { none, strike, doubleStrike };
public enum Misc { none, shadow, outline, outlineShadow, emboss, engrave };
public enum CapsStyle { none, caps, smallCaps };
public enum RectangleShapes
{
rect,

+ 2
- 2
DocX/Extensions.cs ファイルの表示

@@ -6,9 +6,9 @@ using System.Drawing;
namespace Novacode
{
public static class Extensions
internal static class Extensions
{
public static string ToHex(this Color source)
internal static string ToHex(this Color source)
{
byte red = source.R;
byte green = source.G;

+ 68
- 9
DocX/Formatting.cs ファイルの表示

@@ -4,16 +4,11 @@ using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Drawing;
namespace Novacode
{
public enum Script { superscript, subscript, none }
public enum Highlight { yellow, green, cyan, magenta, blue, red, darkBlue, darkCyan, darkGreen, darkMagenta, darkRed, darkYellow, darkGray, lightGray, black, none};
public enum UnderlineStyle { none, singleLine, doubleLine, thick, dotted, dottedHeavy, dash, dashedHeavy, dashLong, dashLongHeavy, dotDash, dashDotHeavy, dotDotDash, dashDotDotHeavy, wave, wavyHeavy, wavyDouble, words};
public enum StrickThrough { none, strike, doubleStrike };
public enum Misc { none, shadow, outline, outlineShadow, emboss, engrave};
public enum CapsStyle { none, caps, smallCaps };
/// <summary>
/// A text formatting.
/// </summary>
public class Formatting
{
private XElement rPr;
@@ -35,6 +30,9 @@ namespace Novacode
private int? position;
private double? spacing;
/// <summary>
/// A text formatting.
/// </summary>
public Formatting()
{
capsStyle = CapsStyle.none;
@@ -47,7 +45,7 @@ namespace Novacode
rPr = new XElement(XName.Get("rPr", DocX.w.NamespaceName));
}
public XElement Xml
internal XElement Xml
{
get
{
@@ -173,11 +171,29 @@ namespace Novacode
}
}
/// <summary>
/// This formatting will apply Bold.
/// </summary>
public bool Bold { get { return bold; } set { bold = value;} }
/// <summary>
/// This formatting will apply Italic.
/// </summary>
public bool Italic { get { return Italic; } set { italic = value; } }
/// <summary>
/// This formatting will apply StrickThrough.
/// </summary>
public StrickThrough StrikeThrough { get { return strikethrough; } set { strikethrough = value; } }
/// <summary>
/// The script that this formatting should be, normal, superscript or subscript.
/// </summary>
public Script Script { get { return script; } set { script = value; } }
/// <summary>
/// The Size of this text, must be between 0 and 1638.
/// </summary>
public double? Size
{
get { return size; }
@@ -199,6 +215,9 @@ namespace Novacode
}
}
/// <summary>
/// Percentage scale must be one of the following values 200, 150, 100, 90, 80, 66, 50 or 33.
/// </summary>
public int? PercentageScale
{
get { return percentageScale; }
@@ -212,6 +231,9 @@ namespace Novacode
}
}
/// <summary>
/// The Kerning to apply to this text must be one of the following values 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72.
/// </summary>
public int? Kerning
{
get { return kerning; }
@@ -225,6 +247,9 @@ namespace Novacode
}
}
/// <summary>
/// Text position must be in the range (-1585 - 1585).
/// </summary>
public int? Position
{
get { return position; }
@@ -238,6 +263,9 @@ namespace Novacode
}
}
/// <summary>
/// Text spacing must be in the range (-1585 - 1585).
/// </summary>
public double? Spacing
{
get { return spacing; }
@@ -259,13 +287,44 @@ namespace Novacode
}
}
/// <summary>
/// The colour of the text.
/// </summary>
public Color? FontColor { get { return fontColor; } set { fontColor = value; } }
/// <summary>
/// Highlight colour.
/// </summary>
public Highlight Highlight { get { return highlight; } set { highlight = value; } }
/// <summary>
/// The Underline style that this formatting applies.
/// </summary>
public UnderlineStyle UnderlineStyle { get { return underlineStyle; } set { underlineStyle = value; } }
/// <summary>
/// The underline colour.
/// </summary>
public Color? UnderlineColor { get { return underlineColor; } set { underlineColor = value; } }
/// <summary>
/// Misc settings.
/// </summary>
public Misc Misc { get { return misc; } set { misc = value; } }
/// <summary>
/// Is this text hidden or visible.
/// </summary>
public bool Hidden { get { return hidden; } set { hidden = value; } }
/// <summary>
/// Capitalization style.
/// </summary>
public CapsStyle CapsStyle { get { return capsStyle; } set { capsStyle = value; } }
/// <summary>
/// The font familt of this formatting.
/// </summary>
public FontFamily FontFamily { get { return FontFamily; } set { fontFamily = value; } }
}

バイナリ
DocX/Help/Documentation.chm ファイルの表示


+ 9
- 0
DocX/Image.cs ファイルの表示

@@ -6,10 +6,19 @@ using DocumentFormat.OpenXml.Packaging;
namespace Novacode
{
/// <summary>
/// Represents an Image embedded in a document.
/// </summary>
public class Image
{
/// <summary>
/// A unique id which identifies this Image.
/// </summary>
private string id;
/// <summary>
/// Returns the id of this Image.
/// </summary>
public string Id
{
get {return id;}

+ 7
- 0
DocX/License/License.html ファイルの表示

@@ -0,0 +1,7 @@
<html>
<body>
<div id="licenseTextHolder" style="margin: 1em">
<span id="ctl00_ctl00_MasterContent_Content_licenseText">Microsoft Public License &#40;Ms-PL&#41;<br /><br />This license governs use of the accompanying software. If you use the software, you accept this license. If you do not accept the license, do not use the software.<br /><br />1. Definitions<br /><br />The terms &#34;reproduce,&#34; &#34;reproduction,&#34; &#34;derivative works,&#34; and &#34;distribution&#34; have the same meaning here as under U.S. copyright law.<br /><br />A &#34;contribution&#34; is the original software, or any additions or changes to the software.<br /><br />A &#34;contributor&#34; is any person that distributes its contribution under this license.<br /><br />&#34;Licensed patents&#34; are a contributor&#39;s patent claims that read directly on its contribution.<br /><br />2. Grant of Rights<br /><br />&#40;A&#41; Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.<br /><br />&#40;B&#41; Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and&#47;or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.<br /><br />3. Conditions and Limitations<br /><br />&#40;A&#41; No Trademark License- This license does not grant you rights to use any contributors&#39; name, logo, or trademarks.<br /><br />&#40;B&#41; If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically.<br /><br />&#40;C&#41; If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software.<br /><br />&#40;D&#41; If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license.<br /><br />&#40;E&#41; The software is licensed &#34;as-is.&#34; You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.</span>
</div>
</body>
</html><!-- @SortOrder 2 -->

+ 388
- 123
DocX/Paragraph.cs ファイルの表示

@@ -11,7 +11,7 @@ using System.Collections;
namespace Novacode
{
/// <summary>
/// Represents a .docx paragraph.
/// Represents a document paragraph.
/// </summary>
public class Paragraph
{
@@ -22,28 +22,32 @@ namespace Novacode
Dictionary<int, Run> runLookup = new Dictionary<int, Run>();
// The underlying XElement which this Paragraph wraps
private XElement p;
internal XElement xml;
internal int startIndex, endIndex;
// A collection of images in this paragraph
private IEnumerable<Picture> pictures;
public IEnumerable<Picture> Pictures { get { return pictures; } }
private List<Picture> pictures;
/// <summary>
/// Wraps a XElement as a Paragraph.
/// Returns a list of Pictures in this Paragraph.
/// </summary>
/// <param name="p">The XElement to wrap.</param>
internal Paragraph(XElement p)
public List<Picture> Pictures { get { return pictures; } }
internal Paragraph(int startIndex, XElement p)
{
this.p = p;
this.startIndex = startIndex;
this.endIndex = startIndex + GetElementTextLength(p);
this.xml = p;
BuildRunLookup(p);
// Get all of the images in this document
pictures = from i in p.Descendants(XName.Get("drawing", DocX.w.NamespaceName))
select new Picture(i);
pictures = (from i in p.Descendants(XName.Get("drawing", DocX.w.NamespaceName))
select new Picture(i)).ToList();
}
/// <summary>
/// Gets or set this paragraphs text alignment
/// Gets or set this Paragraphs text alignment.
/// </summary>
public Alignment Alignment
{
@@ -53,14 +57,14 @@ namespace Novacode
{
alignment = value;
XElement pPr = p.Element(XName.Get("pPr", DocX.w.NamespaceName));
XElement pPr = xml.Element(XName.Get("pPr", DocX.w.NamespaceName));
if (alignment != Novacode.Alignment.left)
{
if (pPr == null)
p.Add(new XElement(XName.Get("pPr", DocX.w.NamespaceName)));
xml.Add(new XElement(XName.Get("pPr", DocX.w.NamespaceName)));
pPr = p.Element(XName.Get("pPr", DocX.w.NamespaceName));
pPr = xml.Element(XName.Get("pPr", DocX.w.NamespaceName));
XElement jc = pPr.Element(XName.Get("jc", DocX.w.NamespaceName));
@@ -83,13 +87,33 @@ namespace Novacode
}
}
public void Delete(bool trackChanges)
/// <summary>
/// Remove this Paragraph from the document.
/// </summary>
/// <param name="trackChanges">Should this remove be tracked as a change?</param>
/// <example>
/// Remove a Paragraph from a document and track it as a change.
/// <code>
/// // Load a document using its fully qualified filename.
/// DocX document = DocX.Create(@"Test.docx");
///
/// // Create and Insert a new Paragraph into this document.
/// Paragraph p = document.InsertParagraph("Hello", false);
///
/// // Remove the Paragraph and track this as a change.
/// p.Remove(true);
///
/// // Close the document.
/// document.Close(true);
/// </code>
/// </example>
public void Remove(bool trackChanges)
{
if (trackChanges)
{
DateTime now = DateTime.Now.ToUniversalTime();
List<XElement> elements = p.Elements().ToList();
List<XElement> elements = xml.Elements().ToList();
List<XElement> temp = new List<XElement>();
for (int i = 0; i < elements.Count(); i++ )
{
@@ -112,14 +136,14 @@ namespace Novacode
}
if (temp.Count() > 0)
p.Add(CreateEdit(EditType.del, now, temp));
xml.Add(CreateEdit(EditType.del, now, temp));
}
else
{
// Remove this paragraph from the document
p.Remove();
p = null;
xml.Remove();
xml = null;
runLookup.Clear();
runLookup = null;
@@ -149,9 +173,9 @@ namespace Novacode
}
/// <summary>
/// Gets the value of this Novacode.DocX.Paragraph.
/// Gets the text value of this Paragraph.
/// </summary>
public string Value
public string Text
{
// Returns the underlying XElement's Value property.
get
@@ -159,7 +183,7 @@ namespace Novacode
StringBuilder sb = new StringBuilder();
// Loop through each run in this paragraph
foreach (XElement r in p.Descendants(XName.Get("r", DocX.w.NamespaceName)))
foreach (XElement r in xml.Descendants(XName.Get("r", DocX.w.NamespaceName)))
{
// Loop through each text item in this run
foreach (XElement descendant in r.Descendants())
@@ -186,19 +210,58 @@ namespace Novacode
}
}
/// <summary>
/// Insert a Picture into this document at a specified index.
/// </summary>
/// <param name="picture">The Picture to insert.</param>
/// <param name="index">The index to insert at.</param>
/// <example>
/// <code>
/// // Create a document using a relative filename.
/// DocX document = DocX.Create(@"Test.docx");
///
/// // Add an Image to this document.
/// Novacode.Image img = document.AddImage("Image.jpg");
///
/// // Create a Picture, a picture is like a custom view of an Image.
/// Picture pic = new Picture(img.Id, "Photo 31415", "A pie I baked.");
///
/// // Rotate the Picture clockwise by 30 degrees.
/// pic.Rotation = 30;
///
/// // Resize the Picture.
/// pic.Width = 400;
/// pic.Height = 300;
///
/// // Set the shape of this Picture to be a cube.
/// pic.SetPictureShape(BasicShapes.cube);
///
/// // Flip the Picture Horizontally.
/// pic.FlipHorizontal = true;
///
/// // Add a new Paragraph to this document.
/// Paragraph p = document.InsertParagraph("Here is Picture 1", false);
///
/// // Insert pic at the end of Paragraph p.
/// p.InsertPicture(pic, p.Text.Length);
///
/// // Close the document.
/// document.Close(true);
/// </code>
/// </example>
public void InsertPicture(Picture picture, int index)
{
Run run = GetFirstRunEffectedByEdit(index);
if (run == null)
p.Add(picture.i);
xml.Add(picture.i);
else
{
// Split this run at the point you want to insert
XElement[] splitRun = Run.SplitRun(run, index);
// Replace the origional run
run.Xml.ReplaceWith
run.xml.ReplaceWith
(
splitRun[0],
picture.i,
@@ -208,7 +271,7 @@ namespace Novacode
// Rebuild the run lookup for this paragraph
runLookup.Clear();
BuildRunLookup(p);
BuildRunLookup(xml);
DocX.RenumberIDs();
}
@@ -249,12 +312,7 @@ namespace Novacode
);
}
/// <summary>
/// Return the first Run that will be effected by an edit at the index
/// </summary>
/// <param name="index">The index of this edit</param>
/// <returns>The first Run that will be effected</returns>
public Run GetFirstRunEffectedByEdit(int index)
internal Run GetFirstRunEffectedByEdit(int index)
{
foreach (int runEndIndex in runLookup.Keys)
{
@@ -268,12 +326,7 @@ namespace Novacode
throw new ArgumentOutOfRangeException();
}
/// <summary>
/// Return the first Run that will be effected by an edit at the index
/// </summary>
/// <param name="index">The index of this edit</param>
/// <returns>The first Run that will be effected</returns>
public Run GetFirstRunEffectedByInsert(int index)
internal Run GetFirstRunEffectedByInsert(int index)
{
// This paragraph contains no Runs and insertion is at index 0
if (runLookup.Keys.Count() == 0 && index == 0)
@@ -288,13 +341,10 @@ namespace Novacode
throw new ArgumentOutOfRangeException();
}
/// <summary>
/// If the value to be inserted contains tab elements or br elements, multiple runs will be inserted.
/// </summary>
/// <param name="text">The text to be inserted, this text can contain the special characters \t (tab) and \n (br)</param>
/// <returns></returns>
private List<XElement> formatInput(string text, XElement rPr)
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");
@@ -304,7 +354,7 @@ namespace Novacode
{
XElement firstText = new XElement(DocX.w + "t", runTexts[0]);
Text.PreserveSpace(firstText);
Novacode.Text.PreserveSpace(firstText);
firstRun = new XElement(DocX.w + "r", rPr, firstText);
@@ -324,7 +374,7 @@ namespace Novacode
else
{
// Value begins or ends with a space
Text.PreserveSpace(newText);
Novacode.Text.PreserveSpace(newText);
newRun = new XElement(DocX.w + "r", rPr, tabRun, newText);
}
@@ -336,11 +386,6 @@ namespace Novacode
return newRuns;
}
/// <summary>
/// Counts the text length of an element
/// </summary>
/// <param name="run">An element</param>
/// <returns>The length of this elements text</returns>
static internal int GetElementTextLength(XElement run)
{
int count = 0;
@@ -362,14 +407,7 @@ namespace Novacode
return count;
}
/// <summary>
/// Splits an edit element at the specified run, at the specified index.
/// </summary>
/// <param name="edit">The edit element to split</param>
/// <param name="run">The run element to split</param>
/// <param name="index">The index to split at</param>
/// <returns></returns>
public XElement[] SplitEdit(XElement edit, int index, EditType type)
internal XElement[] SplitEdit(XElement edit, int index, EditType type)
{
Run run;
if(type == EditType.del)
@@ -379,11 +417,11 @@ namespace Novacode
XElement[] splitRun = Run.SplitRun(run, index);
XElement splitLeft = new XElement(edit.Name, edit.Attributes(), run.Xml.ElementsBeforeSelf(), splitRun[0]);
XElement splitLeft = new XElement(edit.Name, edit.Attributes(), run.xml.ElementsBeforeSelf(), splitRun[0]);
if (GetElementTextLength(splitLeft) == 0)
splitLeft = null;
XElement splitRight = new XElement(edit.Name, edit.Attributes(), splitRun[1], run.Xml.ElementsAfterSelf());
XElement splitRight = new XElement(edit.Name, edit.Attributes(), splitRun[1], run.xml.ElementsAfterSelf());
if (GetElementTextLength(splitRight) == 0)
splitRight = null;
@@ -397,56 +435,214 @@ namespace Novacode
);
}
public void Insert(int index, string value, bool trackChanges)
/// <summary>
/// Inserts a specified instance of System.String into a Novacode.DocX.Paragraph at a specified index position.
/// </summary>
/// <example>
/// <code>
/// // Load Test.docx.
/// DocX document = DocX.Load(@"C:\Example\Test.docx");
///
/// // Iterate through the Paragraphs in this document.
/// foreach (Paragraph p in document.Paragraphs)
/// {
/// // Insert the string "Start: " at the begining of every Paragraph and flag it as a change.
/// p.InsertText(0, "Start: ", true);
/// }
///
/// // Save changes to this document.
/// document.Close(true);
/// </code>
/// </example>
/// <example>
/// Inserting tabs using the \t switch.
/// <code>
/// // Load Test.docx.
/// DocX document = DocX.Load(@"C:\Example\Test.docx");
///
/// // Iterate through the paragraphs in this document.
/// foreach (Paragraph p in document.Paragraphs)
/// {
/// // Insert the string "\tStart:\t" at the begining of every paragraph and flag it as a change.
/// p.InsertText(0, "\tStart:\t", true);
/// }
///
/// // Save changes to this document.
/// document.Close(true);
/// </code>
/// </example>
/// <seealso cref="Paragraph.RemoveText(int, bool)"/>
/// <seealso cref="Paragraph.RemoveText(int, int, bool)"/>
/// <seealso cref="Paragraph.ReplaceText(string, string, bool)"/>
/// <seealso cref="Paragraph.ReplaceText(string, string, bool, RegexOptions)"/>
/// <param name="index">The index position of the insertion.</param>
/// <param name="value">The System.String to insert.</param>
/// <param name="trackChanges">Flag this insert as a change.</param>
public void InsertText(int index, string value, bool trackChanges)
{
Insert(index, value, null, trackChanges);
InsertText(index, value, trackChanges, null);
}
/// <summary>
/// Inserts a specified instance of System.String into a Novacode.DocX.Paragraph at a specified index position.
/// </summary>
/// <example>
/// <code>
/// // Description: Simple string insertion
///
/// // Load Example.docx
/// DocX dx = DocX.Load(@"C:\Example.docx");
/// <code>
/// // Load Test.docx.
/// DocX document = DocX.Load(@"C:\Example\Test.docx");
///
/// // Iterate through the paragraphs
/// foreach (Paragraph p in dx.Paragraphs)
/// // Iterate through the Paragraphs in this document.
/// foreach (Paragraph p in document.Paragraphs)
/// {
/// // Insert the string "Start: " at the begining of every paragraph and flag it as a change.
/// p.Insert(0, "Start: ", true);
/// // Insert the string "End: " at the end of every Paragraph and flag it as a change.
/// p.InsertText("End: ", true);
/// }
///
/// // Save changes to Example.docx
/// dx.Save();
/// // Save changes to this document.
/// document.Close(true);
/// </code>
/// </example>
/// <example>
/// <code>
/// // Description: Inserting tabs using the \t switch
/// Inserting tabs using the \t switch.
/// <code>
/// // Load Test.docx.
/// DocX document = DocX.Load(@"C:\Example\Test.docx");
///
/// // Iterate through the paragraphs in this document.
/// foreach (Paragraph p in document.Paragraphs)
/// {
/// // Insert the string "\tEnd" at the end of every paragraph and flag it as a change.
/// p.InsertText("\tEnd", true);
/// }
///
/// // Save changes to this document.
/// document.Close(true);
/// </code>
/// </example>
/// <seealso cref="Paragraph.RemoveText(int, bool)"/>
/// <seealso cref="Paragraph.RemoveText(int, int, bool)"/>
/// <seealso cref="Paragraph.ReplaceText(string, string, bool)"/>
/// <seealso cref="Paragraph.ReplaceText(string, string, bool, RegexOptions)"/>
/// <param name="value">The System.String to insert.</param>
/// <param name="trackChanges">Flag this insert as a change.</param>
public void InsertText(string value, bool trackChanges)
{
InsertText(Text.Length, value, trackChanges, null);
}
/// <summary>
/// Inserts a specified instance of System.String into a Novacode.DocX.Paragraph at a specified index position.
/// </summary>
/// <example>
/// <code>
/// // Load Test.docx.
/// DocX document = DocX.Load(@"C:\Example\Test.docx");
///
/// // Create a text formatting.
/// Formatting f = new Formatting();
/// f.FontColor = Color.Red;
/// f.Size = 30;
///
/// // Load Example.docx
/// DocX dx = DocX.Load(@"C:\Example.docx");
/// // Iterate through the Paragraphs in this document.
/// foreach (Paragraph p in document.Paragraphs)
/// {
/// // Insert the string "Start: " at the begining of every Paragraph and flag it as a change.
/// p.InsertText("Start: ", true, f);
/// }
///
/// // Save changes to this document.
/// document.Close(true);
/// </code>
/// </example>
/// <example>
/// Inserting tabs using the \t switch.
/// <code>
/// // Load Test.docx.
/// DocX document = DocX.Load(@"C:\Example\Test.docx");
///
/// // Create a text formatting.
/// Formatting f = new Formatting();
/// f.FontColor = Color.Red;
/// f.Size = 30;
///
/// // Iterate through the paragraphs in this document.
/// foreach (Paragraph p in document.Paragraphs)
/// {
/// // Insert the string "\tEnd" at the end of every paragraph and flag it as a change.
/// p.InsertText("\tEnd", true, f);
/// }
///
/// // Save changes to this document.
/// document.Close(true);
/// </code>
/// </example>
/// <seealso cref="Paragraph.RemoveText(int, bool)"/>
/// <seealso cref="Paragraph.RemoveText(int, int, bool)"/>
/// <seealso cref="Paragraph.ReplaceText(string, string, bool)"/>
/// <seealso cref="Paragraph.ReplaceText(string, string, bool, RegexOptions)"/>
/// <param name="value">The System.String to insert.</param>
/// <param name="trackChanges">Flag this insert as a change.</param>
/// <param name="formatting">The text formatting.</param>
public void InsertText(string value, bool trackChanges, Formatting formatting)
{
InsertText(Text.Length, value, trackChanges, formatting);
}
/// <summary>
/// Inserts a specified instance of System.String into a Novacode.DocX.Paragraph at a specified index position.
/// </summary>
/// <example>
/// <code>
/// // Load Test.docx.
/// DocX document = DocX.Load(@"C:\Example\Test.docx");
///
/// // Create a text formatting.
/// Formatting f = new Formatting();
/// f.FontColor = Color.Red;
/// f.Size = 30;
///
/// // Iterate through the Paragraphs in this document.
/// foreach (Paragraph p in document.Paragraphs)
/// {
/// // Insert the string "Start: " at the begining of every Paragraph and flag it as a change.
/// p.InsertText(0, "Start: ", true, f);
/// }
///
/// // Save changes to this document.
/// document.Close(true);
/// </code>
/// </example>
/// <example>
/// Inserting tabs using the \t switch.
/// <code>
/// // Load Test.docx.
/// DocX document = DocX.Load(@"C:\Example\Test.docx");
///
/// // Create a text formatting.
/// Formatting f = new Formatting();
/// f.FontColor = Color.Red;
/// f.Size = 30;
///
/// // Iterate through the paragraphs
/// foreach (Paragraph p in dx.Paragraphs)
/// // Iterate through the paragraphs in this document.
/// foreach (Paragraph p in document.Paragraphs)
/// {
/// // Insert the string "\tStart:\t" at the begining of every paragraph and flag it as a change.
/// p.Insert(0, "\tStart:\t", true);
/// p.InsertText(0, "\tStart:\t", true, f);
/// }
///
/// // Save changes to Example.docx
/// dx.Save();
/// // Save changes to this document.
/// document.Close(true);
/// </code>
/// </example>
/// <seealso cref="Paragraph.Remove"/>
/// <seealso cref="Paragraph.Replace"/>
/// <seealso cref="Paragraph.RemoveText(int, bool)"/>
/// <seealso cref="Paragraph.RemoveText(int, int, bool)"/>
/// <seealso cref="Paragraph.ReplaceText(string, string, bool)"/>
/// <seealso cref="Paragraph.ReplaceText(string, string, bool, RegexOptions)"/>
/// <param name="index">The index position of the insertion.</param>
/// <param name="value">The System.String to insert.</param>
/// <param name="trackChanges">Flag this insert as a change</param>
public void Insert(int index, string value, Formatting formatting, bool trackChanges)
/// <param name="trackChanges">Flag this insert as a change.</param>
/// <param name="formatting">The text formatting.</param>
public void InsertText(int index, string value, bool trackChanges, Formatting formatting)
{
// Timestamp to mark the start of insert
DateTime now = DateTime.Now;
@@ -459,25 +655,25 @@ namespace Novacode
{
object insert;
if (formatting != null)
insert = formatInput(value, formatting.Xml);
insert = FormatInput(value, formatting.Xml);
else
insert = formatInput(value, null);
insert = FormatInput(value, null);
if (trackChanges)
insert = CreateEdit(EditType.ins, insert_datetime, insert);
p.Add(insert);
xml.Add(insert);
}
else
{
object newRuns;
if (formatting != null)
newRuns = formatInput(value, formatting.Xml);
newRuns = FormatInput(value, formatting.Xml);
else
newRuns = formatInput(value, run.Xml.Element(XName.Get("rPr", DocX.w.NamespaceName)));
newRuns = FormatInput(value, run.xml.Element(XName.Get("rPr", DocX.w.NamespaceName)));
// The parent of this Run
XElement parentElement = run.Xml.Parent;
XElement parentElement = run.xml.Parent;
switch (parentElement.Name.LocalName)
{
case "ins":
@@ -535,7 +731,7 @@ namespace Novacode
XElement[] splitRun = Run.SplitRun(run, index);
// Replace the origional run
run.Xml.ReplaceWith
run.xml.ReplaceWith
(
splitRun[0],
insert,
@@ -549,7 +745,7 @@ namespace Novacode
// Rebuild the run lookup for this paragraph
runLookup.Clear();
BuildRunLookup(p);
BuildRunLookup(xml);
DocX.RenumberIDs();
}
@@ -558,26 +754,30 @@ namespace Novacode
/// </summary>
/// <example>
/// <code>
/// // Load Example.docx
/// DocX dx = DocX.Load(@"C:\Example.docx");
/// // Load the document Test.docx.
/// DocX document = DocX.Load(@"C:\Example\Test.docx");
///
/// // Iterate through the paragraphs
/// foreach (Paragraph p in dx.Paragraphs)
/// foreach (Paragraph p in document.Paragraphs)
/// {
/// // Remove the first two characters from every paragraph
/// p.Remove(0, 2);
/// p.RemoveText(0, 2, false);
/// }
///
/// // Save changes to Example.docx
/// dx.Save();
/// // Save changes to this document.
/// document.Close(true);
/// </code>
/// </example>
/// <seealso cref="Paragraph.Insert"/>
/// <seealso cref="Paragraph.Replace"/>
/// <seealso cref="Paragraph.ReplaceText(string, string, bool)"/>
/// <seealso cref="Paragraph.ReplaceText(string, string, bool, RegexOptions)"/>
/// <seealso cref="Paragraph.InsertText(string, bool)"/>
/// <seealso cref="Paragraph.InsertText(int, string, bool)"/>
/// <seealso cref="Paragraph.InsertText(int, string, bool, Formatting)"/>
/// <seealso cref="Paragraph.InsertText(string, bool, Formatting)"/>
/// <param name="index">The position to begin deleting characters.</param>
/// <param name="count">The number of characters to delete</param>
/// <param name="trackChanges">Track changes</param>
public void Remove(int index, int count, bool trackChanges)
public void RemoveText(int index, int count, bool trackChanges)
{
// Timestamp to mark the start of insert
DateTime now = DateTime.Now;
@@ -592,13 +792,13 @@ namespace Novacode
Run run = GetFirstRunEffectedByEdit(index + processed);
// The parent of this Run
XElement parentElement = run.Xml.Parent;
XElement parentElement = run.xml.Parent;
switch (parentElement.Name.LocalName)
{
case "ins":
{
XElement[] splitEditBefore = SplitEdit(parentElement, index + processed, EditType.del);
int min = Math.Min(count - processed, run.Xml.ElementsAfterSelf().Sum(e => GetElementTextLength(e)));
int min = Math.Min(count - processed, run.xml.ElementsAfterSelf().Sum(e => GetElementTextLength(e)));
XElement[] splitEditAfter = SplitEdit(parentElement, index + processed + min, EditType.del);
XElement temp = SplitEdit(splitEditBefore[1], index + processed + min, EditType.del)[0];
@@ -645,7 +845,7 @@ namespace Novacode
if (!trackChanges)
middle = null;
run.Xml.ReplaceWith
run.xml.ReplaceWith
(
splitRunBefore[0],
middle,
@@ -664,50 +864,115 @@ namespace Novacode
// Rebuild the run lookup
runLookup.Clear();
BuildRunLookup(p);
BuildRunLookup(xml);
DocX.RenumberIDs();
}
/// <summary>
/// Replaces all occurrences of a specified System.String in this instance, with another specified System.String.
/// Removes characters from a Novacode.DocX.Paragraph.
/// </summary>
/// <example>
/// <code>
/// // Load Example.docx
/// DocX dx = DocX.Load(@"C:\Example.docx");
/// // Load the document Test.docx.
/// DocX document = DocX.Load(@"C:\Example\Test.docx");
///
/// // Iterate through the paragraphs
/// foreach (Paragraph p in dx.Paragraphs)
/// foreach (Paragraph p in document.Paragraphs)
/// {
/// // Replace all instances of the string "wrong" with the stirng "right"
/// p.Replace("wrong", "right");
/// // Remove all but the first 2 characters from this Paragraph.
/// p.RemoveText(2, false);
/// }
///
/// // Save changes to Example.docx
/// dx.Save();
/// // Save changes to this document.
/// document.Close(true);
/// </code>
/// </example>
/// <seealso cref="Paragraph.Remove"/>
/// <seealso cref="Paragraph.Insert"/>
/// <seealso cref="Paragraph.ReplaceText(string, string, bool)"/>
/// <seealso cref="Paragraph.ReplaceText(string, string, bool, RegexOptions)"/>
/// <seealso cref="Paragraph.InsertText(string, bool)"/>
/// <seealso cref="Paragraph.InsertText(int, string, bool)"/>
/// <seealso cref="Paragraph.InsertText(int, string, bool, Formatting)"/>
/// <seealso cref="Paragraph.InsertText(string, bool, Formatting)"/>
/// <param name="index">The position to begin deleting characters.</param>
/// <param name="trackChanges">Track changes</param>
public void RemoveText(int index, bool trackChanges)
{
RemoveText(index, Text.Length - index, trackChanges);
}
/// <summary>
/// Replaces all occurrences of a specified System.String in this instance, with another specified System.String.
/// </summary>
/// <example>
/// <code>
/// // Load the document Test.docx.
/// DocX document = DocX.Load(@"C:\Example.docx");
///
/// // Iterate through the paragraphs in this document.
/// foreach (Paragraph p in document.Paragraphs)
/// {
/// // Replace all instances of the string "wrong" with the string "right" and ignore case.
/// p.ReplaceText("wrong", "right", false, RegexOptions.IgnoreCase);
/// }
///
/// // Save changes to this document.
/// document.Close(true);
/// </code>
/// </example>
/// <seealso cref="Paragraph.RemoveText(int, int, bool)"/>
/// <seealso cref="Paragraph.RemoveText(int, bool)"/>
/// <seealso cref="Paragraph.InsertText(string, bool)"/>
/// <seealso cref="Paragraph.InsertText(int, string, bool)"/>
/// <seealso cref="Paragraph.InsertText(int, string, bool, Formatting)"/>
/// <seealso cref="Paragraph.InsertText(string, bool, Formatting)"/>
/// <param name="newValue">A System.String to replace all occurances of oldValue.</param>
/// <param name="oldValue">A System.String to be replaced.</param>
/// <param name="options">A bitwise OR combination of RegexOption enumeration options.</param>
/// <param name="trackChanges">Track changes</param>
public void Replace(string oldValue, string newValue, bool trackChanges, RegexOptions options)
public void ReplaceText(string oldValue, string newValue, bool trackChanges, RegexOptions options)
{
MatchCollection mc = Regex.Matches(this.Value, Regex.Escape(oldValue), options);
MatchCollection mc = Regex.Matches(this.Text, Regex.Escape(oldValue), options);
// Loop through the matches in reverse order
foreach (Match m in mc.Cast<Match>().Reverse())
{
Insert(m.Index + oldValue.Length, newValue, trackChanges);
Remove(m.Index, m.Length, trackChanges);
InsertText(m.Index + oldValue.Length, newValue, trackChanges);
RemoveText(m.Index, m.Length, trackChanges);
}
}
public void Replace(string oldValue, string newValue, bool trackChanges)
/// <summary>
/// Replaces all occurrences of a specified System.String in this instance, with another specified System.String.
/// </summary>
/// <example>
/// <code>
/// // Load the document Test.docx.
/// DocX document = DocX.Load(@"C:\Example.docx");
///
/// // Iterate through the paragraphs in this document.
/// foreach (Paragraph p in document.Paragraphs)
/// {
/// // Replace all instances of the string "wrong" with the string "right".
/// p.ReplaceText("wrong", "right", false);
/// }
///
/// // Save changes to this document.
/// document.Close(true);
/// </code>
/// </example>
/// <seealso cref="Paragraph.RemoveText(int, int, bool)"/>
/// <seealso cref="Paragraph.RemoveText(int, bool)"/>
/// <seealso cref="Paragraph.InsertText(string, bool)"/>
/// <seealso cref="Paragraph.InsertText(int, string, bool)"/>
/// <seealso cref="Paragraph.InsertText(int, string, bool, Formatting)"/>
/// <seealso cref="Paragraph.InsertText(string, bool, Formatting)"/>
/// <param name="newValue">A System.String to replace all occurances of oldValue.</param>
/// <param name="oldValue">A System.String to be replaced.</param>
/// <param name="trackChanges">Track changes</param>
public void ReplaceText(string oldValue, string newValue, bool trackChanges)
{
Replace(oldValue, newValue, trackChanges, RegexOptions.None);
ReplaceText(oldValue, newValue, trackChanges, RegexOptions.None);
}
}
}

+ 46
- 0
DocX/Picture.cs ファイルの表示

@@ -8,6 +8,9 @@ using DocumentFormat.OpenXml.Packaging;
namespace Novacode
{
/// <summary>
/// Represents a Picture in this document, a Picture is a customized view of an Image.
/// </summary>
public class Picture
{
private string id;
@@ -24,6 +27,12 @@ namespace Novacode
private XElement xfrm;
private XElement prstGeom;
/// <summary>
/// Create a new Picture.
/// </summary>
/// <param name="id">A unique id that identifies an Image embedded in this document.</param>
/// <param name="name">The name of this Picture.</param>
/// <param name="descr">The description of this Picture.</param>
public Picture(string id, string name, string descr)
{
OpenXmlPart part = DocX.mainDocumentPart.GetPartById(id);
@@ -171,46 +180,80 @@ namespace Novacode
prstGeom.Attribute(XName.Get("prst")).Value = shape.ToString();
}
/// <summary>
/// Set the shape of this Picture to one in the BasicShapes enumeration.
/// </summary>
/// <param name="shape">A shape from the BasicShapes enumeration.</param>
public void SetPictureShape(BasicShapes shape)
{
SetPictureShape((object)shape);
}
/// <summary>
/// Set the shape of this Picture to one in the RectangleShapes enumeration.
/// </summary>
/// <param name="shape">A shape from the RectangleShapes enumeration.</param>
public void SetPictureShape(RectangleShapes shape)
{
SetPictureShape((object)shape);
}
/// <summary>
/// Set the shape of this Picture to one in the BlockArrowShapes enumeration.
/// </summary>
/// <param name="shape">A shape from the BlockArrowShapes enumeration.</param>
public void SetPictureShape(BlockArrowShapes shape)
{
SetPictureShape((object)shape);
}
/// <summary>
/// Set the shape of this Picture to one in the EquationShapes enumeration.
/// </summary>
/// <param name="shape">A shape from the EquationShapes enumeration.</param>
public void SetPictureShape(EquationShapes shape)
{
SetPictureShape((object)shape);
}
/// <summary>
/// Set the shape of this Picture to one in the FlowchartShapes enumeration.
/// </summary>
/// <param name="shape">A shape from the FlowchartShapes enumeration.</param>
public void SetPictureShape(FlowchartShapes shape)
{
SetPictureShape((object)shape);
}
/// <summary>
/// Set the shape of this Picture to one in the StarAndBannerShapes enumeration.
/// </summary>
/// <param name="shape">A shape from the StarAndBannerShapes enumeration.</param>
public void SetPictureShape(StarAndBannerShapes shape)
{
SetPictureShape((object)shape);
}
/// <summary>
/// Set the shape of this Picture to one in the CalloutShapes enumeration.
/// </summary>
/// <param name="shape">A shape from the CalloutShapes enumeration.</param>
public void SetPictureShape(CalloutShapes shape)
{
SetPictureShape((object)shape);
}
/// <summary>
/// A unique id that identifies an Image embedded in this document.
/// </summary>
public string Id
{
get { return id; }
}
/// <summary>
/// Flip this Picture Horizontally.
/// </summary>
public bool FlipHorizontal
{
get { return hFlip; }
@@ -227,6 +270,9 @@ namespace Novacode
}
}
/// <summary>
/// Flip this Picture Vertically.
/// </summary>
public bool FlipVertical
{
get { return vFlip; }

+ 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.2")]
[assembly: AssemblyFileVersion("1.0.0.2")]
[assembly: AssemblyVersion("1.0.0.4")]
[assembly: AssemblyFileVersion("1.0.0.4")]

+ 18
- 41
DocX/Run.cs ファイルの表示

@@ -6,7 +6,7 @@ using System.Xml.Linq;
namespace Novacode
{
public class Run
internal class Run
{
// A lookup for the text elements in this paragraph
Dictionary<int, Text> textLookup = new Dictionary<int, Text>();
@@ -14,7 +14,7 @@ namespace Novacode
private int startIndex;
private int endIndex;
private string text;
private XElement e;
internal XElement xml;
/// <summary>
/// Gets the start index of this Text (text length before this text)
@@ -31,44 +31,32 @@ namespace Novacode
/// </summary>
private string Value { set { value = text; } get { return text; } }
/// <summary>
/// The underlying XElement of this run
/// </summary>
public XElement Xml { get { return e; } }
/// <summary>
/// A Run can contain text, delText, br, t and rPr elements
/// </summary>
/// <param name="startIndex">The start index of this run (text length before this run)</param>
/// <param name="endIndex">The end index of this run (text length before this run, plus this runs text length)</param>
/// <param name="runText">The text value of this run</param>
/// <param name="e">The underlying XElement of this run</param>
internal Run(int startIndex, XElement e)
internal Run(int startIndex, XElement xml)
{
this.startIndex = startIndex;
this.e = e;
this.xml = xml;
// Get the text elements in this run
IEnumerable<XElement> texts = e.Descendants();
IEnumerable<XElement> texts = xml.Descendants();
int start = startIndex;
// Loop through each text in this run
foreach (XElement text in texts)
foreach (XElement te in texts)
{
switch (text.Name.LocalName)
switch (te.Name.LocalName)
{
case "tab":
{
textLookup.Add(start + 1, new Text(start, text));
Value += "\t";
textLookup.Add(start + 1, new Text(start, te));
text += "\t";
start++;
break;
}
case "br":
{
textLookup.Add(start + 1, new Text(start, text));
Value += "\n";
textLookup.Add(start + 1, new Text(start, te));
text += "\n";
start++;
break;
}
@@ -76,11 +64,11 @@ namespace Novacode
case "delText":
{
// Only add strings which are not empty
if (text.Value.Length > 0)
if (te.Value.Length > 0)
{
textLookup.Add(start + text.Value.Length, new Text(start, text));
Value += text.Value;
start += text.Value.Length;
textLookup.Add(start + te.Value.Length, new Text(start, te));
text += te.Value;
start += te.Value.Length;
}
break;
}
@@ -91,22 +79,16 @@ namespace Novacode
endIndex = start;
}
/// <summary>
/// Splits a run element at a specified index
/// </summary>
/// <param name="r">The run element to split</param>
/// <param name="index">The index to split at</param>
/// <returns>A two element array which contains both sides of the split</returns>
static internal XElement[] SplitRun(Run r, int index)
{
Text t = r.GetFirstTextEffectedByEdit(index);
XElement[] splitText = Text.SplitText(t, index);
XElement splitLeft = new XElement(r.e.Name, r.e.Attributes(), r.Xml.Element(XName.Get("rPr", DocX.w.NamespaceName)), t.Xml.ElementsBeforeSelf().Where(n => n.Name.LocalName != "rPr"), splitText[0]);
XElement splitLeft = new XElement(r.xml.Name, r.xml.Attributes(), r.xml.Element(XName.Get("rPr", DocX.w.NamespaceName)), t.xml.ElementsBeforeSelf().Where(n => n.Name.LocalName != "rPr"), splitText[0]);
if(Paragraph.GetElementTextLength(splitLeft) == 0)
splitLeft = null;
XElement splitRight = new XElement(r.e.Name, r.e.Attributes(), r.Xml.Element(XName.Get("rPr", DocX.w.NamespaceName)), splitText[1], t.Xml.ElementsAfterSelf().Where(n => n.Name.LocalName != "rPr"));
XElement splitRight = new XElement(r.xml.Name, r.xml.Attributes(), r.xml.Element(XName.Get("rPr", DocX.w.NamespaceName)), splitText[1], t.xml.ElementsAfterSelf().Where(n => n.Name.LocalName != "rPr"));
if(Paragraph.GetElementTextLength(splitRight) == 0)
splitRight = null;
@@ -120,12 +102,7 @@ namespace Novacode
);
}
/// <summary>
/// Return the first Run that will be effected by an edit at the index
/// </summary>
/// <param name="index">The index of this edit</param>
/// <returns>The first Run that will be effected</returns>
public Text GetFirstTextEffectedByEdit(int index)
internal Text GetFirstTextEffectedByEdit(int index)
{
foreach (int textEndIndex in textLookup.Keys)
{

+ 9
- 26
DocX/Text.cs ファイルの表示

@@ -6,12 +6,12 @@ using System.Xml.Linq;
namespace Novacode
{
public class Text
internal class Text
{
private int startIndex;
private int endIndex;
private string text;
private XElement e;
internal XElement xml;
/// <summary>
/// Gets the start index of this Text (text length before this text)
@@ -28,21 +28,10 @@ namespace Novacode
/// </summary>
public string Value { get { return text; } }
/// <summary>
/// The underlying XElement of this run
/// </summary>
public XElement Xml { get { return e; } }
/// <summary>
/// A Text element
/// </summary>
/// <param name="startIndex">The index this text starts at</param>
/// <param name="text">The index this text ends at</param>
/// <param name="e">The underlying xml element that this text wraps</param>
internal Text(int startIndex, XElement e)
{
this.startIndex = startIndex;
this.e = e;
this.xml = e;
switch (e.Name.LocalName)
{
@@ -78,29 +67,23 @@ namespace Novacode
}
}
/// <summary>
/// Splits a text element at a specified index
/// </summary>
/// <param name="e">The text element to split</param>
/// <param name="index">The index to split at</param>
/// <returns>A two element array which contains both sides of the split</returns>
public static XElement[] SplitText(Text t, int index)
internal static XElement[] SplitText(Text t, int index)
{
if (index < t.startIndex || index > t.EndIndex)
throw new ArgumentOutOfRangeException("index");
XElement splitLeft = null, splitRight = null;
if (t.e.Name.LocalName == "t" || t.e.Name.LocalName == "delText")
if (t.xml.Name.LocalName == "t" || t.xml.Name.LocalName == "delText")
{
// The origional text element, now containing only the text before the index point.
splitLeft = new XElement(t.e.Name, t.e.Attributes(), t.e.Value.Substring(0, index - t.startIndex));
splitLeft = new XElement(t.xml.Name, t.xml.Attributes(), t.xml.Value.Substring(0, index - t.startIndex));
if (splitLeft.Value.Length == 0)
splitLeft = null;
else
PreserveSpace(splitLeft);
// The origional text element, now containing only the text after the index point.
splitRight = new XElement(t.e.Name, t.e.Attributes(), t.e.Value.Substring(index - t.startIndex, t.e.Value.Length - (index - t.startIndex)));
splitRight = new XElement(t.xml.Name, t.xml.Attributes(), t.xml.Value.Substring(index - t.startIndex, t.xml.Value.Length - (index - t.startIndex)));
if (splitRight.Value.Length == 0)
splitRight = null;
else
@@ -110,10 +93,10 @@ namespace Novacode
else
{
if (index == t.StartIndex)
splitLeft = t.e;
splitLeft = t.xml;
else
splitRight = t.e;
splitRight = t.xml;
}
return

読み込み中…
キャンセル
保存