From fde97b6f351059dd54c4f4b89039ef42b63ecfd7 Mon Sep 17 00:00:00 2001 From: Andreas Botsikas Date: Sat, 25 May 2013 12:54:15 +0300 Subject: [PATCH 1/3] Fix duplicate custom properties Check if property exists ignoring case as it would produce a corrupted docx otherwise. --- DocX/DocX.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/DocX/DocX.cs b/DocX/DocX.cs index 2b9c010..0c4032b 100644 --- a/DocX/DocX.cs +++ b/DocX/DocX.cs @@ -3608,13 +3608,14 @@ select int.Parse(d.Attribute(XName.Get("pid")).Value) pid = pids.Max(); // Check if a custom property already exists with this name + // 2013-05-25: IgnoreCase while searching for custom property as it would produce a currupted docx. var customProperty = ( from d in customPropDoc.Descendants() - where (d.Name.LocalName == "property") && (d.Attribute(XName.Get("name")).Value == cp.Name) + where (d.Name.LocalName == "property") && (d.Attribute(XName.Get("name")).Value.Equals(cp.Name,StringComparison.InvariantCultureIgnoreCase)) select d ).SingleOrDefault(); - + // If a custom property with this name already exists remove it. if (customProperty != null) customProperty.Remove(); From 24fa2fc1d1bb89152e3964e63ce4c03c3bed80b9 Mon Sep 17 00:00:00 2001 From: Andreas Botsikas Date: Sat, 25 May 2013 13:06:35 +0300 Subject: [PATCH 2/3] Check if stream can seek This will allow devs to save the document directly on a System.Web.HttpResponseStream without the need to create yet another memory stream. --- DocX/DocX.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/DocX/DocX.cs b/DocX/DocX.cs index 0c4032b..fe9ba5f 100644 --- a/DocX/DocX.cs +++ b/DocX/DocX.cs @@ -3275,13 +3275,17 @@ select e.Attribute(r + "id").Value } else { - // Set the length of this stream to 0 - stream.SetLength(0); + if (stream.CanSeek) // 2013-05-25: Check if stream can be seeked to support System.Web.HttpResponseStream + { + // Set the length of this stream to 0 + stream.SetLength(0); - // Write to the beginning of the stream - stream.Position = 0; + // Write to the beginning of the stream + stream.Position = 0; + } memoryStream.WriteTo(stream); + memoryStream.Flush(); } #endregion } From cfbeee30f3223406974efb792481d597231c79ec Mon Sep 17 00:00:00 2001 From: Andreas Botsikas Date: Sat, 25 May 2013 15:32:33 +0300 Subject: [PATCH 3/3] If custom property value is null insert empty string This will avoid crashing the AddProperty. Another way would be to throw an application exception, but it's much more intuitive to set an empty string instead. --- DocX/DocX.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DocX/DocX.cs b/DocX/DocX.cs index fe9ba5f..3898347 100644 --- a/DocX/DocX.cs +++ b/DocX/DocX.cs @@ -3633,7 +3633,7 @@ select d new XAttribute("fmtid", "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}"), new XAttribute("pid", pid + 1), new XAttribute("name", cp.Name), - new XElement(customVTypesSchema + cp.Type, cp.Value) + new XElement(customVTypesSchema + cp.Type, cp.Value??"") ) ); @@ -3642,7 +3642,7 @@ select d customPropDoc.Save(tw, SaveOptions.None); // Refresh all fields in this document which display this custom property. - UpdateCustomPropertyValue(this, cp.Name, cp.Value.ToString()); + UpdateCustomPropertyValue(this, cp.Name, (cp.Value ?? "").ToString()); } ///