Showing HTML in PowerPoint

匿名 (未验证) 提交于 2019-12-03 01:00:01

问题:

My Goal: Want to show HTML in Slide which I dynamically inject into a Master Presentation.

What I've achieved So far: Converted Html to OpenXML (or WordML to be more specific) then Embedded a word object into the PowerPoint and then analyzed the structure via OpenXML SDK Productivity Tool, It has created embeddings folder which contains the document I've selected, The view which I see when I open presentation is basically an Image which is in /ppt/media/image.emf.

Now I've dynamically replaced the contents of embedded docx but how can I generate its image so that I can update the view as well?

Or is there a pain free solution?

回答1:

NOTE: For WINFORMS C#

OK, I didn't go through the pain description you wrote in your query I believe you have shed a lot of sweat to make it work, here I am sharing the pain free solution(at least it worked for me) and I believe it should work for you too.

Create a class as below: (I have taken this solution partially from some other SO, sorry don't remember the source)

Usage Explained below :

using PowerPoint = Microsoft.Office.Interop.PowerPoint;

var oPowerPoint = new PowerPoint.Application(); oPowerPoint.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;  

I had a requirement to paste the content on the active slide so I used the below code, your logic could be different depending on your need you can ignore the below code line

 var activeSlide = (PowerPoint.Slide)oPowerPoint.ActiveWindow.View.Slide; 

Feed your HTML content to the below method

HtmlFragment.CopyToClipboard(HTML CONTENT WILL COME HERE); 

Below code will paste the HTML into the active slide

oPowerPoint.ActiveWindow.View.PasteSpecial(); 


回答2:

I hope you managed to find something about your issue.

A bit late, but for future people who might come here.

This is for the HTML -> PPT part.

PowerPoint.Presentation presentation; presentation = ppApp.Presentations.Open(configuration.PPTTExportedFile, MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue);        foreach (PowerPoint.Slide slide in presentation.Slides) {     foreach (PowerPoint.Shape shape in slide.Shapes)     {         File.WriteAllText(temporaryFilePath, html);         WebsiteToImage websiteToImage = new WebsiteToImage(temporaryFilePath, @"New picture path");         websiteToImage.Generate();         slide.Shapes.AddPicture(@"picture path", MsoTriState.msoTrue, MsoTriState.msoTrue, oldshapeleft, oldshapetop, oldshapewidth, oldshapeheight);         fileToDelete.Add(temporaryFilePath);         fileToDelete.Add(@""dont forget to remove tmp files");     } } 

Convert webpage to image from ASP.NET

If you want to do any object manipulation in Word/Excel/PowerPoint, I suggest to work with

Console.Write("AlternativeText: "); Console.WriteLine(shape.AlternativeText); 

Because if you save in your original file an AlternativeText in your object, you can access it fast and you can even change the PATH into a simple variable.

And if you want for example to export a HTML table, do an image from it and change the AlternativeText in order to access it easier later, while giving it an appropriate name that you can access with a 3rd software tool, because PowerPoint doesn't support HTML Tags

Next to do:

File.Copy(WordTemplateFile, WordExportedFile, true); 

Why you want to change the original? Just make a copy, and keep it as a template which you can change at any moment while creating a new changed version from it.( good for reports )

AlternativeText is very useful if you plan to work with.

For your replacement, you might want to use NetOffice/Microsoft Office libraries.

foreach (NetOffice.WordApi.InlineShape s in docWord.InlineShapes) {     if (s.Type==NetOffice.WordApi.Enums.WdInlineShapeType.wdInlineShapePicture &&  s.AlternativeText.Contains("any pattern you are looking for"))     {                  s.Range.InsertFile();     }  } 

You loop through all your file and check if something fits your pattern.

Good luck.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!