Skip to content

Saving a changed drawing

Everything on the previous pages changes the document in memory. This page shows how to write it back: to a file, to a string list, or to a stream such as a database field.

To a file

The loaded document is SVGRoot.Doc. SaveToFile writes it as it is now, with every change made through the document:

SVG2Image1.SVGRoot.Doc.SaveToFile('C:\Drawings\plant-annotated.svg');

To a string list or a stream

Doc.XML returns the same text as a TStrings, rebuilt from the document each time you read it. Use it to show the drawing in a memo, or to store it somewhere other than a file:

var
  Stream: TMemoryStream;
begin
  Memo1.Lines.Assign(SVG2Image1.SVGRoot.Doc.XML);

  Stream := TMemoryStream.Create;
  try
    SVG2Image1.SVGRoot.Doc.XML.SaveToStream(Stream, TEncoding.UTF8);
    // ... write Stream to a blob field
  finally
    Stream.Free;
  end;
end;

What the saved file looks like

The saved file is the same drawing, not the same text:

Kept Changed
Elements and attributes, including custom ones such as data-tag Indentation is rewritten, two spaces per level
Namespaces and prefixed attributes, such as inkscape:label and xlink:href The attributes of an element are written on one line
The XML declaration, comments and CDATA sections Line breaks between elements are rewritten
Text in text and tspan elements, including its spaces
Characters XML reserves, written as &, <, > and "

The file is written as UTF-8. It loads back into the library, and into other SVG tools, as the drawing it was saved from.

Before update 25

Earlier versions wrote &, < and " in text and attribute values without escaping them, so a drawing containing, for example, Pumps &amp; valves was saved as text that no XML parser accepts. If you save drawings, use update 25 or later.

The component's SVG property is the loaded text

A component's SVG property holds the text the drawing was loaded from. It is not updated when you change the document, so saving SVG2Image1.SVG saves the original. Assigning SVG, or FileName, loads the drawing again from that text and discards the changes made in memory.

To make the changed drawing the component's new source, for example before storing the form's state, assign the document text back:

SVG2Image1.SVG := SVG2Image1.SVGRoot.Doc.XML;

This parses the drawing again, so find your elements again afterwards (see Element references and reloading).