c# - AltChunk corrupts rich text content control -
i used altchunk object re-create info docx file rich text content command in file. re-create works fine. content command cannot cast sdtelement in openxml nor contentcontrol in vsto.
this code used
sdtelement sdtelement = destinationdocument.maindocumentpart.document.body.descendants<sdtelement>().where(b => b.sdtproperties.getfirstchild<tag>() != null).firstordefault(); string altchunkid = "altchunkid" + guid.newguid().tostring(); alternativeformatimportpart chunk = destinationdocument.maindocumentpart.addalternativeformatimportpart(alternativeformatimport parttype.wordprocessingml, altchunkid); chunk.feeddata(file.open("sourcefile", filemode.openorcreate)); altchunk altchunk = new altchunk(); altchunk.id = altchunkid; sdtelement.removeallchildren(); sdtelement.append(altchunk); the first time code works fine. @ sec run first line throws unable cast exception. same problem occurs while using vsto @ client side contentcontrol object cannot hold content command in altchunk inserted. somehow procedure corrupts rich text content control.
is there doing wrong? or there improve alternative?
worddocument.maindocumentpart.document.body.descendants<sdtelement>() returns ienumerable<sdtelement> , assiging sdtelemtnt. seek using var or actual homecoming type.
update:
your code working one. doing wrong line sdtelement.removeallchildren();
an sdt element (content control) contains other elements sdtpr (content command properties), sdtcontent (the actual content within content control) etc. in below eg.
<w:sdt> <w:sdtpr> ... </w:sdtpr> <w:sdtcontent> .... </w:sdtcontent> </w:sdt> what sdtelement.removeallchildren(); doing delete within sdt element , replacing them as:
<w:sdt> <w:altchunk r:id="altchunkidffebf242-30b3-4905-bf39-fc0077be9474" /> </w:sdt> which making programme throw exception on secondrun in line destinationdocument.maindocumentpart.document.body.descendants<sdtelement>().where(b => b.sdtproperties.getfirstchild<tag>() != null).firstordefault(); replaced document sdt element has no sdtproperties , no tag or sdtcontent.
to workaround problem seek inserting altchunk block content command content element (sdtcontent) instead of sdt element straight below:
using ( filestream filestream = file.open("file.docx", filemode.open)) { chunk.feeddata(filestream); altchunk altchunk = new altchunk(); altchunk.id = altchunkid; //sdtelement.removeallchildren(); sdtelement.elements<sdtcontentblock>().firstordefault().append(altchunk); // going add together existing content. } hope helps!
c# ms-word vsto openxml
No comments:
Post a Comment