Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I asked a version of this question earlier, but I'm in need of other solutions, so this is a more pointed question.

I'm in need of a server-based solution for converting ppt files to pdf files.

This solution can either sit on the current web server as a console command-triggered service, it can be integrated into the C# code of the web all, or it can be it's own server.

It also can't be based off of Libreoffice or Openoffice, as those two have problems converting SmartArt. I'm currently using Libreoffice.

I've tried Powerpoint console commands (/pt) combined with a PDF print driver (PDFCreator) but I can't get that to work from C#. The related question for that issue is here

I've tried a .vbs script, but that briefly opens the powerpoint window.

The script, from this answer, is here.

Option Explicit

Sub WriteLine ( strLine )
    WScript.Stdout.WriteLine strLine
End Sub

' http://msdn.microsoft.com/en-us/library/office/aa432714(v=office.12).aspx
Const msoFalse = 0   ' False.
Const msoTrue = -1   ' True.

' http://msdn.microsoft.com/en-us/library/office/bb265636(v=office.12).aspx
Const ppFixedFormatIntentScreen = 1 ' Intent is to view exported file on screen.
Const ppFixedFormatIntentPrint = 2  ' Intent is to print exported file.

' http://msdn.microsoft.com/en-us/library/office/ff746754.aspx
Const ppFixedFormatTypeXPS = 1  ' XPS format
Const ppFixedFormatTypePDF = 2  ' PDF format

' http://msdn.microsoft.com/en-us/library/office/ff744564.aspx
Const ppPrintHandoutVerticalFirst = 1   ' Slides are ordered vertically, with the first slide in the upper-left corner and the second slide below it.
Const ppPrintHandoutHorizontalFirst = 2 ' Slides are ordered horizontally, with the first slide in the upper-left corner and the second slide to the right of it.

' http://msdn.microsoft.com/en-us/library/office/ff744185.aspx
Const ppPrintOutputSlides = 1               ' Slides
Const ppPrintOutputTwoSlideHandouts = 2     ' Two Slide Handouts
Const ppPrintOutputThreeSlideHandouts = 3   ' Three Slide Handouts
Const ppPrintOutputSixSlideHandouts = 4     ' Six Slide Handouts
Const ppPrintOutputNotesPages = 5           ' Notes Pages
Const ppPrintOutputOutline = 6              ' Outline
Const ppPrintOutputBuildSlides = 7          ' Build Slides
Const ppPrintOutputFourSlideHandouts = 8    ' Four Slide Handouts
Const ppPrintOutputNineSlideHandouts = 9    ' Nine Slide Handouts
Const ppPrintOutputOneSlideHandouts = 10    ' Single Slide Handouts

' http://msdn.microsoft.com/en-us/library/office/ff745585.aspx
Const ppPrintAll = 1            ' Print all slides in the presentation.
Const ppPrintSelection = 2      ' Print a selection of slides.
Const ppPrintCurrent = 3        ' Print the current slide from the presentation.
Const ppPrintSlideRange = 4     ' Print a range of slides.
Const ppPrintNamedSlideShow = 5 ' Print a named slideshow.

' http://msdn.microsoft.com/en-us/library/office/ff744228.aspx
Const ppShowAll = 1             ' Show all.
Const ppShowNamedSlideShow = 3  ' Show named slideshow.
Const ppShowSlideRange = 2      ' Show slide range.

'
' This is the actual script
'

Dim inputFile
Dim outputFile
Dim objPPT
Dim objPresentation
Dim objPrintOptions
Dim objFso

If WScript.Arguments.Count <> 2 Then
    WriteLine "You need to specify input and output files."
    WScript.Quit
End If

inputFile = WScript.Arguments(0)
outputFile = WScript.Arguments(1)

Set objFso = CreateObject("Scripting.FileSystemObject")

If Not objFso.FileExists( inputFile ) Then
    WriteLine "Unable to find your input file " & inputFile
    WScript.Quit
End If

If objFso.FileExists( outputFile ) Then
    WriteLine "Your output file (' & outputFile & ') already exists!"
    WScript.Quit
End If

WriteLine "Input File:  " & inputFile
WriteLine "Output File: " & outputFile

Set objPPT = CreateObject( "PowerPoint.Application" )

objPPT.Visible = True
objPPT.Presentations.Open inputFile

Set objPresentation = objPPT.ActivePresentation
Set objPrintOptions = objPresentation.PrintOptions

objPrintOptions.Ranges.Add 1,objPresentation.Slides.Count
objPrintOptions.RangeType = ppShowAll

' Reference for this at http://msdn.microsoft.com/en-us/library/office/ff746080.aspx
objPresentation.ExportAsFixedFormat outputFile, ppFixedFormatTypePDF, ppFixedFormatIntentScreen, msoTrue, ppPrintHandoutHorizontalFirst, ppPrintOutputSlides, msoFalse, objPrintOptions.Ranges(1), ppPrintAll, "Slideshow Name", False, False, False, False, False

