SaveSVG writes a vector SVG file and SVG returns the same markup as a string, which is what you want inside a web server or when storing the drawing in a database. Neither needs a visible editor, so both work in a service or a WebBroker module.
{ FormEditor already holds EmbedDraw1 (TEmbedDrawVCL), EDWEditor1
(TEDWEditor) and SaveDialog1 (TSaveDialog). }
procedure TFormEditor.butSaveSVGClick(Sender: TObject);
begin
SaveDialog1.Filter := 'SVG files (*.svg)|*.svg';
SaveDialog1.DefaultExt := 'svg';
{ Execute opens the modal, centred Windows save dialog. svCurrentUnit
keeps the document units, Factor = 1, and the last flag writes an id
attribute on every SVG element, taken from the element ID. }
if SaveDialog1.Execute then
EmbedDraw1.SaveSVG(SaveDialog1.FileName, svCurrentUnit, 1, True);
end;
function TFormEditor.DrawingAsSVG: string;
begin
{ The same output as a string: an HTTP response, a report or a blob. }
Result := EmbedDraw1.SVG(svCurrentUnit, 1, True);
end;
procedure TFormEditor.butExportClick(Sender: TObject);
begin
{ Type, file, page, scale, quality, check data, transparent [, fit]. }
EmbedDraw1.ExportDoc(exPDF, 'plate.pdf', 1, 1, 100, False, False);
EmbedDraw1.ExportDoc(exPNG, 'plate.png', 1, 4, 100, False, True);
end;
procedure TFormEditor.butPreviewClick(Sender: TObject);
begin
EmbedDraw1.Preview; // modal print preview window
end;
{ ---- Building the drawing by code before exporting ---- }
procedure TFormEditor.butBuildPlateClick(Sender: TObject);
begin
EmbedDraw1.New;
EmbedDraw1.Units := unMM;
EmbedDraw1.Measure.PaperWidth := 210; // A5 landscape
EmbedDraw1.Measure.PaperHeight := 148;
EmbedDraw1.Measure.Width := 210;
EmbedDraw1.Measure.Height := 148;
EmbedDraw1.BeginUpdate; // one recalculation instead of one per element
try
TEDWFactory.AddRectangle(EmbedDraw1, 20, 20, 120, 60).PenWidth := 0.5;
with TEDWFactory.AddText(EmbedDraw1, 20, 12) do
begin
Text := 'Assembly plate';
FontHeight := 6;
end;
with TEDWFactory.AddArrowLine(EmbedDraw1, 20, 90, 140, 90) do
begin
Text := '120 mm';
FontHeight := 3;
FromType := ttArrow;
ToType := ttArrow;
end;
finally
EmbedDraw1.EndUpdate;
end;
end;
What each step does
ExportType is svCurrentUnit with Factor = 1 for a one-to-one export in document units; svFactor and svFactorPx scale the output, and svCurUnitNoMed omits the measurement wrapper.
- The last argument writes an
id attribute on each SVG element, taken from the element ID. That is what makes the output scriptable in a browser — give your items meaningful IDs and you can address them from JavaScript.
BeginUpdate and EndUpdate around bulk creation avoid one recalculation per element on large drawings.
ExportDoc covers exPDF, exXPS, exPNG, exJPEG, exBMP and exGIF, with page, scale, quality, data-check, transparency and fit-to-page arguments.
- The
TEDWFactory class methods create every element type by code: lines, arcs, polygons, text, barcodes, QR, DataMatrix, PDF417, images, flow shapes, links, tables, dimensions and symbols.
Full reference: Developer Guide (PDF) — section 12.1.2.17 SaveSVG, and the export chapter.