PPTX to Image conversion doesn't work on windo

2019-03-05 19:48发布

问题:

We have requirement to convert uploaded PPT or PPTX files into image files. We developed this working locally with following (POC code):

Application pptApplication = new Application();
Microsoft.Office.Interop.PowerPoint.Presentation pptPresentation = pptApplication.Presentations.Open2007(filePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
List<string> files = new List<string>();
for (int i = 1; i <= pptPresentation.Slides.Count; i++)
{
    pptPresentation.SaveCopyAs(serverPath + randomId, PpSaveAsFileType.ppSaveAsPNG, MsoTriState.msoTrue);
    files.Add(root + "Uploads/Slide" + i + ".PNG");
}
pptPresentation.Close();

Now when this code is deployed on Windows Server 2012 R2, I receive following error:

This error looks like some access permissions issue and when I googled I found several solutions which I tried with no luck, here's some of them:

  1. Install office on server - not make any sense to have office on server :( well I installed and still getting same issue.

  2. Install office Interop Assemblies on server - i can't find this assembly for windows server 2012, the one i found here https://www.microsoft.com/en-us/download/details.aspx?id=3508 is not supported for 2012 and when I installed it doesn't work.

  3. Tried this solution https://stackoverflow.com/a/30117146 as well

  4. We can't switch to paid solutions like Aspose, Spire etc

Any help on this thread is highly appreciated. Thanks.

回答1:

Here's the narrowed down solution which worked on all the servers, i'm posting this solution after 7 months mature research.

  1. Installed Office and activated the product on every servers
  2. Created a folder 'Desktop' at C:\Windows\SysWOW64\config\systemprofile\Desktop (64 bit OS)
  3. In IIS changed ApplicationPool identity from 'ApplicationPoolIdentity' to 'LocalSystem'.

That's it and i was able to convert slides into images.

Source Code

In case you are interested in code that I was using:

Application pptApplication = new Application();
Microsoft.Office.Interop.PowerPoint.Presentation pptPresentation = pptApplication.Presentations.Open2007(Server.MapPath("~/tempslides/pptfilename.pptx"), MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
List<string> files = new List<string>();
for (int i = 1; i <= pptPresentation.Slides.Count; i++)
{
    pptPresentation.SaveCopyAs(serverPath + randomId, PpSaveAsFileType.ppSaveAsPNG, MsoTriState.msoTrue);
    files.Add(Server.MapPath("~/tempslides") + "/slide" + i + ".PNG");
}
pptPresentation.Close();

To run above code, you need to add reference to interop lib in your project.

Hope this helps you to save your time.