Переглянути джерело

Removed Rebuild[Images, Pictures, Tables, CustomProperties], this work is now done by getter functions, this is a much cleaner approach.

Added Getter and Setter functions for Hyperlink. Also added Hyperlink properties for Document, Paragraph, Table, Row and Cell.

Added similar functions for Pictures.
master
coffeycathal_cp 15 роки тому
джерело
коміт
e379f53cd8
5 змінених файлів з 542 додано та 90 видалено
  1. 119
    80
      DocX/DocX.cs
  2. 132
    4
      DocX/Hyperlink.cs
  3. 71
    2
      DocX/Paragraph.cs
  4. 216
    0
      DocX/Table.cs
  5. 4
    4
      DocX/_BaseClasses.cs

+ 119
- 80
DocX/DocX.cs Переглянути файл

@@ -30,12 +30,6 @@ namespace Novacode
internal List<PackagePart> footers = new List<PackagePart>();
// The collection of Paragraphs in this document.
private List<Paragraph> paragraphs = new List<Paragraph>();
// A dictionary of CustomProperties in this document.
private Dictionary<string, CustomProperty> customProperties = new Dictionary<string,CustomProperty>();
// A list of Images in this document.
private List<Image> images = new List<Image>();
// A collection of Tables in this Paragraph
private List<Table> tables = new List<Table>();
#endregion
#region Internal variables defined foreach DocX object
@@ -96,7 +90,18 @@ namespace Novacode
/// </summary>
public List<Table> Tables
{
get { return tables; }
get
{
List<Table> tables =
(
from t in mainDoc.Descendants(XName.Get("body", DocX.w.NamespaceName)).Elements()
where (t.Name.LocalName == "tbl")
select new Table(this, t)
).ToList();
return tables;
}
}
/// <summary>
@@ -123,7 +128,21 @@ namespace Novacode
/// <seealso cref="Paragraph.InsertPicture"/>
public List<Image> Images
{
get { return images; }
get
{
PackagePart word_document = package.GetPart(new Uri("/word/document.xml", UriKind.Relative));
PackageRelationshipCollection imageRelationships = word_document.GetRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/image");
if (imageRelationships.Count() > 0)
{
return
(
from i in imageRelationships
select new Image(this, i)
).ToList();
}
return new List<Image>();
}
}
/// <summary>
@@ -181,7 +200,28 @@ namespace Novacode
/// <seealso cref="AddCustomProperty"/>
public Dictionary<string, CustomProperty> CustomProperties
{
get { return customProperties; }
get
{
if (package.PartExists(new Uri("/docProps/custom.xml", UriKind.Relative)))
{
PackagePart docProps_custom = package.GetPart(new Uri("/docProps/custom.xml", UriKind.Relative));
XDocument customPropDoc;
using (TextReader tr = new StreamReader(docProps_custom.GetStream(FileMode.Open, FileAccess.Read)))
customPropDoc = XDocument.Load(tr, LoadOptions.PreserveWhitespace);
// Get all of the custom properties in this document
return
(
from p in customPropDoc.Descendants(XName.Get("property", customPropertiesSchema.NamespaceName))
let Name = p.Attribute(XName.Get("name")).Value
let Type = p.Descendants().Single().Name.LocalName
let Value = p.Descendants().Single().Value
select new CustomProperty(Name, Type, Value)
).ToDictionary(p => p.Name, StringComparer.CurrentCultureIgnoreCase);
}
return new Dictionary<string, CustomProperty>();
}
}
static internal void RebuildParagraphs(DocX document)
@@ -749,9 +789,6 @@ namespace Novacode
#endregion
RebuildParagraphs(this);
RebuildTables(this);
RebuildImages(this);
RebuildCustomProperties(this);
}
/// <summary>
@@ -1003,7 +1040,7 @@ namespace Novacode
XElement newTable = CreateTable(rowCount, coloumnCount);
mainDoc.Descendants(XName.Get("body", DocX.w.NamespaceName)).First().Add(newTable);
RebuildTables(this);
RebuildParagraphs(this);
return new Table(this, newTable);
}
@@ -1106,7 +1143,7 @@ namespace Novacode
Table newTable = new Table(this, newXElement);
newTable.Design = t.Design;
RebuildTables(this);
RebuildParagraphs(this);
return newTable;
}
@@ -1151,7 +1188,6 @@ namespace Novacode
Table newTable = new Table(this, newXElement);
newTable.Design = t.Design;
tables.Add(newTable);
return newTable;
}
@@ -1213,17 +1249,8 @@ namespace Novacode
}
RebuildParagraphs(this);
RebuildTables(this);
return new Table(this, newTable);
}
static internal void RebuildTables(DocX document)
{
document.tables =
(
from t in document.mainDoc.Descendants(XName.Get("tbl", DocX.w.NamespaceName))
select new Table(document, t)
).ToList();
return new Table(this, newTable);
}
/// <summary>
@@ -1550,12 +1577,6 @@ namespace Novacode
document.mainDoc = XDocument.Load(tr, LoadOptions.PreserveWhitespace);
RebuildParagraphs(document);
RebuildTables(document);
RebuildImages(document);
#endregion
#region CustomFilePropertiesPart
RebuildCustomProperties(document);
#endregion
#region Headers
@@ -1587,41 +1608,6 @@ namespace Novacode
return document;
}
static internal void RebuildImages(DocX document)
{
PackagePart word_document = document.package.GetPart(new Uri("/word/document.xml", UriKind.Relative));
PackageRelationshipCollection imageRelationships = word_document.GetRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/image");
if (imageRelationships.Count() > 0)
{
document.images =
(
from i in imageRelationships
select new Image(document, i)
).ToList();
}
}
internal static void RebuildCustomProperties(DocX document)
{
if(document.package.PartExists(new Uri("/docProps/custom.xml", UriKind.Relative)))
{
PackagePart docProps_custom = document.package.GetPart(new Uri("/docProps/custom.xml", UriKind.Relative));
XDocument customPropDoc;
using (TextReader tr = new StreamReader(docProps_custom.GetStream(FileMode.Open, FileAccess.Read)))
customPropDoc = XDocument.Load(tr, LoadOptions.PreserveWhitespace);
// Get all of the custom properties in this document
document.customProperties =
(
from p in customPropDoc.Descendants(XName.Get("property", customPropertiesSchema.NamespaceName))
let Name = p.Attribute(XName.Get("name")).Value
let Type = p.Descendants().Single().Name.LocalName
let Value = p.Descendants().Single().Value
select new CustomProperty(Name, Type, Value)
).ToDictionary(p => p.Name, StringComparer.CurrentCultureIgnoreCase);
}
}
/// <summary>
/// Loads a document into a DocX object using a Stream.
/// </summary>
@@ -1925,6 +1911,71 @@ namespace Novacode
return h;
}
/// <summary>
/// Returns a list of Hyperlinks in this document.
/// </summary>
/// <example>
/// <code>
/// // Create a document.
/// using (DocX document = DocX.Load(@"Test.docx"))
/// {
/// // Get all of the hyperlinks in this document
/// List<Hyperlink> hyperlinks = document.Hyperlinks;
///
/// // Change the first hyperlinks text and Uri
/// Hyperlink h0 = hyperlinks[0];
/// h0.Text = "DocX";
/// h0.Uri = new Uri("http://docx.codeplex.com");
///
/// // Save this document.
/// document.Save();
/// }
/// </code>
/// </example>
public List<Hyperlink> Hyperlinks
{
get
{
List<Hyperlink> hyperlinks = new List<Hyperlink>();
foreach (Paragraph p in Paragraphs)
hyperlinks.AddRange(p.Hyperlinks);
return hyperlinks;
}
}
/// <summary>
/// Returns a list of all Pictures in a Document.
/// </summary>
/// <example>
/// Returns a list of all Pictures in a Document.
/// <code>
/// // Create a document.
/// using (DocX document = DocX.Load(@"Test.docx"))
/// {
/// // Get all of the Pictures in this Document.
/// List<Picture> pictures = document.Pictures;
///
/// // Save this document.
/// document.Save();
/// }
/// </code>
/// </example>
public List<Picture> Pictures
{
get
{
List<Picture> pictures = new List<Picture>();
foreach (Paragraph p in Paragraphs)
pictures.AddRange(p.Pictures);
return pictures;
}
}
internal void AddHyperlinkStyleIfNotPresent()
{
Uri word_styles_Uri = new Uri("/word/styles.xml", UriKind.Relative);
@@ -2028,7 +2079,7 @@ namespace Novacode
.Select(r => r.Id).First();
// Return the Image object
return images.Where(i => i.Id == id).First();
return Images.Where(i => i.Id == id).First();
}
}
}
@@ -2068,9 +2119,7 @@ namespace Novacode
}// Close the Stream to the new image.
}// Close the Stream to the new image part.
Image newImg = new Image(this, rel);
images.Add(newImg);
return newImg;
return new Image(this, rel);
}
/// <summary>
@@ -2330,16 +2379,6 @@ namespace Novacode
// Refresh all fields in this document which display this custom property.
UpdateCustomPropertyValue(this, cp.Name, cp.Value.ToString());
// Get all of the custom properties in this document
customProperties =
(
from p in customPropDoc.Descendants(XName.Get("property", customPropertiesSchema.NamespaceName))
let Name = p.Attribute(XName.Get("name")).Value
let Type = p.Descendants().Single().Name.LocalName
let Value = p.Descendants().Single().Value
select new CustomProperty(Name, Type, Value)
).ToDictionary(p => p.Name, StringComparer.CurrentCultureIgnoreCase);
}
internal static void CreateCustomPropertiesPart(DocX document)

