我之前问过这个问题的一个版本,但我需要其他解决方案,所以这是一个更尖锐的问题。
我需要一个基于服务器ppt
将文件转换为文件的解决方案pdf
。
该解决方案既可以作为控制台命令触发服务位于当前 Web 服务器上,也可以集成到 Web 的 C# 代码中,也可以作为它自己的服务器。
它也不能基于 Libreoffice 或 Openoffice,因为这两个软件在转换 SmartArt 时存在问题。我目前正在使用 Libreoffice。
我尝试过将 Powerpoint 控制台命令 ( /pt
) 与 PDF 打印驱动程序 ( PDFCreator
) 结合使用,但无法通过 C# 使其工作。该问题的相关问题是这里
我尝试过 .vbs 脚本,但是它只会短暂地打开 powerpoint 窗口。
剧本来自这个答案, 在这儿。
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
我尝试过的另一种方法是这样的:这个问题类似,它会短暂地打开一个(较小的)窗口。
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);
}
}
}
}
答案1
我刚刚尝试了一个小的 vbs 脚本:
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
你说得对。你不能设置oApp.Visible = false
。对我来说,窗口非常短暂(几乎不引人注意),但对于较大的 ppt,窗口会更长。因此,如果你真的不想让 Powerpoint 可见(即使是短暂的),你就不能使用自动化(通过CreateObject
)。(顺便说一句,如果这是通过 Web 服务器在服务器上创建的,为什么短暂的窗口会成为问题??不需要对话框或交互。)
接下来我尝试PDF创建器。 (做取消选中安装此免费的 Pdf 打印机时,工具栏等,与所有软件一样。)
右键单击后Create PDF with PDFCreator
Powerpoint 窗口不可见。(甚至短暂不可见)
因此运行以下命令会导致test.pdf
我的目录中出现Documents
(默默):
"C:\Program Files\Microsoft Office\Office12\POWERPNT.exe" /PT "PDFCreator" "" "" "C:\TEMP\test.pptx"
vbs 等效项为:
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
注意所有这些双"
s。它们都是必需的。您不能使用,而""
需要使用""""
. (也包括程序周围)。
你确实需要设置 PDFCreator 创建文件无对话(见下文)。结果将是一个文件(与所需目录中的 .pptx 同名)。
顺便说一句。如果此文件是由网络访问者创建的,并且该网站访问量很大,您可能需要预先创建这些 pdf。(不确定如果您多次启动 Powerpoint 进行打印会发生什么)