We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Shape.ToImage in SVG format

Hello,
I need to save the individual shapes in SVG format. My understanding is that the Shape.ToImage method does not support SaveFileFormat.SVG. I have some kind of workaround for this functionality. Please let me know if it will work properly or suggest a better solution. So far it works in simple tests. Here is the code which is mostly based on your examples:

public class SvgConverter
{
    // temporary use file system
    private string _dataDir;

    public SvgConverter(string dataDir)
    {
        _dataDir = dataDir;
    }
    public void Convert(Diagram diagram, Shape shape)
    {
        // 1. create new page
        var newPage = CreateTemporaryPage(diagram);
        diagram.Pages.Add(newPage);

        // 2. copy shape to the new page to the top left corner
        shape.XForm.PinX.Value = shape.XForm.Width.Value / 2;
        shape.XForm.PinY.Value = newPage.PageSheet.PageProps.PageHeight.Value - shape.XForm.Height.Value / 2;
        newPage.Shapes.Add(shape);

        // 3. export new page to svg
        SVGSaveOptions options = new SVGSaveOptions();
        options.DefaultFont = "MS Gothic";
        options.PageIndex = diagram.Pages.Count - 1; ;
        options.PageSize = new PageSize(Properties.GetWidth(shape), Properties.GetHeight(shape));

        diagram.Save(_dataDir + shape.Name + ".svg", options);

        // 4. remove temporary page
        diagram.Pages.Remove(newPage);
    }

    private Page CreateTemporaryPage(Diagram diagram)
    {
        // Set max page ID
        int maxPageId = NewMethod(diagram);

        // Initialize a new page object
        Page newPage = new Page();

        // Set page ID
        newPage.ID = maxPageId + 1;

        // Set name
        newPage.Name = "temp_page_" + newPage.ID.ToString();

        return newPage;
    }

    private static int NewMethod(Diagram diagram)
    {
        int max = 0;
        if (diagram.Pages.Count != 0)
        {
            max = diagram.Pages[0].ID;
        }

        for (int i = 1; i < diagram.Pages.Count; i++)
        {
            if (max < diagram.Pages[i].ID)
            {
                max = diagram.Pages[i].ID;
            }
        }

        return max;
    }

Thank you,
Michael

@mtalis

An investigation ticket has been generated as DIAGRAMNET-51738 in our issue tracking system for your inquiry. We will further check details of the scenario and let you know as soon as the ticket is resolved. Please spare us little time.