Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
CSS-Selektoren sind eine praktische Möglichkeit, bestimmte SVG-Elemente vor dem Bearbeiten gezielt anzusprechen. Mit Aspose.SVG for .NET kann C#-Code
QuerySelector() und
QuerySelectorAll() verwenden, um Elemente nach Tag-Namen, Hierarchie, Attributen oder Position im SVG-Baum zu finden und anschließend Attribute zu ändern, Knoten zu ersetzen oder Pfaddaten zu aktualisieren.
Dieser Artikel verwendet die Beispielillustration owl.svg. Die Beispiele wählen Kreise und Pfade im SVG DOM aus und erzeugen bearbeitete Versionen des ursprünglichen Vektorbildes.
Ein selektorbasierter Bearbeitungsablauf besteht meist aus vier Schritten:
SVGDocument.QuerySelector() für ein Element oder QuerySelectorAll() für eine Elementliste.Eine kurze Einführung in DOM-Navigation, XPath und Node-Filter finden Sie unter SVG in C# inspizieren und navigieren.
Das erste Beispiel bearbeitet die Quelldatei
owl.svg. Die Eulenillustration besteht aus path- und circle-Elementen. Der Code verwendet CSS-Selektoren, um:
circle-Elemente zu finden und ihre Attribute so zu ändern, dass die Augen größer und blau werden;path-Element zu finden, das den Flügel darstellt, und sein Aussehen zu ändern.1using Aspose.Svg;
2using System.IO;
3using Aspose.Svg.Dom;
4using Aspose.Svg.Collections; 1// Edit SVG elements with CSS selectors in C#
2
3// Load an SVG document
4using (SVGDocument document = new SVGDocument(new Url("https://docs.aspose.com/svg/files/owl.svg")))
5{
6 // Get the root <svg> element of the document
7 SVGSVGElement svgElement = document.RootElement;
8
9 // Find the first element that matches the specified in selector
10 SVGGElement gElement = svgElement.QuerySelector("g") as SVGGElement;
11
12 // Find all <circle> elements in a <g> element
13 NodeList circleNodes = gElement.QuerySelectorAll("circle");
14
15 // Make big blue eyes
16 foreach (Node circleNode in circleNodes)
17 {
18 SVGCircleElement circleElement = circleNode as SVGCircleElement;
19 circleElement.R.BaseVal.Value *= 1.5F;
20 circleElement.SetAttribute("stroke", "blue");
21 }
22
23 // Get a path for the owl's wing
24 SVGPathElement wingPath = gElement.QuerySelector("path:nth-child(2)") as SVGPathElement;
25
26 // Apply style attributes to the wing
27 wingPath.SetAttribute("stroke-width", "16");
28 wingPath.SetAttribute("stroke-dasharray", "2 8");
29
30 document.Save(Path.Combine(OutputDir, "owl-edited1.svg"));
31}Die bearbeitete SVG-Datei können Sie hier öffnen: owl-edited1.svg.
Das zweite Beispiel arbeitet weiter mit derselben Quelldatei owl.svg. Es wählt die ursprünglichen runden Augen aus und ersetzt sie durch quadratische Augen:
<g>-Element für die Ersatzrechtecke.QuerySelectorAll("g:first-child > circle"), um Kreise in der ersten Gruppe auszuwählen.<rect>-Elemente mit den erforderlichen Größen- und Style-Attributen.<circle>-Elemente aus dem Dokument.1using Aspose.Svg;
2using System.IO;
3using Aspose.Svg.Dom;
4using Aspose.Svg.Collections; 1// Replace SVG circles with rectangles using CSS selectors in C#
2
3// Load an SVG document
4using (SVGDocument document = new SVGDocument(new Url("https://docs.aspose.com/svg/files/owl.svg")))
5{
6 // Get the root <svg> element of the document
7 SVGSVGElement svgElement = document.RootElement;
8
9 // Create a new <g> element with style attributes and append it as the last child of the <svg> element
10 SVGGElement gElement = (SVGGElement)document.CreateElementNS(SvgNamespace, "g");
11 gElement.SetAttribute("fill", "none");
12 gElement.SetAttribute("stroke-width", "2");
13 svgElement.AppendChild(gElement);
14
15 // Find all <circle> elements from the first <g> element
16 NodeList circleNodes = svgElement.QuerySelectorAll("g:first-child > circle");
17
18 // Make square sky-blue eyes
19 foreach (Node circleNode in circleNodes)
20 {
21 SVGCircleElement circleElement = circleNode as SVGCircleElement;
22
23 float cx = circleElement.Cx.BaseVal.Value;
24 float cy = circleElement.Cy.BaseVal.Value;
25 float r = circleElement.R.BaseVal.Value;
26
27 SVGRectElement rectElement = (SVGRectElement)document.CreateElementNS(SvgNamespace, "rect");
28 rectElement.X.BaseVal.Value = cx - r;
29 rectElement.Y.BaseVal.Value = cy - r;
30 rectElement.Width.BaseVal.Value = 3 * r;
31 rectElement.Height.BaseVal.Value = 3 * r;
32 rectElement.SetAttribute("stroke", "SkyBlue");
33
34 // Add a <rectangle> element into the second (new) <g> element and remove <circle> elements
35 gElement.AppendChild(rectElement);
36 circleElement.Remove();
37 }
38 // Recolor last rectangle in the second (new) <g> element
39 Element lastRect = gElement.LastElementChild;
40 lastRect.SetAttribute("stroke", "red");
41
42 document.Save(Path.Combine(OutputDir, "owl-svg-eyes.svg"));
43}Der letzte Teil ändert den Flügel der Eule von einer Kurve zu einem Linienzug und färbt ihn neu. Das Beispiel wählt das path-Element aus, aktualisiert pfadbezogene Daten und speichert das Ergebnis:
1using Aspose.Svg;
2using System.IO;
3using Aspose.Svg.Dom;
4using Aspose.Svg.Collections; 1// Modify SVG path data selected with CSS selectors in C#
2
3// Load an SVG document
4using (SVGDocument document = new SVGDocument(Path.Combine(DataDir, "owl-svg-eyes.svg")))
5{
6 SVGSVGElement svgElement = document.RootElement;
7
8 // Get a path for owl's body from the first <g> element
9 Element bodyPath = (svgElement.QuerySelector("g:first-child") as SVGGElement).FirstElementChild;
10 bodyPath.SetAttribute("stroke", "Teal");
11
12 // Get a path for the owl's wing from the first <g> element
13 SVGPathElement wingPath = svgElement.QuerySelector("g:first-child > path:nth-child(2)") as SVGPathElement;
14
15 // Form new wing path data based on the old
16 string d = "";
17 foreach (SVGPathSeg pathSeg in wingPath.PathSegList)
18 {
19 if (pathSeg is SVGPathSegMovetoAbs)
20 {
21 SVGPathSegMovetoAbs pathSegMovetoAbs = pathSeg as SVGPathSegMovetoAbs;
22
23 d += string.Format(" M {0} {1}", pathSegMovetoAbs.X, pathSegMovetoAbs.Y);
24 }
25 if (pathSeg is SVGPathSegCurvetoCubicAbs)
26 {
27 SVGPathSegCurvetoCubicAbs pathSegCurvetoCubicAbs = pathSeg as SVGPathSegCurvetoCubicAbs;
28
29 d += string.Format(
30 " L {0} {1} L {2} {3}",
31 (pathSegCurvetoCubicAbs.X1 + pathSegCurvetoCubicAbs.X2) / 2F,
32 (pathSegCurvetoCubicAbs.Y1 + pathSegCurvetoCubicAbs.Y2) / 2F,
33 pathSegCurvetoCubicAbs.X,
34 pathSegCurvetoCubicAbs.Y
35 );
36 }
37 }
38 // Set d attribute - new path data formation
39 wingPath.SetAttribute("d", d.Trim());
40 wingPath.SetAttribute("stroke", "Teal");
41
42 document.Save(Path.Combine(OutputDir, "owl-edited2.svg"));
43}Das Bild vergleicht die ursprüngliche Eule und zwei bearbeitete Versionen.

Die zweite bearbeitete SVG-Datei können Sie hier öffnen: owl-edited2.svg.
1. Wann sollte ich QuerySelector() statt QuerySelectorAll() verwenden?
Verwenden Sie QuerySelector(), wenn nur das erste passende Element benötigt wird. Verwenden Sie QuerySelectorAll(), wenn alle passenden Elemente geprüft oder bearbeitet werden sollen, zum Beispiel alle Kreise in einer Gruppe.
2. Können CSS-Selektoren SVG-Elemente selbst ändern?
Nein. Selektoren finden nur Elemente. Nachdem ein Element ausgewählt wurde, verwenden Sie DOM-Methoden und Eigenschaften wie SetAttribute(), AppendChild(), RemoveChild() oder typisierte SVG-Eigenschaften, um Änderungen vorzunehmen.
3. Können selektorbasierte Änderungen in PDF oder Bilder konvertiert werden?
Ja. Nach dem Bearbeiten des SVGDocument rufen Sie Converter.ConvertSVG(document, options, outputPath) mit passenden Speicheroptionen auf oder speichern zuerst die SVG-Datei und konvertieren sie später.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.