70001376 Export SVG file with background color

Article 70001376
Type HowTo
Product Engine
Version 7
Date Added 8/2/2018
Fixed 7.7016.0.2 (8/2/2018)
Submitted by EVGET

Summary

Is it possible to export SVG file with background color different than white?

Solution

This is possible with some lines of code that will instruct the VDF components to use the Palette’s background color in the OnDrawBackground event, like:

// the form conatins a vdFramedControl and a Button

bool isOnSVGSave = false; // use this global boolean in the form and make it true just before saving the SVG and then again to false after save is finished

private void button1_Click(object sender, EventArgs e)
{
    vdDocument doc = vdFramedControl.BaseControl.ActiveDocument;

    doc.Open(@"C:\test\simple1.vdml"); // open a test file
    doc.Palette.Background = Color.LightYellow; // change the background color
    doc.Palette.Forground = Color.DarkSlateGray; // and the foreground color

.....
.....

    isOnSVGSave = true; //set this flag to true before saving SVG
    doc.OnDrawBackground += new vdDocument.DrawBackgroundEventHandler(doc_OnDrawBackground); // enable the event
    doc.SaveAs(@"c:\test\svg1.svg"); // save the SVG
    doc.OnDrawBackground -= new vdDocument.DrawBackgroundEventHandler(doc_OnDrawBackground); // disable the event
    isOnSVGSave = false;//set this flag back to false after saving SVG
}

void doc_OnDrawBackground(object sender, vdRender render, ref bool cancel)
{
    if (isOnSVGSave && render!=null && render is RenderFormats.SvgRender) // check that is on "save" and render is SvgRender
    {
        cancel = true; // you need to pass this as tru
        render.Clear(vdFramedControl.BaseControl.ActiveDocument.Palette.Background); // clear the render and use the Palette’s Background color!
    }
}   

Send comments on this topic.