Articles in this section

How to convert and replace EMF image to PNG with the same size during PowerPoint to PDF conversion?

Syncfusion® Presentation is a .NET PowerPoint Library used to create, read, edit, and convert PowerPoint files to PDF and images programmatically without Microsoft Office or interop dependencies. Using this library, you can convert and replace EMF images to PNG with the same size during PowerPoint to PDF conversion using C#.

The conversion of metafile image formats (WMF or EMF) to bitmap image formats (JPEG or PNG) externally uses the System.Drawing.Common package, which is only applicable in a Windows environment. For more information regarding this limitation, please refer to this link. Therefore, it is important to acknowledge that there is no cross-platform solution available for converting metafile image formats to PNG files.

Steps to convert and replace EMF images in PowerPoint to PNG with the same size using C#:

  1. Create a new C# .NET Core console application.

Console application in Visual Studio

  1. Install the Syncfusion.PresentationRenderer.Net.Core NuGet package as a reference to your .NET Core application from NuGet.org.

Note:

  • The Syncfusion.Compression library is a dependent package of the PresentationRenderer NuGet package. Once PresentationRenderer gets installed, the Compression library will automatically be installed as its dependent package.

  • Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.

Install the PresentationRenderer NuGet package in ASP.NET Core

  1. Include the following namespace in the Program.cs file:
    C#
using Syncfusion.Presentation;
using Syncfusion.PresentationRenderer;
using Syncfusion.Pdf;
using Image = System.Drawing.Image;
using ImageFormat = System.Drawing.Imaging.ImageFormat;
  1. Use the following code snippet for PowerPoint to PDF conversion.
    C#
// Open the file as Stream
using (FileStream fileStreamInput = new FileStream(@"Input.pptx", FileMode.Open, FileAccess.Read))
{
    // Open the existing PowerPoint presentation with stream.
    using (IPresentation pptxDoc = Presentation.Open(fileStreamInput))
    {
        // Convert the EMF images to PNG
        ConvertEMFToPNG(pptxDoc);
        // Initialize the PresentationRenderer to perform image conversion.
        pptxDoc.PresentationRenderer = new PresentationRenderer();

        using (MemoryStream pdfStream = new MemoryStream())
        {
            // Convert the PowerPoint document to PDF document.
            using (PdfDocument pdfDocument = PresentationToPdfConverter.Convert(pptxDoc))
            {
                // Save the converted PDF document to MemoryStream.
                pdfDocument.Save(pdfStream);
                pdfStream.Position = 0;
            }
            // Create the output PDF file stream
            using (FileStream fileStreamOutput = File.Create(@"PPTXToPDF.pdf"))
            {
                // Copy the converted PDF stream into created output PDF stream
                pdfStream.CopyTo(fileStreamOutput);
            }
        }
    }
}
  1. Use the following method to iterate through the PowerPoint file to find the EMF images.
    C#
public static void ConvertEMFToPNG(IPresentation pptxDoc)
{
    // Iterate in master slides.
    foreach (IMasterSlide masterSlide in pptxDoc.Masters)
    {
        foreach (ILayoutSlide layoutSlide in masterSlide.LayoutSlides)
        {
            // Iterate in layout slide.
            IterateShapes(layoutSlide.Shapes);
        }
        IterateShapes(masterSlide.Shapes);
    }
    // Iterate in slides.
    foreach (ISlide slide in pptxDoc.Slides)
    {
        // Iterate in layout slide.
        IterateShapes(slide.LayoutSlide.Shapes);
        IterateShapes(slide.Shapes);
    }
}
  1. Use the following method to convert and replace the EMF images to PNG image with the same size.
    C#
