DocX icon indicating copy to clipboard operation
DocX copied to clipboard

Ability to preserve formatting for CustomProperty value.

Open EduardBlees opened this issue 5 years ago • 5 comments

Issue: Formatting in CustomProperty.Value wasn't preserved, because it's added as string value to xElement.

PR: Parameter withFormatting to Xceed.Document.NET.Document.UpdateCustomPropertyValue method. When true, CustomProperty.Value is formatted using HelperFunctions.FormatInput.

EduardBlees avatar Apr 15 '20 15:04 EduardBlees

Hi EduardBlees, I can understand your point to format custom properties, but based on the sample you provided, it doesn't seems necessary to do the formatting. From v1.6, I was able to add the same custom property as you without formatting:

document.AddCustomProperty( new CustomProperty( "Formatted", $"First Line{Environment.NewLine}Second Line{Environment.NewLine}Third Line" ) );

Then displaying all the custom properties of the document with: // Display each propertie's name and value. foreach( var prop in document.CustomProperties ) { document.InsertParagraph( prop.Value.Name ).Append( " = " ).Append( prop.Value.Value.ToString() ).AppendLine(); }

showed the "Formatted" property value with the "\n" displayed in 3 lines. I'm I missing something ?

Thank you

XceedBoucherS avatar Apr 20 '20 17:04 XceedBoucherS

My bad, missed important detail in issue description - I already have a document that has some custom property, which value is inserted in a lot of places throughout the document. So, by calling AddCustomProperty with the same CustomProperty Name, I'm just updating its value, and Xceed.Document.NET.Document.UpdateCustomPropertyValue updating all insertions.

In this scenario, it's very handy to have ability to preserve formatting in Xceed.Document.NET.Document.UpdateCustomPropertyValue.

EduardBlees avatar Apr 20 '20 18:04 EduardBlees

Hi, Yes, the existing custom properties will be updated with a call to AddCustomPoperty() with the same Custom PropertyName, but what kind of formatting are you expecting. Can you provide an example of formatted value to pass to the AddCustomProperty() ? Currently you suggested some {Environment.NewLine} which doesn't seem to need a Formatting. Could you provide a complete sample please ? Thank you.

XceedBoucherS avatar Apr 20 '20 20:04 XceedBoucherS

Hi, I provided an example: https://github.com/EduardBlees/DocX/tree/formatting_example Problem is, {Environment.NewLine} needs formatting, when updating property value throughout the document.

In example branch you can find DocumentSample.FormattedCustomProperties example. Run it, you should be able to reproduce this. In /Samples/Document/Output you will find two documents - formatting_test.docx and formatting_test2.docx. It's just copies of the same document. Each document has CustomProperty named FormatProperty, with value "Initial value", inserted in document 5 times. DocumentSample.FormattedCustomProperties runs AddCustomProperty without flag on first document, and runs AddCustomProperty with flag on second document. You can see my results here: https://github.com/EduardBlees/DocX/tree/formatting_example/Xceed.Words.NET.Examples/Samples/Document/screenshots

EduardBlees avatar Apr 21 '20 09:04 EduardBlees

Hi,

Thank you for you clear explanation.

I will use your idea, but modify it to make all CustomProperties formattable. In v1.8, your issue will be fixed by default (without any bool parameter) and you will have the possibility to format (with a Formatting object) any CustomProperty value.

XceedBoucherS avatar Apr 21 '20 14:04 XceedBoucherS