PowerPoint to Markdown Conversion in .NET PowerPoint
16 Sep 202615 minutes to read
Markdown is a lightweight markup language that adds formatting elements to plain text documents. The .NET PowerPoint Library supports the conversion of a PowerPoint Presentation to a Markdown file, which mostly follows the CommonMark specification and GitHub-flavored syntax.
Assemblies and NuGet packages required
Refer to the following links for assemblies and NuGet packages required based on platforms to convert a PowerPoint Presentation document to a Markdown file using the .NET PowerPoint Library.
Convert PowerPoint to Markdown
Convert an existing PowerPoint Presentation document into a Markdown file using the .NET PowerPoint Library.
The following code example shows how to convert a PowerPoint Presentation document to a Markdown file.
//Open an existing Presentation document.
using (IPresentation presentation = Presentation.Open("Input.pptx"))
{
//Save the PowerPoint Presentation as a Markdown file.
presentation.Save("PPTXtoMd.md");
}//Open an existing Presentation document.
using (IPresentation presentation = Presentation.Open("Input.pptx"))
{
//Save the PowerPoint Presentation as a Markdown file.
presentation.Save("PPTXtoMd.md");
}' Open an existing Presentation document
Using presentation As IPresentation = Presentation.Open("Input.pptx")
' Save the Presentation document as a Markdown file
presentation.Save("PPTXtoMd.md")
End Using// Open the file as Stream
using (Stream pptStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Input.pptx"))
{
// Load the stream into a Presentation document
using (IPresentation presentation = Presentation.Open(pptStream))
{
// Save as Markdown into MemoryStream
MemoryStream stream = new MemoryStream();
presentation.Save(stream);
// Save the stream as a Markdown file locally
Save(stream, "PPTXtoMd.md");
}
}
// Saves the Markdown file
async void Save(MemoryStream streams, string filename)
{
streams.Position = 0;
StorageFile stFile;
if (!Windows.Foundation.Metadata.ApiInformation.IsTypePresent(
"Windows.Phone.UI.Input.HardwareButtons"))
{
FileSavePicker savePicker = new FileSavePicker();
savePicker.DefaultFileExtension = ".md";
savePicker.SuggestedFileName = filename;
savePicker.FileTypeChoices.Add("Markdown Files", new List<string>() { ".md" });
stFile = await savePicker.PickSaveFileAsync();
}
else
{
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
stFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
}
if (stFile != null)
{
using (IRandomAccessStream zipStream = await stFile.OpenAsync(FileAccessMode.ReadWrite))
{
using (Stream outstream = zipStream.AsStreamForWrite())
{
byte[] buffer = streams.ToArray();
outstream.Write(buffer, 0, buffer.Length);
outstream.Flush();
}
}
}
// Launch the saved Markdown file
await Windows.System.Launcher.LaunchFileAsync(stFile);
}You can download a complete working sample from GitHub.
Preserve Visual Elements as Images
The .NET PowerPoint (Presentation) library preserves PowerPoint elements such as Chart, SmartArt, GroupShape, and OLE Object as fallback images when converting a PowerPoint presentation to a Markdown file. This ensures that content which does not have a direct Markdown equivalent is still retained in the output as an image.
The following code example shows how to preserve charts and other PowerPoint elements as fallback images while converting a PowerPoint presentation to a Markdown file.
// Open an existing PowerPoint Presentation.
using (IPresentation presentation = Presentation.Open("Input.pptx"))
{
// Initialize the PresentationRenderer to preserve PowerPoint elements (including charts) as images.
presentation.PresentationRenderer = new PresentationRenderer();
// Save the PowerPoint Presentation as a Markdown file.
presentation.Save("Output.md");
}// Open an existing PowerPoint Presentation.
using (IPresentation presentation = Presentation.Open("Input.pptx"))
{
// Initialize the chart-to-image converter to preserve charts as images.
presentation.ChartToImageConverter = new ChartToImageConverter();
// SmartArt, GroupShape, and OLE Object are preserved automatically on Windows.
// Save the PowerPoint Presentation as a Markdown file.
presentation.Save("Output.md");
}'Open an existing PowerPoint Presentation.
Using presentation As IPresentation = Presentation.Open("Input.pptx")
'Initialize the chart-to-image converter to preserve charts as images.
presentation.ChartToImageConverter = New ChartToImageConverter()
'SmartArt, GroupShape, and OLE Object are preserved automatically on Windows.
'Save the PowerPoint Presentation as a Markdown file.
presentation.Save("Output.md")
End Using
- On Windows, install the Syncfusion.OfficeChartToImageConverter.WPF NuGet package and use the
ChartToImageConverterto preserve charts as fallback images. The remaining PowerPoint elements (SmartArt, GroupShape, and OLE Object) are preserved automatically as images without any additional code.- On cross-platform (ASP.NET Core, Blazor, .NET MAUI, and WinUI), install either the Syncfusion.PresentationRenderer.Net.Core NuGet package or the Syncfusion.PresentationRenderer.NET NuGet package based on your project requirements, and use the
PresentationRendererto preserve all supported PowerPoint elements as fallback images.- Preserving PowerPoint elements as fallback images is not supported on the UWP platform.
Save Options
When converting a PowerPoint Presentation to Markdown, the .NET PowerPoint Library provides various save options to customize the output Markdown file. These options allow you to customize image paths, set character encoding, and other export behaviors.
When converting a PowerPoint Presentation to a Markdown using the Save(fileName) overloads, the library creates a new folder parallel to the output file name and exports all the images into it as default.
When converting a PowerPoint Presentation to a Markdown using the Save(Stream) overloads, the library preserves the images as base64 format in the output Markdown file as default.
You can customize the above default behaviors using the following options in the .NET PowerPoint Library.
Customize the image path
The .NET PowerPoint Library provides an ImageNodeVisited event, which is used to customize the image path to set in the output Markdown file and save images externally while converting a PowerPoint Presentation to a Markdown.
The following code example shows how to save image files during a PowerPoint to Markdown conversion.
// Open an existing Presentation document.
using (IPresentation presentation = Presentation.Open("Input.pptx"))
{
// Hook the event to customize the image.
presentation.MdSaveOptions.ImageNodeVisited += SaveImage;
// Save the PowerPoint Presentation as a Markdown file.
presentation.Save("Output.md");
}// Open an existing Presentation document.
using (IPresentation presentation = Presentation.Open("Input.pptx"))
{
// Hook the event to customize the image.
presentation.MdSaveOptions.ImageNodeVisited += SaveImage;
// Save the PowerPoint Presentation as a Markdown file.
presentation.Save("Output.md");
}'Open an existing Presentation document.
Using presentation As IPresentation = Presentation.Open("Input.pptx")
'Hook the event to customize the image.
AddHandler presentation.MdSaveOptions.ImageNodeVisited, AddressOf SaveImage
'Save the Presentation document as a Markdown file.
presentation.Save("Output.md")
End UsingThe following code examples show the event handler to customize the image path and save the image in an external folder.
static void SaveImage(object sender, MdImageNodeVisitedEventArgs args)
{
string imagepath = @"D:\Temp\Image1.png";
// Save the image stream as a file.
using (FileStream fileStreamOutput = File.Create(imagepath))
args.ImageStream.CopyTo(fileStreamOutput);
// Set the URI to be used for the image in the output Markdown.
args.Uri = imagepath;
}static void SaveImage(object sender, MdImageNodeVisitedEventArgs args)
{
string imagepath = @"D:\Temp\Image1.png";
// Save the image stream as a file.
using (FileStream fileStreamOutput = File.Create(imagepath))
args.ImageStream.CopyTo(fileStreamOutput);
// Set the URI to be used for the image in the output Markdown.
args.Uri = imagepath;
}Private Shared Sub SaveImage(ByVal sender As Object, ByVal args As MdImageNodeVisitedEventArgs)
Dim imagepath = "D:\Temp\Image1.png"
'Save the image stream as a file.
Using fileStreamOutput = File.Create(imagepath)
args.ImageStream.CopyTo(fileStreamOutput)
End Using
'Set the URI to be used for the image in the output markdown.
args.Uri = imagepath
End SubYou can download a complete working sample from GitHub.
Encoding
The .NET PowerPoint Library provides an Encoding property to specify the character encoding to use when saving the Markdown file. This property is useful when you need to save the output Markdown file with specific character encodings such as UTF-8, UTF-16, ASCII, or other encodings.
The following code example shows how to save a Markdown file with a specific encoding.
// Open an existing Presentation document.
using (IPresentation presentation = Presentation.Open("Input.pptx"))
{
// Set the encoding for the Markdown file.
presentation.MdSaveOptions.Encoding = Encoding.ASCII;
// Save the PowerPoint Presentation as a Markdown file.
presentation.Save("Output.md");
}// Open an existing Presentation document.
using (IPresentation presentation = Presentation.Open("Input.pptx"))
{
// Set the encoding for the Markdown file.
presentation.MdSaveOptions.Encoding = Encoding.ASCII;
// Save the PowerPoint Presentation as a Markdown file.
presentation.Save("Output.md");
}'Open an existing Presentation document.
Using presentation As IPresentation = Presentation.Open("Input.pptx")
'Set the encoding for the Markdown file.
presentation.MdSaveOptions.Encoding = Encoding.ASCII
'Save the PowerPoint Presentation as a Markdown file.
presentation.Save("Output.md")
End UsingYou can download a complete working sample from GitHub.
Set the encoding value before saving the document as per the above code example.
Get Markdown document
Convert an existing PowerPoint Presentation to a MarkdownDocument instance using the GetMarkdownDocument API. This allows you to access and manipulate the Markdown DOM before saving or further processing.
The following code example shows how to convert a PowerPoint Presentation to a Markdown document instance.
//Open an existing Presentation document.
using (IPresentation presentation = Presentation.Open(Path.GetFullPath("Data/Input.pptx")))
{
//Convert the PowerPoint Presentation to Markdown.
MarkdownDocument markdownDocument = presentation.GetMarkdownDocument();
//Save or process the Markdown document as needed.
markdownDocument.Save(Path.GetFullPath("Output/Output.md"));
//Dispose the Markdown document.
markdownDocument.Dispose();
}//Open an existing Presentation document.
using (IPresentation presentation = Presentation.Open("Input.pptx"))
{
//Convert the PowerPoint Presentation to Markdown.
MarkdownDocument markdownDocument = presentation.GetMarkdownDocument();
//Save or process the Markdown document as needed.
markdownDocument.Save("Output.md");
//Dispose the Markdown document.
markdownDocument.Dispose();
}'Open an existing Presentation document.
Using presentation As IPresentation = Presentation.Open("Input.pptx")
'Convert the PowerPoint Presentation to Markdown.
Dim markdownDocument As MarkdownDocument = presentation.GetMarkdownDocument()
'Save or process the Markdown document as needed.
markdownDocument.Save("Output.md")
'Dispose the Markdown document.
markdownDocument.Dispose()
End UsingYou can download a complete working sample from GitHub.