From 631103568eacb19cb8d1ff2cebe49064ef360301 Mon Sep 17 00:00:00 2001 From: Eduard Blees Date: Wed, 15 Apr 2020 18:03:05 +0300 Subject: [PATCH] UpdateCustomPropertyValue, added withFormatting param. Ability to preserve formatting for CustomProperty value. --- Xceed.Document.NET/Src/Document.cs | 30 ++++++++++++------- .../Samples/Document/DocumentSample.cs | 1 + 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/Xceed.Document.NET/Src/Document.cs b/Xceed.Document.NET/Src/Document.cs index 302e233a..ce759a71 100644 --- a/Xceed.Document.NET/Src/Document.cs +++ b/Xceed.Document.NET/Src/Document.cs @@ -2079,7 +2079,7 @@ public void AddCoreProperty( string propertyName, string propertyValue ) /// /// /// - public void AddCustomProperty( CustomProperty cp ) + public void AddCustomProperty( CustomProperty cp, bool withFormatting = false) { // If this document does not contain a customFilePropertyPart create one. if( !_package.PartExists( new Uri( "/docProps/custom.xml", UriKind.Relative ) ) ) @@ -2142,7 +2142,7 @@ select d } // Refresh all fields in this document which display this custom property. - Document.UpdateCustomPropertyValue( this, cp.Name, ( cp.Value ?? "" ).ToString() ); + Document.UpdateCustomPropertyValue( this, cp.Name, ( cp.Value ?? "" ).ToString(), withFormatting ); } public override Paragraph InsertParagraph() @@ -3199,14 +3199,15 @@ internal static void UpdateCorePropertyValue( Document document, string coreProp Document.PopulateDocument( document, document._package ); } - /// - /// Update the custom properties inside the document - /// - /// The Document document - /// The property used inside the document - /// The new value for the property - /// Different version of Word create different Document XML. - internal static void UpdateCustomPropertyValue( Document document, string customPropertyName, string customPropertyValue ) + /// + /// Update the custom properties inside the document + /// + /// The Document document + /// The property used inside the document + /// The new value for the property + /// Use formatting for the new value + /// Different version of Word create different Document XML. + internal static void UpdateCustomPropertyValue( Document document, string customPropertyName, string customPropertyValue, bool withFormatting = false ) { // A list of documents, which will contain, The Main Document and if they exist: header1, header2, header3, footer1, footer2, footer3. var documents = new List { document._mainDoc.Root }; @@ -3276,7 +3277,14 @@ internal static void UpdateCustomPropertyValue( Document document, string custom { if( !found ) { - match.First().Value = customPropertyValue; + var first = match.First(); + if (withFormatting) + { + first.Value = "\n"; + first.Add(HelperFunctions.FormatInput(customPropertyValue, null)); + } + else + first.Value = customPropertyValue; found = true; } else diff --git a/Xceed.Words.NET.Examples/Samples/Document/DocumentSample.cs b/Xceed.Words.NET.Examples/Samples/Document/DocumentSample.cs index b80db6dc..0f7f4b20 100644 --- a/Xceed.Words.NET.Examples/Samples/Document/DocumentSample.cs +++ b/Xceed.Words.NET.Examples/Samples/Document/DocumentSample.cs @@ -126,6 +126,7 @@ public static void AddCustomProperties() document.AddCustomProperty( new CustomProperty( "Product", "Xceed Words for .NET" ) ); document.AddCustomProperty( new CustomProperty( "Address", "3141 Taschereau, Greenfield Park" ) ); document.AddCustomProperty( new CustomProperty( "Date", DateTime.Now ) ); + document.AddCustomProperty( new CustomProperty("Formatted", $"First Line{Environment.NewLine}Second Line{Environment.NewLine}Third Line"), true ); // Add a paragraph displaying the number of custom properties. var p = document.InsertParagraph( "This document contains " ).Append( document.CustomProperties.Count.ToString() ).Append(" Custom Properties :");