Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active December 23, 2021 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/43e53ea52673b2211d6023a13b3a3dde to your computer and use it in GitHub Desktop.
Save aspose-com-gists/43e53ea52673b2211d6023a13b3a3dde to your computer and use it in GitHub Desktop.
Create SVG Image File Programmatically using C# | Embedded HTML Contents
// Specify SVG content with embedded HTML.
// Specify the XHTML namespace from which the foreign object originates.
string documentContent = "<svg viewBox =\"0 0 200 200\" xmlns=\"http://www.w3.org/2000/svg\">\n <style>\n div {\n color: white;\n font: 18px serif;\n height: 100%;\n overflow: hidden;\n }\n </style>\n \n <polygon points=\"5,5 195,10 185,185 10,195\" />\n\n <!-- Common use case: embed HTML text into SVG -->\n <foreignObject x=\"20\" y=\"20\" width=\"160\" height=\"160\">\n <!--\n In the context of SVG embedded in an HTML document, the XHTML \n namespace could be omitted, but it is mandatory in the \n context of an SVG document\n -->\n <div xmlns=\"http://www.w3.org/1999/xhtml\">\n Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n Sed mollis mollis mi ut ultricies. Nullam magna ipsum,\n porta vel dui convallis</div>\n </foreignObject>\n</svg>";
// Initialize an SVG document using SVGDocument.
SVGDocument document = new SVGDocument(documentContent, ".");
// Save the document to a file with SVGSaveFormat enumeration.
document.Save(dataDir + "Test.svg", SVGSaveFormat.SVG);
// Specify SVG content as string
string documentContent = "<svg xmlns=\"http://www.w3.org/2000/svg\"><circle cx=\"50\" cy=\"50\" r=\"40\" /></svg>";
// Initialize an object of SVGDocument class from the string content
SVGDocument document = new SVGDocument(documentContent, ".");
// Save the document to a file
document.Save(dataDir + "test.svg", SVGSaveFormat.SVG);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment