C#. Net ppt export as picture

Time:2021-9-22

Before using Microsoft Office components, I have learned many ways that I don’t need to rely on, such asSpire.PresentationAsposeFor these components, spire.presentation is very convenient to use, but the free version has a limit on the number of pictures. Aspose seems to have no simple ppt processing. It should first convert the PPT to PDF and then output it from PDF. But after all, it is a commercial project, and the charging problem can not be avoided. The developers of these components are not big manufacturers, so I will directly refuse any way of cracking and piracy(I think we should respect the fruits of labor), it’s too difficult for me to parse ppt from the bottom layer, because it involves interpretation languages. Even if it takes a long time to parse it, I have to consider platform compatibility, so I finally chose Microsoft office. In fact, the disadvantages of using office suite are also very obvious. For example, the volume is too large. Originally, only such a small function is required, but several GB of redundant parts have to be installed. The deployment environment needs to rely on Windows + office from now on. However, compared with other commercial projects, Microsoft office can use programmable components without activation (there may be a little problem after expiration), which is really a persuasive reason. There are not many functions in terms of code:

using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;

Application app = new Application();
Presentation ppt = app.Presentations.Open(fileName, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);
int slideCounts = ppt.Slides.Count + 1;
for (int i = 1; i < slideCounts; i++)
{
    string picSavePath = "D:/filename_" + i + ".jpg";
    ppt.Slides[i].Export(picSavePath, ".jpg");
}
ppt.Close();
app.Quit();

The code is actually finished here, and the added reference is in the com assemblyMicrosoft PowerPoint 15.0 Object LibraryAndMicrosoft Office 15.0 Object LibraryThe specific version number depends on the situation.
Although there are no problems with the code, or with local debugging, most of them cannot be executed when deployed in most production environments, because the calling of COM assemblies requires certain permissions. If some places are not configured properly, various errors may be encountered, and it may be difficult to see the problem in most exception messages. I deployed the program to IIS as an asp.net website. For example, the following are various exceptions I encountered:

C#. Net ppt export as picture

There may even be such mistakes that seem irrelevant:

C#. Net ppt export as picture

The solution is to change the identity of the application pool to a higher permission owner, such as localsystem; This is not fundamental, but also requires services in windows components(dcomcnfg)Set the permissions of PPT components in:

C#. Net ppt export as picture

The security tab provides permissions for administrators by default, so as long as the application pool has set localsystem, you don’t have to manage it, otherwise you can change the user’s permissions as appropriate.

C#. Net ppt export as picture

After the configuration of component services is completed, most environments can use the functions normally, but another exception is encountered in the environment I use:

C#. Net ppt export as picture

The general meaning seems to be that the type does not match. It should be that the program type of PowerPoint is inconsistent with that of another PowerPoint. This strange problem has plagued me for a long time. Later, I found that the WPS of Jinshan company provides a component called office, which has the microsoft.office.interop.powerpoint namespace_ Application type, I found that I was misled. I used Microsoft’s PowerPoint type, but quoted Jinshan’s office kernel. I don’t know whether Jinshan WPS used the Microsoft Office kernel or Jinshan WPS tampered with the Microsoft Office kernel. Anyway, I also installed Microsoft office, so I just unloaded the WPS, Just re reference all Microsoft components.

Recommended Today

Monkey patch monkey patch programming method and its application in Ruby

What ismonkey patch (Monkey Patch)? In a dynamic language, functions are added and changed without modifying the source code. Purpose of using monkey patch:1. Additional function2. Function change3. Fix program errors4. Add a hook to execute some other processing while executing a method, such as printing logs, realizing AOP, etc,5. Cache, when the amount of […]