Skip to content

Ability to preserve formatting for CustomProperty value. #357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions Xceed.Document.NET/Src/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2079,7 +2079,7 @@ public void AddCoreProperty( string propertyName, string propertyValue )
/// </example>
/// <seealso cref="CustomProperty"/>
/// <seealso cref="CustomProperties"/>
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 ) ) )
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -3199,14 +3199,15 @@ internal static void UpdateCorePropertyValue( Document document, string coreProp
Document.PopulateDocument( document, document._package );
}

/// <summary>
/// Update the custom properties inside the document
/// </summary>
/// <param name="document">The Document document</param>
/// <param name="customPropertyName">The property used inside the document</param>
/// <param name="customPropertyValue">The new value for the property</param>
/// <remarks>Different version of Word create different Document XML.</remarks>
internal static void UpdateCustomPropertyValue( Document document, string customPropertyName, string customPropertyValue )
/// <summary>
/// Update the custom properties inside the document
/// </summary>
/// <param name="document">The Document document</param>
/// <param name="customPropertyName">The property used inside the document</param>
/// <param name="customPropertyValue">The new value for the property</param>
/// <param name="withFormatting">Use formatting for the new value</param>
/// <remarks>Different version of Word create different Document XML.</remarks>
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<XElement> { document._mainDoc.Root };
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 :");
Expand Down