Convert PPTX to SVG with C#
To convert a Microsoft PowerPoint Open XML presentation (PPTX) file to a Scalable Vector Graphics (SVG) file, use the Document Converter Services (DCS) API. This conversion involves generating a separate SVG file for each slide in a multipage presentation.
DCS doesn’t support direct PPTX-to-SVG conversion out of the box. However, you can enable this workflow by configuring the GdPicture converter to use full fidelity.
Prerequisites
Update the DCS configuration file to include a Fidelity="Full"
entry for the GdPicture converter. Duplicate the existing GdPicture
entry and modify it as demonstrated in the code snippet below:
<add key="GdPicture" description="GdPicture Converter" fidelity="Full" maxInstances="16" supportedExtensions="docx,docm,dotx,doc,dot,txt,htm,html,mht,mhtml,xml,tif,xls,rtf,odt,xlsx,xlsm,ppt,pptx,pptm,ps,msg,eml,dxf,cur,wsq,gif,bmp,cut,dds,dib,dicom,exif,exr,fax,g3,hdr,heif,heic,iff,ico,j2k,j2c,webp,jb2,jbig2,jif,jfif,jng,jp2,jpeg,jpg,jpe,koa,lbm,mng,pbm,pcd,pct,pict,pic,pcx,pdf,pfm,pgm,psd,png,pnm,ppm,ras,raw,rle,sgi,svg,tga,targa,tiff,tif,wbmp,wap,wbm,xbm,xpm" supportedOutputFormats="pdf" type="Muhimbi.DocumentConverter.WebService.GdPictureConverter,Muhimbi.DocumentConverter.WebService, Version=1.0.1.1, Culture=neutral, PublicKeyToken=c9db4759c9eaad12" />
After updating the configuration file, restart Document Converter Services.
Implementation
Use the OpenService
and CloseService
methods from the DCS client sample to send a conversion request. This method processes each slide in the PPTX file and generates a corresponding SVG output:
/// <summary> /// Convert a PPTX file into SVG file. /// </summary> /// <param name="ServiceURL">URL endpoint for the PDF converter. service</param> /// <param name="sourceFileName">Source filename</param> /// <param name="targetFolder">Target folder to receive the output file</param> static void PPTX_To_SVG(string ServiceURL, string sourceFileName, string targetFolder) { DocumentConverterServiceClient client = null; try { client = OpenService(ServiceURL); byte[] convFile = PPTX_To_PDF(client, sourceFileName); if(convFile!= null) { PDFToSVG(client, convFile, sourceFileName, targetFolder); } else { Console.WriteLine($"Conversion of {sourceFileName} to intermediate PDF failed"); } Console.WriteLine(targetFolder); } catch (FaultException<WebServiceFaultException> ex) { Console.WriteLine($"FaultException occurred: ExceptionType: {ex.Detail.ExceptionType.ToString()}"); Console.WriteLine(); Console.WriteLine($"Error Detail: {string.Join(Environment.NewLine, ex.Detail.ExceptionDetails)}"); Console.WriteLine($"Error message: {ex.Message}"); Console.WriteLine(); Console.WriteLine($"Error reason: {ex.Reason}"); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); Console.WriteLine(ex.Data.ToString()); } finally { if (client != null) { CloseService(client); } } } /// <summary> /// Convert a PPTX file into a PDF file returning the PDF as a byte array. /// </summary> /// <param name="client">Service client</param> /// <param name="sourceFileName">Source filename</param> /// <returns>PDF file as byte array</returns> static byte[] PPTX_To_PDF(DocumentConverterServiceClient client, string sourceFileName) { byte[] convFile = null; Console.WriteLine($"Converting file {sourceFileName}"); // ** Determine the source file and read it into a byte array. byte[] sourceFile = File.ReadAllBytes(sourceFileName); //** Set the absolute minimum open options. OpenOptions openOptions = new OpenOptions(); openOptions.OriginalFileName = Path.GetFileName(sourceFileName); openOptions.FileExtension = Path.GetExtension(sourceFileName); // ** Set the absolute minimum conversion settings. ConversionSettings conversionSettings = new ConversionSettings(); conversionSettings.Fidelity = ConversionFidelities.High; // Set conversion to use GDPicture engine (added entry in config) conversionSettings.Quality = ConversionQuality.OptimizeForPrint; // ** Carry out the conversion. convFile = client.Convert(sourceFile, openOptions, conversionSettings); return convFile; } /// <summary> /// Convert a PDF to a collection of SVG files. /// </summary> /// <param name="client"></param> /// <param name="sourceFile">A byte array containing a PDF file</param> /// <param name="sourceFileName">Filename of the source file that had been converted into the PDF</param> /// <param name="targetFolder">Target folder to receive the output file</param> static void PDFToSVG(DocumentConverterServiceClient client, byte[] sourceFile, string sourceFileName, string targetFolder) { // Create minimum `OpenOptions` object. OpenOptions openOptions = new OpenOptions(); openOptions.OriginalFileName = Path.GetFileName(sourceFileName); // Create minimum `PatternHighlightSettings`. PDFToOfficeSettings pDFToOfficeSettings = new PDFToOfficeSettings(); pDFToOfficeSettings.OfficeType = OfficeTypes.SVG; pDFToOfficeSettings.PageRange = "*"; // Create target folder if required if (!Directory.Exists(targetFolder)) { Directory.CreateDirectory(targetFolder); } // ** Open the service and configure the bindings. // Convert the pages to SVG files BatchResults batchResults = client.PDFToSVG(sourceFile, openOptions, pDFToOfficeSettings); // If results are returned. if (batchResults != null && batchResults.Results != null && batchResults.Results.Length > 0) { // For each result. foreach (BatchResult result in batchResults.Results) { // Get the filename. string filename = result.FileName; string destinationFileName = Path.GetFullPath(Path.Combine(targetFolder, filename)); Console.WriteLine(destinationFileName); // Write the file content to a file using (FileStream fs = File.Create(destinationFileName)) { fs.Write(result.File, 0, result.File.Length); fs.Close(); } Console.WriteLine("File converted to " + destinationFileName); } } else { Console.WriteLine("Nothing returned"); } }