objPresentation.Close
ObjPPT.Quit

Another method I tried has this: The problem with this is similar, it opens a (smaller) window briefly.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            String sourceFilename = args[0];
            String destinationFileName = args[1];

            if (!File.Exists(sourceFilename))
            {
                throw new FileNotFoundException(string.Format("The specified file {0} does not exist.", sourceFilename), sourceFilename);
            }

            try
            {
                Microsoft.Office.Interop.PowerPoint.Application app = new Microsoft.Office.Interop.PowerPoint.Application();

                app.Presentations.Open(sourceFilename,Microsoft.Office.Core.MsoTriState.msoFalse,Microsoft.Office.Core.MsoTriState.msoFalse,Microsoft.Office.Core.MsoTriState.msoFalse).SaveAs(destinationFileName, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF);
                app.Quit();
            }
            catch (Exception e)
            {
                throw new Exception(string.Format("Unable to convert {0} to {1}", sourceFilename, destinationFileName), e);
            }
        }
    }
}
share|improve this question
    
There a lots of ppt2pdf solutions but if the .vbs-script works (but only opens the window briefly), maybe, if you show the code, we can advise you on how to hide the window. –  Rik Oct 29 '13 at 15:04
    
@Rik There are lots of ppt2pdf solutions, but very few of them are suitable to run silently, automatically, and programmatically. The script is here. I'll post it –  Houseman Oct 29 '13 at 15:30

1 Answer 1

up vote 1 down vote accepted

I just tried with a small vbs-script:

Dim oApp, Pres
Set oApp = CreateObject("Powerpoint.Application")
Set Pres = oApp.Presentations.Open("C:\\temp\\test.pptx", , , msoFalse)
Pres.SaveAs "c:\\temp\\test.pdf", 32, True
Pres.Close
oApp.Quit

And you're right. You can't set oApp.Visible = false. For me the window was very brief (almost not noticeable) but with a larger ppt it will be longer. So if you really don't want Powerpoint visible (even briefly) you can't use automation (via CreateObject). (BTW why is a brief window a problem if this is created on a server via the web-server?? There is no dialog or interaction necessary.)

Next I tried PDFCreator. (Do uncheck the toolbars, etc when installing this free Pdf printer, as with all software.)

With right-click and Create PDF with PDFCreator the Powerpoint window was not visible. (not even briefly)

So running the following command resulted in a test.pdf in my Documents directory (silently):

"C:\Program Files\Microsoft Office\Office12\POWERPNT.exe" /PT "PDFCreator" "" "" "C:\TEMP\test.pptx"

The vbs-equivalant would be:

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.run """C:\Program Files\Microsoft Office\Office12\POWERPNT.exe"" /PT PDFCreator """" """" C:\TEMP\test.pptx"
Set objShell = Nothing

Watch all those double "s. They are all necessary. You can't use "" you need to use """". (also around the program.

You do need to set PDFCreator to create the file without dialog (see below). The result will be a file (with the same name as your .pptx in your desired directory.

BTW. If this file is created by a web-visitor and the site is heavily visited you might want to pre-create these pdfs. (Not sure what will happen if you start Powerpoint lots of times to print)

enter image description here

share|improve this answer
    
@Rick. The brief window might not end up being a problem. I've also tried PDFCreator with Powerpoint /pt, but I got errors when doing it programatically. I'm not totally sure what the problem is, but the usage is this: User logs in, user uploads a powerpoint, user is shown a series of images representing their powerpoint. –  Houseman Oct 29 '13 at 16:54
    
Mmm, i didn't get any errors. In the end i didn't even have to specify the "" "" between PDFCreator and the filename. I did have a hell of a time getting those quotes to work. Maybe that's a problem. I don't have C# so i can only test with VBScript. –  Rik Oct 29 '13 at 17:09
    
As i said i had a lot of trouble with those quotes so maybe they need to be doubled in c# too (like vbscript). I needed to double quote the "" "" too ! (see all those " in my last VBScript) While the official docs state these are necessary i couldn't find out why and for me they also work without them. But ultimately if you can live with the windows, i would. Although, if Powerpoint is executed in a different user-level (i.e. SYSTEM or ADMIN) you might not even see the open window on the desktop ;) –  Rik Oct 29 '13 at 17:22
    
I don't get errors when using this as a console command, only when I call this command through a C# Process. –  Houseman Oct 30 '13 at 21:45
    
Do you have a Norton antivirus product? If so, you might want to check if disabling the "Microsoft Office Automatic Scan" helps in this case. See here and here. (If that's the case i don't know why it would scan in the integrated solution and not in the console.) –  Rik Oct 31 '13 at 8:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.