xml - ASP.NET to PowerPoint: File gets corrupted when adding image -
i have used this example when exporting data powerpoint:
i have modified generateslidesfromdb() method:
public void generateslidesfromdb() { string slidename = @"c:\users\x\desktop\output.pptx"; file.copy(@"c:\users\x\desktop\test.pptx", slidename, true); using (presentationdocument presentationdocument = presentationdocument.open(slidename, true)) { presentationpart presentationpart = presentationdocument.presentationpart; slidepart slidetemplate = (slidepart)presentationpart.getpartbyid("rid2"); string firstname = "test user"; slidepart newslide = cloneslidepart(presentationpart, slidetemplate); insertcontent(newslide, firstname); newslide.slide.save(); deletetemplateslide(presentationpart, slidetemplate); presentationpart.presentation.save(); } }
as can see overwrite placeholder "test user", , works charm. need add image (as placeholder) pptx-file.
when (and run code again) corrupted pptx-file? error message:
powerpoint removed unreadable content in output.pptx. should review presentation determine whether content unexpectedly changed or removed.
edit: if try original code (which modified since dont have adventureworks), other kind of error message:
this file may have become corrupt or damaged following reasons: third-party xml editors create files not compatible microsoft office xml specifications. file has been purposely corrupted intent harm computer or data. cautious when opening file unknown source. powerpoint can attempt recover data file, presentation data, such shapes, text,and formatting, may lost. 1 of following: if want recover data file, click yes. if not want recoverdata file, click no.
ok, sorry useless post. bad.
solution:
string imgid = "ridimg" + i; imagepart imagepart = newslide.addimagepart(imageparttype.jpeg, imgid); memorystream stream3 = new memorystream(); using (filestream file = file.open(@"c:\users\x\desktop\test.jpg", filemode.open)) { byte[] buffer = new byte[file.length]; file.read(buffer, 0, (int)file.length); stream3.write(buffer, 0, buffer.length); imagepart.feeddata(new memorystream(buffer)); } swapphoto(newslide, imgid);
Comments
Post a Comment