+ 132
- 4
DocX/Hyperlink.cs Переглянути файл

@@ -3,16 +3,144 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO.Packaging;
namespace Novacode
{
/// <summary>
/// Represents a Hyperlink in a document.
/// </summary>
public class Hyperlink: DocXElement
{
private string text;
public string Text { get{return text;} set{text = value;} }
/// <summary>
/// Change the Text of a Hyperlink.
/// </summary>
/// <example>
/// Change the Text of a Hyperlink.
/// <code>
/// // Create a document.
/// using (DocX document = DocX.Load(@"Test.docx"))
/// {
/// // Get all of the hyperlinks in this document
/// List<Hyperlink> hyperlinks = document.Hyperlinks;
///
/// // Change the first hyperlinks text and Uri
/// Hyperlink h0 = hyperlinks[0];
/// h0.Text = "DocX";
/// h0.Uri = new Uri("http://docx.codeplex.com");
///
/// // Save this document.
/// document.Save();
/// }
/// </code>
/// </example>
public string Text
{
get
{
// Create a string builder.
StringBuilder sb = new StringBuilder();
private Uri uri;
public Uri Uri { get{return uri;} set{uri = value;} }
// Get all the runs in this Text.
var runs = from r in Xml.Elements()
where r.Name.LocalName == "r"
select new Run(Document, r, 0);
// Remove each run.
foreach (Run r in runs)
sb.Append(r.Value);
return sb.ToString();
}
set
{
// Get all the runs in this Text.
var runs = from r in Xml.Elements()
where r.Name.LocalName == "r"
select r;
// Remove each run.
for (int i = 0; i < runs.Count(); i++)
runs.Remove();
XElement rPr =
new XElement
(
DocX.w + "rPr",
new XElement
(
DocX.w + "rStyle",
new XAttribute(DocX.w + "val", "Hyperlink")
)
);
// Format and add the new text.
List<XElement> newRuns = DocX.FormatInput(value, rPr);
Xml.Add(newRuns);
}
}
// Return the Id of this Hyperlink.
internal string GetId()
{
return Xml.Attribute(DocX.r + "id").Value;
}
/// <summary>
/// Change the Uri of a Hyperlink.
/// </summary>
/// <example>
/// Change the Uri of a Hyperlink.
/// <code>
/// // Create a document.
/// using (DocX document = DocX.Load(@"Test.docx"))
/// {
/// // Get all of the hyperlinks in this document
/// List<Hyperlink> hyperlinks = document.Hyperlinks;
///
/// // Change the first hyperlinks text and Uri
/// Hyperlink h0 = hyperlinks[0];
/// h0.Text = "DocX";
/// h0.Uri = new Uri("http://docx.codeplex.com");
///
/// // Save this document.
/// document.Save();
/// }
/// </code>
/// </example>
public Uri Uri
{
get
{
// Get the word\document.xml part
PackagePart word_document = Document.package.GetPart(new Uri("/word/document.xml", UriKind.Relative));
// Get the Hyperlink relation based on its Id.
PackageRelationship r = word_document.GetRelationship(GetId());
// Return the Hyperlinks Uri.
return r.TargetUri;
}
set
{
// Get the word\document.xml part
PackagePart word_document = Document.package.GetPart(new Uri("/word/document.xml", UriKind.Relative));
// Get the Hyperlink relation based on its Id.
PackageRelationship r = word_document.GetRelationship(GetId());
// Get all of the information about this relationship.
TargetMode r_tm = r.TargetMode;
string r_rt = r.RelationshipType;
string r_id = r.Id;
// Delete the relationship
word_document.DeleteRelationship(r_id);
word_document.CreateRelationship(value, r_tm, r_rt, r_id);
}
}
internal Hyperlink(DocX document, XElement i): base(document, i)
{

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

@@ -31,9 +31,78 @@ namespace Novacode
private List<Picture> pictures;
/// <summary>
/// Returns a list of Pictures in this Paragraph.
/// Returns a list of all Pictures in a Paragraph.
/// </summary>
public List<Picture> Pictures { get { return pictures; } }
/// <example>
/// Returns a list of all Pictures in a Paragraph.
/// <code>
/// // Create a document.
/// using (DocX document = DocX.Load(@"Test.docx"))
/// {
/// // Get the first Paragraph in a document.
/// Paragraph p = document.Paragraphs[0];
///
/// // Get all of the Pictures in this Paragraph.
/// List<Picture> pictures = p.Pictures;
///
/// // Save this document.
/// document.Save();
/// }
/// </code>
/// </example>
public List<Picture> Pictures
{
get
{
List<Picture> pictures =
(
from p in Xml.Descendants()
where (p.Name.LocalName == "drawing")
select new Picture(Document, p)
).ToList();
return pictures;
}
}
/// <summary>
/// Returns a list of Hyperlinks in this Paragraph.
/// </summary>
/// <example>
/// <code>
/// // Create a document.
/// using (DocX document = DocX.Load(@"Test.docx"))
/// {
/// // Get the first Paragraph in this document.
/// Paragraph p = document.Paragraphs[0];
///
/// // Get all of the hyperlinks in this Paragraph.
/// List<Hyperlink> hyperlinks = paragraph.Hyperlinks;
///
/// // Change the first hyperlinks text and Uri
/// Hyperlink h0 = hyperlinks[0];
/// h0.Text = "DocX";
/// h0.Uri = new Uri("http://docx.codeplex.com");
///
/// // Save this document.
/// document.Save();
/// }
/// </code>
/// </example>
public List<Hyperlink> Hyperlinks
{
get
{
List<Hyperlink> hyperlinks =
(
from h in Xml.Elements()
where (h.Name.LocalName == "hyperlink")
select new Hyperlink(Document, h)
).ToList();
return hyperlinks;
}
}
// A collection of field type DocProperty.
private List<DocProperty> docProperties;

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

@@ -21,6 +21,39 @@ namespace Novacode
private List<Row> rows;
private int rowCount, columnCount;
/// <summary>
/// Returns a list of all Pictures in a Table.
/// </summary>
/// <example>
/// Returns a list of all Pictures in a Table.
/// <code>
/// // Create a document.
/// using (DocX document = DocX.Load(@"Test.docx"))
/// {
/// // Get the first Table in a document.
/// Table t = document.Tables[0];
///
/// // Get all of the Pictures in this Table.
/// List<Picture> pictures = t.Pictures;
///
/// // Save this document.
/// document.Save();
/// }
/// </code>
/// </example>
public List<Picture> Pictures
{
get
{
List<Picture> pictures = new List<Picture>();
foreach (Row r in Rows)
pictures.AddRange(r.Pictures);
return pictures;
}
}
/// <summary>
/// Set the direction of all content in this Table.
/// </summary>
@@ -51,6 +84,39 @@ namespace Novacode
r.SetDirection(direction);
}
/// <summary>
/// Get all of the Hyperlinks in this Table.
/// </summary>
/// <example>
/// Get all of the Hyperlinks in this Table.
/// <code>
/// // Create a document.
/// using (DocX document = DocX.Load(@"Test.docx"))
/// {
/// // Get the first Table in this document.
/// Table t = document.Tables[0];
///
/// // Get a list of all Hyperlinks in this Table.
/// List<Hyperlink> hyperlinks = t.Hyperlinks;
///
/// // Save this document.
/// document.Save();
/// }
/// </code>
/// </example>
public List<Hyperlink> Hyperlinks
{
get
{
List<Hyperlink> hyperlinks = new List<Hyperlink>();
foreach (Row r in Rows)
hyperlinks.AddRange(r.Hyperlinks);
return hyperlinks;
}
}
/// <summary>
/// If the tblPr element doesent exist it is created, either way it is returned by this function.
/// </summary>
@@ -1218,6 +1284,78 @@ namespace Novacode
select new Cell(document, c)).ToList();
}
/// <summary>
/// Returns a list of all Pictures in a Row.
/// </summary>
/// <example>
/// Returns a list of all Pictures in a Row.
/// <code>
/// // Create a document.
/// using (DocX document = DocX.Load(@"Test.docx"))
/// {
/// // Get the first Table in a document.
/// Table t = document.Tables[0];
///
/// // Get the first Row in a Table.
/// Row r = t.Rows[0];
///
/// // Get all of the Pictures in this Row.
/// List<Picture> pictures = r.Pictures;
///
/// // Save this document.
/// document.Save();
/// }
/// </code>
/// </example>
public List<Picture> Pictures
{
get
{
List<Picture> pictures = new List<Picture>();
foreach (Cell c in Cells)
pictures.AddRange(c.Pictures);
return pictures;
}
}
/// <summary>
/// Get all of the Hyperlinks in this Row.
/// </summary>
/// <example>
/// Get all of the Hyperlinks in this Row.
/// <code>
/// // Create a document.
/// using (DocX document = DocX.Load(@"Test.docx"))
/// {
/// // Get the first Table in this document.
/// Table t = document.Tables[0];
///
/// // Get the first Row in this Table.
/// Row r = t.Rows[0];
///
/// // Get a list of all Hyperlinks in this Row.
/// List<Hyperlink> hyperlinks = r.Hyperlinks;
///
/// // Save this document.
/// document.Save();
/// }
/// </code>
/// </example>
public List<Hyperlink> Hyperlinks
{
get
{
List<Hyperlink> hyperlinks = new List<Hyperlink>();
foreach (Cell c in Cells)
hyperlinks.AddRange(c.Hyperlinks);
return hyperlinks;
}
}
/// <summary>
/// Set the content direction of a single Row in a Table.
/// </summary>
@@ -1423,6 +1561,84 @@ namespace Novacode
paragraphs = xml.Elements(XName.Get("p", DocX.w.NamespaceName)).Select(p => new Paragraph(document, p, 0)).ToList();
}
/// <summary>
/// Returns a list of all Pictures in a Cell.
/// </summary>
/// <example>
/// Returns a list of all Pictures in a Cell.
/// <code>
/// // Create a document.
/// using (DocX document = DocX.Load(@"Test.docx"))
/// {
/// // Get the first Table in a document.
/// Table t = document.Tables[0];
///
/// // Get the first Row in a Table.
/// Row r = t.Rows[0];
///
/// // Get the first Cell in a Row.
/// Cell c = r.Cells[0];
///
/// // Get all of the Pictures in this Cell.
/// List<Picture> pictures = c.Pictures;
///
/// // Save this document.
/// document.Save();
/// }
/// </code>
/// </example>
public List<Picture> Pictures
{
get
{
List<Picture> pictures = new List<Picture>();
foreach (Paragraph p in Paragraphs)
pictures.AddRange(p.Pictures);
return pictures;
}
}
/// <summary>
/// Get all of the Hyperlinks in this Cell.
/// </summary>
/// <example>
/// Get all of the Hyperlinks in this Cell.
/// <code>
/// // Create a document.
/// using (DocX document = DocX.Load(@"Test.docx"))
/// {
/// // Get the first Table in this document.
/// Table t = document.Tables[0];
///
/// // Get the first Row in this Table.
/// Row r = t.Rows[0];
///
/// // Get the first Cell in this Row.
/// Cell c = r.Cells[0];
///
/// // Get a list of all Hyperlinks in this Cell.
/// List<Hyperlink> hyperlinks = c.Hyperlinks;
///
/// // Save this document.
/// document.Save();
/// }
/// </code>
/// </example>
public List<Hyperlink> Hyperlinks
{
get
{
List<Hyperlink> hyperlinks = new List<Hyperlink>();
foreach (Paragraph p in Paragraphs)
hyperlinks.AddRange(p.Hyperlinks);
return hyperlinks;
}
}
/// <summary>
/// Set the content direction of a single Cell in a Table.
/// </summary>

