c# - I am merging two Word documents with OpenXML SDK but get a corrupt document when copying an image into a header -


i have code works in sorts of different situations, including when copying images body of document.

the code works when copying (adding) headers , footers 1 document other, long headers/footers being copied not contain images.

when copy header has image in it, resulting file corrupt, , when try open openxml sdk throws exception saying "compressed part has inconsistent data length". know image has created in headerpart (as against maindocumentpart when copying body).

the code merging of image looks like:

    private void addsourceimagestodestination(xelement sourcexml, openxmlpart sourcepart, openxmlpart destpart) {       foreach(xelement drawingelement in sourcexml.descendants(_mswdrawingelementname)) {          xattribute ablipembedattribute = drawingelement.descendants(_ablipelementname).first().attribute(_embedattributename);         string relationshipid = ablipembedattribute.value;         imagepart sourceimagepart = (imagepart)sourcepart.getpartbyid(relationshipid);         imagepart destinationimagepart = ((headerpart)destpart).addimagepart(sourceimagepart.contenttype);         string newrelationshipid = destpart.getidofpart(destinationimagepart);         ablipembedattribute.setvalue(newrelationshipid);          destinationimagepart.feeddata(sourceimagepart.getstream(filemode.open, fileaccess.read));       }     } 

the above called passing source , destination headerparts, , xml of source header after copied destination document. after calling above procedure, destinationheaderpart.header.save() called.

as said above, if there no images in source header, resulting document fine (i.e. when foreach doesn't find drawing elements in source xml).

i wonder, though, whether symptom of images in header perhaps red herring , real problem somewhere else.

as said in comment on question, code include images header , footer fine - did trick.

how solved problem of corrupt file code (elsewhere) creating bit of trial , error. other contributors have said, documentation around openxml is, put mildly, not good. there might resolution problem, , maybe "solution" works because of other side effects.

anyway, have code looks this:

    private memorystream _memorystream;     private wordprocessingdocument _worddocument;       ...     _worddocument = wordprocessingdocument.open(_memorystream, true);       ...       private void reopendocument() {       _worddocument.package.flush();       _worddocument.close();       memorystream newstream = new memorystream();       _memorystream.writeto(newstream);       _memorystream.close();       _memorystream = newstream;       _memorystream.position = 0l;       _worddocument = wordprocessingdocument.open(_memorystream, true);     } 

if call reopendocument method prior writing _memorystream filestream, corruption avoided.


Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -