c# - Apply style on Content Control in a Word Document using OpenXML -
i'm trying generate word documents using openxml sdk , word document generator. need apply custom style on contentcontrols (repeating section).
for recursive placeholders, use
foreach (var item in list) { var datacontext = new openxmlelementdatacontext() { element = openxmlelementdatacontext.element, datacontext = item.value }; var clonedelement = cloneelementandsetcontentinplaceholders(datacontext); setcontentofcontentcontrol(clonedelement, item.value); } openxmlelementdatacontext.element.remove();
i need apply style on element. how can ?
i try see generated code "open xml sdk 2.5 productivity tool microsoft office" inspire me:
var moduledatacontext = new openxmlelementdatacontext() { element = openxmlelementdatacontext.element, datacontext = module.valeur }; var moduleclonedelement = cloneelementandsetcontentinplaceholders(moduledatacontext); var sdtproperties1 = new sdtproperties(); var styleid1 = new styleid() { val = "formationtitre2" }; paragraphmarkrunproperties paragraphmarkrunproperties1 = new paragraphmarkrunproperties(); runfonts runfonts1 = new runfonts() { complexscripttheme = themefontvalues.minorhighansi }; paragraphmarkrunproperties1.append(runfonts1); sdtproperties1.append(styleid1); sdtproperties1.append(paragraphmarkrunproperties1); run run1 = new run() { rsidrunproperties = "00c463e5" }; runproperties runproperties1 = new runproperties(); runfonts runfonts2 = new runfonts() { complexscripttheme = themefontvalues.minorhighansi }; runproperties1.append(runfonts2); run1.append(runproperties1); moduleclonedelement.append(sdtproperties1); moduleclonedelement.append(run1);
when open generated document, have error :
we're sorry. can't open "...docx" because found problem contents.
i validate document , can see 15 errors:
i've found solution. search first paragraph , apply custom style on it.
// clone element var clonedelement = cloneelementandsetcontentinplaceholders(datacontext); // search first created paragraph on clonedelement paragraph p = clonedelement.descendants<paragraph>().firstordefault(); if (p != null) p.prependchild<paragraphproperties>(new paragraphproperties()); // paragraph properties paragraphproperties ppr = p.elements<paragraphproperties>().first(); // apply style ppr.paragraphstyleid = new paragraphstyleid { val = "formationtitre2" }; // set content of content control setcontentofcontentcontrol(clonedelement, item.value);
Comments
Post a Comment