+ 4
- 4
DocX/_BaseClasses.cs Переглянути файл

@@ -175,7 +175,7 @@ namespace Novacode
Xml.AddAfterSelf(newTable);
XElement newlyInserted = Xml.ElementsAfterSelf().First();
DocX.RebuildTables(Document);
////DocX.RebuildTables(Document);
DocX.RebuildParagraphs(Document);
return new Table(Document, newlyInserted);
}
@@ -186,7 +186,7 @@ namespace Novacode
XElement newlyInserted = Xml.ElementsAfterSelf().First();
t.Xml = newlyInserted;
DocX.RebuildTables(Document);
//DocX.RebuildTables(Document);
DocX.RebuildParagraphs(Document);
return t;
@@ -198,7 +198,7 @@ namespace Novacode
Xml.AddBeforeSelf(newTable);
XElement newlyInserted = Xml.ElementsBeforeSelf().First();
DocX.RebuildTables(Document);
//DocX.RebuildTables(Document);
DocX.RebuildParagraphs(Document);
return new Table(Document, newlyInserted);
}
@@ -209,7 +209,7 @@ namespace Novacode
XElement newlyInserted = Xml.ElementsBeforeSelf().First();
t.Xml = newlyInserted;
DocX.RebuildTables(Document);
//DocX.RebuildTables(Document);
DocX.RebuildParagraphs(Document);
return t;

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