E' ormai da un pò di tempo che Microsoft ha rilasciato questo il plugin Microsoft Save as PDF or XPS, per permettere la conversione in PDF dei documenti compatibili con i programmi del pacchetto Office 2007.
E siccome il livello di programmabilità di Office è cresciuto notevolemente, sempre attraverso questo plugin e all'installazione degli Interop Assemblies per Office 2007, è possibile effettuare le medesime conversioni via codice, sia da applicazioni ASP.NET che da applicazione Windows.
I requisiti sono:
- Microsoft Office 2007, installato sulla macchina
- Plugin "Microsoft Save as PDF or XPS"
- Office 2007 Redistributable Primary Interop Assemblies
using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
private static void ConvertPPT(object inputFile, object outputFile)
{
ApplicationClass powerpoint = new ApplicationClass();
Presentation ppt = null;
object paramMissing = Type.Missing;
try
{
PpFixedFormatType formato = PpFixedFormatType.ppFixedFormatTypePDF;
PpFixedFormatIntent intent = PpFixedFormatIntent.ppFixedFormatIntentPrint;
PpPrintRangeType range = PpPrintRangeType.ppPrintAll;
MsoTriState paramFalse = MsoTriState.msoFalse;
MsoTriState paramTrue = MsoTriState.msoTrue;
PpPrintOutputType outputType = PpPrintOutputType.ppPrintOutputSlides;
PpPrintHandoutOrder handoutOrder = PpPrintHandoutOrder.ppPrintHandoutHorizontalFirst;
ppt = powerpoint.Presentations.Open(inputFile.ToString(), paramFalse, paramFalse, paramFalse);
if (ppt != null)
{
ppt.ExportAsFixedFormat(outputFile.ToString(), formato, intent,
paramTrue, handoutOrder, outputType, paramFalse, null,
range, "", false, false, true, false, true, paramMissing);
}
}
catch (Exception exe)
{
Console.WriteLine(exe.Message + exe.StackTrace);
}
finally
{
if (ppt != null)
{
ppt.Close();
ppt = null;
}
if (powerpoint != null)
{
powerpoint.Quit();
powerpoint = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
Io ho provato il codice anche con documenti abbastanza grossi, con veramente degli ottimi risultati !