Cyberiafreak

"Fortunate are those who take the first steps.” ― Paulo Coelho

Convert PPT to SVG – workaround/hack !

One of my colleague wanted to know if there is any support from MS to convert the PPT’s to SVG without going through 3’rd party lib’s. I got an idea and jumped into connect the two different PIA’s to hack around. At the end, I am able to get it what he wanted. Good hacking 🙂 – here is the poc of my sample tested roughly few times. You may have to fine tune or test thoroughly before moving to production.

Objective: PPT => SVG
Proposed idea: PPT (PIA) =>jpg => VISIO (PIA) => SVG.

  1. Create/open a PPT using Office.Interop.PowerPoint
  2. Save the PPT as Jpeg.
  3. Close the PPT
  1. Load a blank VISIO file using Office.Interop.Visio
  2. Import those exported Jpeg –from step2
  3. Export the document as SVG.

======================================
Here is the c# code.

using Office = Microsoft.Office.Core;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Visio = Microsoft.Office.Interop.Visio;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
CreatePowerPointUsingPIA();
CreateSVGfromJpegUsingVisio();

GC.Collect();
GC.WaitForPendingFinalizers();

GC.Collect();
GC.WaitForPendingFinalizers();

Console.Write(“Done.. Press any key to close”);
Console.ReadLine();
}
private static void CreatePowerPointUsingPIA()
{
try
{
PowerPoint.Application oPowerPoint = new PowerPoint.Application();

// By default PowerPoint is invisible, till you make it visible:
//oPowerPoint.Visible = Office.MsoTriState.msoFalse;

PowerPoint.Presentation oPre = oPowerPoint.Presentations.Add(Microsoft.Office.Core.MsoTriState.msoTrue);

PowerPoint.Slide oSlide = oPre.Slides.Add(1,PowerPoint.PpSlideLayout.ppLayoutText);
oSlide.Shapes[1].TextFrame.TextRange.Text = “WELCOME : Win an Xbox One with Machine Learning!”;

oSlide = oPre.Slides.Add(2, PowerPoint.PpSlideLayout.ppLayoutText);
oSlide.Shapes[1].TextFrame.TextRange.Text = @”Conclusion: Thank you”;

string fileName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + “\\SourcePPoint.pptx”;
oPre.SaveAs(fileName,PowerPoint.PpSaveAsFileType.ppSaveAsJPG,
Office.MsoTriState.msoTriStateMixed);

oPre.Close();
oPowerPoint.Quit();
}
catch (Exception ex)
{
Console.WriteLine(“PowerPoint throws the error: {0}”, ex.Message);
}
}
private static void CreateSVGfromJpegUsingVisio()
{
Visio.Application app = null;
Visio.Documents oDocs = null;
Visio.Document oDoc = null;

try
{
app = new Visio.Application();
app.Visible = false;

// Create an existing blank saved vsdx – dummy
string dummyInput = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + “\\Drawing1.vsdx”;

oDoc = app.Documents.Open(dummyInput);

//int DiagramServices = 0;
//DiagramServices = app.ActiveDocument.DiagramServicesEnabled;

string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

string[] filePaths = Directory.GetFiles(path + @”\SourcePPoint”, “*.jpg”);

int sliceCount = 1;

foreach (string str in filePaths)
{
app.ActiveWindow.Page.Import(str);
app.Settings.SVGExportFormat = Visio.VisSVGExportFormat.visSVGExcludeVisioElements;
app.ActiveWindow.Selection.DeselectAll();
app.ActiveWindow.SelectAll();
app.ActiveWindow.Selection.Export(path + sliceCount + “.svg”);
sliceCount++;
}
oDoc.Saved = true;
oDoc.Close();
app.Quit();
}
catch (Exception ex)
{
Console.WriteLine(“CreateSVGfromJpegUsingVisio throws the error: {0}”, ex.Message);
}
}
}
}
Let me know if this helps you or any correction required.

Advertisements

October 14, 2014 - Posted by | Uncategorized

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: