Wednesday, 15 June 2011

c# - How to keep style on open xml documents -


I open XML (Microsoft Word - .Docx) as a file template to automatically generate other documents I am using. I have defined the content controls in the template document, and I have written the code to change the contents in the control of these materials. I

The content has been changed and the documents have been prepared, but I I am struggling to keep. In the Word, while inspecting the properties of content control, I have checked the checkbox for the "Use a style to format text in blank control" style, and "Remove content control when content is removed" Also checked for Documents do not have any effect when generated by code.

Here I set properties of content control in the word This is my code (Which was enough to help with a community member here) What should I do to keep the formatting for replacing the data in content control? Formatting is a simple text formatting, such as size and font Please suggest:

  change the private static zero copy (main documentpart main part, string tag name, string tag well) {// all tag field square tags Filters = Main Part Document. Body. Descendants & lt; SdBlock & gt; (). Where (r = & gt; rsdtproperties.getfarst chald & lt; tag & gt; () .val == tagname); Foreach (var field in tagFields) {// remove all paragraphs from the content block area. SDtContentBlock.RemoveAllChildren & lt; DocumentFormat.OpenXml.Wordprocessing.Paragraph & gt; (); // A new paragraph in which one run and one text element var new paragraph = new document format OpenXil Word Processing Make paragraph () Var newRun = New DocumentFormat.OpenXml.Wordprocessing.Run (); Var newText = new DocumentFormat.OpenXml.Wordprocessing.Text (tagValue); NewRun.Append (newText); NewParagraph.Append (newRun); // Add new paragraph in the content block field. SDtContentBlock.Append (newParagraph); }}  

When you specify a style to the content, a new RunProperties are added under the element SdTProperties . For example, if I allocate a new style named Style1 , then I can see that the following XML is ready:

  & lt; W: sdt & gt; & Lt; W: sdtPr & gt; & Lt; W: RPR & gt; & Lt; W: rStyle w: val = "style1" /> & Lt; / W: RPR & gt; & Lt; W: nickname w: val = "lastname" /> & Lt; W: tag w: val = "lastname" /> You need to capture this value and assign it to the new  paragraph  You can remove the same level as  SdtBlock  and then remove  SdtBlock , which does the same when you select the option to "Delete content off delete content option".  RunProperties  are  & lt; W: rPr & gt;  element The following should be done by you. 

  Private Static void ReplaceTags (MainDocumentPart mainPart, string tag name, string tag value) {// All tag fields are IEnumerable & lt; SdTBlock & gt; Tagfild = Menpart Document Body. Descendants & lt; STBLock & gt; (). Where (R = & gt; RSDTProperties.Getfest Chald & lt; tag & gt; () .val == tagname); Ingrad (different fields in the tagged field) {// SDTBlockOk RunPropertiesRunProp = Fields let's grab run properties. SdTProperties.GetFirstChild & lt; RunProperties & gt; (); // Create a new paragraph that contains one run and one text element paragraph paragraph = new paragraph (); Run New = New Run (); If (runProp! = Null) {// Assign RunProperties to our new run newRun.Append (runProp.CloneNode (true)); } Text new text = new text (tag value); NewRun.Append (newText); NewParagraph.Append (newRun); // Insert new paragraphs before the field that we are about to remove the field. Parents. Application (previously, new paragraph, field); Remove // ​​remove SdTBlock to copy the content control when the content is edited Option Field Remove (); }}  

No comments:

Post a Comment