public static void IterateShapes(IShapes shapes)
{
   // Iterate through each shape in the slide.
   for (int i = 0; i < shapes.Count; i++)
   {
       IShape shape = (IShape)shapes[i];
       // Identify the slide item type of the shape.
       switch (shape.SlideItemType)
       {
           case SlideItemType.Picture:
               IPicture picture = shape as IPicture;
               if (picture.ImageData != null)
               {
                   Image image = Image.FromStream(new MemoryStream(picture.ImageData));
                   if (image.RawFormat.Equals(ImageFormat.Emf))
                   {
                       // Store original properties
                       double left = picture.Left;
                       double top = picture.Top;
                       double width = picture.Width;
                       double height = picture.Height;
                       // Convert EMF to PNG stream
                       MemoryStream stream;
                       stream = ConvertEmfToPng(picture.ImageData);

                       // Add replacement picture
                       IPicture newPicture = shapes.AddPicture(stream, left, top, width, height);
                       newPicture.Left = left;
                       newPicture.Top = top;
                       // Remove original EMF picture
                       shapes.Remove(picture);
                       image.Dispose();
                   }
               }
               break;
           case SlideItemType.GroupShape:
               IGroupShape groupShape = (IGroupShape)shape;
               // Iterate the shape collection in a group shape.
               IterateShapes(groupShape.Shapes);
               break;
           case SlideItemType.AutoShape:
           case SlideItemType.Placeholder:
               IShape autoShape = shape as IShape;
               // Check the fill type of the shape.
               if (autoShape.Fill.FillType == FillType.Picture)
               {
                   // Get the instance for picture Fill.
                   IPictureFill pictureFill = autoShape.Fill.PictureFill;
                   Image fillImage = Image.FromStream(new MemoryStream(pictureFill.ImageBytes));
                   if (fillImage.RawFormat.Equals(ImageFormat.Emf))
                   {
                       byte[] imageBytes = shape.Fill.PictureFill.ImageBytes;

                       using Image image = Image.FromStream(new MemoryStream(imageBytes));

                       // Process only EMF images
                       if (image.RawFormat.Guid != ImageFormat.Emf.Guid)
                           return;

                       // Convert EMF -> PNG
                       using MemoryStream pngStream = ConvertEmfToPng(imageBytes);

                       // Save existing properties
                       double left = shape.Left;
                       double top = shape.Top;
                       double width = shape.Width;
                       double height = shape.Height;

                       // Create replacement shape
                       IShape newShape = shapes.AddShape(shape.AutoShapeType, left, top, width, height);

                       // Apply PNG picture fill
                       newShape.Fill.FillType = FillType.Picture;
                       newShape.Fill.PictureFill.ImageBytes = pngStream.ToArray();

                       // Copy rotation
                       newShape.Rotation = shape.Rotation;

                       // Remove original shape
                       shapes.Remove(shape);

                   }
               }
               break;
           case SlideItemType.OleObject:
               IOleObject oleObject = shape as IOleObject;

               Image oleImage = Image.FromStream(new MemoryStream(oleObject.ImageData));
               if (oleImage.RawFormat.Equals(ImageFormat.Emf))
               {
                   double height = oleObject.Height;
                   double width = oleObject.Width;
                   double left = oleObject.Left;
                   double top = oleObject.Top;

                   // Convert preview image to PNG stream
                   MemoryStream pngStream = new MemoryStream();
                   oleImage.Save(pngStream, ImageFormat.Png);
                   pngStream.Position = 0;

                   // Get OLE data
                   byte[] objectData = oleObject.ObjectData;
                   MemoryStream objectStream = new MemoryStream(objectData);

                   // Get ProgID
                   string progID = oleObject.ProgID;

                   // Remove the existing OLE object
                   shapes.Remove(oleObject);

                   // Add a new OLE object with PNG preview image
                   IOleObject replacedOleObject = shapes.AddOleObject(pngStream, progID, objectStream);

                   // Restore position and size
                   replacedOleObject.Left = left;
                   replacedOleObject.Top = top;
                   replacedOleObject.Width = width;
                   replacedOleObject.Height = height;

                   // Dispose
                   objectStream.Dispose();
                   pngStream.Dispose();
                   oleImage.Dispose();
               }
               break;
       }
   }
}
/// <summary>
/// Convert EMF to PNG
/// </summary>
private static MemoryStream ConvertEmfToPng(byte[] emfBytes)
{
   using MemoryStream emfStream = new MemoryStream(emfBytes);
   using Metafile metafile = new Metafile(emfStream);

   GraphicsUnit pageUnit = GraphicsUnit.Pixel;
   RectangleF bounds = metafile.GetBounds(ref pageUnit);

   int width = (int)Math.Ceiling(bounds.Width);
   int height = (int)Math.Ceiling(bounds.Height);

   MemoryStream pngStream = new MemoryStream();

   using (Bitmap bitmap = new Bitmap(width, height))
   {
       bitmap.SetResolution(300, 300);

       using (Graphics graphics = Graphics.FromImage(bitmap))
       {
           graphics.Clear(Color.Transparent);

           graphics.SmoothingMode =
                           System.Drawing.Drawing2D.SmoothingMode.HighQuality;
           graphics.InterpolationMode =
               System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
           graphics.PixelOffsetMode =
               System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;

           graphics.DrawImage(
               metafile,
               new Rectangle(0, 0, width, height));
       }

       bitmap.Save(pngStream, ImageFormat.Png);
   }

   pngStream.Position = 0;
   return pngStream;

}

You can download a complete working sample from GitHub.

Take a moment to peruse the documentation, where you can find basic presentation document processing options along with features like clone and merge slides and encrypt and decrypt PowerPoint presentations and most importantly PDF and image conversion with code examples.

Explore more about the rich set of Syncfusion® PowerPoint Framework features.

Conclusion

I hope you enjoyed learning how to convert and replace EMF images in PPTX to PNG with the same size during PowerPoint to PDF conversion using C#.

You can refer to our .NET PowerPoint Library feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied