Handling SVG (Scalable Vector Graphics) files.
- 1. SVG GUI tools.
- 2. Transformation SVG to PDF.
- 3. Processing SVG as XML.
- 4. SVG-GDI+ bridge.
- 5. Rasterize SVG as bitmap.
1. SVG GUI tools.
There are some GUI tools for processing SVG - www.w3.org. My choice is https://inkscape.org/ and https://www.blender.org/download/.
Blender is not listed by W3C and Blender working slowly than inkscape, but working and has many additional future, for example Video Processing (this is my loved video editor) and also Blender can import GCode (inkscape can only export it).
Also there some another interesting GUI, distributed in .NET source code, for example SvgPad.
2. Transformation SVG to PDF.
I don't know opensource freeware software to transform SVG to PDF. Any software I see was scam, it create PNG bitmap and pack it in PDF wrapper. Its not a vector image, it's only scam.
Therefore I use main SVG program to processing SVG-files as native PDF-format - https://inkscape.org/, therefore my main VB.NET pattern to eventually transformation SVG to PDF look as this code:
1: Module Inkscape
2: Public Sub SvgToPdf2(InputSvgFileName As String, OutPdfFileName As String, WorkingDirectory As String, InkscapePath As String)
3:
4: Dim CmdUtil As New Process
5: CmdUtil.StartInfo.FileName = "" & InkscapePath & ""
6: CmdUtil.StartInfo.Arguments = "--without-gui --export-pdf "" & OutPdfFileName & "" " & "" & InputSvgFileName & ""
7: CmdUtil.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(WorkingDirectory)
8: CmdUtil.StartInfo.UseShellExecute = False
9: CmdUtil.StartInfo.RedirectStandardError = True
10: Try
11: CmdUtil.Start()
12: CmdUtil.WaitForExit()
13: Catch ex As Exception
14: MsgBox(ex.Message)
15: End Try
16:
17: End Sub
18: End Module
3. Processing SVG as XML.
There are three choice to processing SVG as XML
- by HtmlAgilityPack (Xpath selector), see more example in Parse HTML by HtmlAgilityPack (Xpath selector) and CsQuery (jQuery selector)
- by CsQuery (jQuery selector)
- by Microsoft .NET class - XDocument/XElement, see more example in page Пример использования LINQ-to-XML (XLINQ) и LINQ-to-SQL., Как парсить XML SOAP в MS SQL.
Below you can see my basic function for parse SVG files.
1: Imports HtmlAgilityPack
2: Imports System.Linq
3:
4: Partial Public Class XMLParser_HtmlAgilityPack
5:
6: Dim SVG As HtmlDocument
7:
8: Public Sub New(SvgFileName As String)
9: SVG = New HtmlDocument()
10: SVG.Load(SvgFileName)
11: End Sub
12:
13: Public Function GetTextNode(TextNodeName As String) As HtmlNode
14: Return SVG.DocumentNode.SelectNodes("//text[@id='" & TextNodeName & "']").First
15: End Function
16:
17: Public Function GetSpanNode(SpanNodeName As String) As HtmlNode
18: Return SVG.DocumentNode.SelectNodes("//tspan[@id='" & SpanNodeName & "']").First
19: End Function
20:
21: Public Sub SetSpanNode(SpanNodeName As String, Value As String)
22: SVG.DocumentNode.SelectNodes("//tspan[@id='" & SpanNodeName & "']").First.InnerHtml = Value
23: End Sub
24:
25: Public Sub ReplaceSpanText(SpanNodeName As String, OldTxt As String, NewTxt As String)
26: SetSpanNode(SpanNodeName, Replace(GetSpanNode(SpanNodeName).InnerText, OldTxt, NewTxt))
27: End Sub
28:
29: Public Sub Save(FileName As String)
30: SVG.Save(FileName)
31: End Sub
32:
33: End Class
And real project usually contains thousands of similar line of code.
4. SVG-GDI+ bridge.
This interesting project SVG-GDI+ bridge contains definition of all GDI+ class function and with this bridge we can write directly to SVG. This project was write in 2003 year and use ancient ActiveX component ASV3Sharp. I have no interest to rise again that component, but directly writing to SVG surface from VB.NET is cool.
5. Rasterize SVG as bitmap.
Basic package to transform SVG to bitmap is SVG.NET, vvvv/SVG. This package contains full source code and viewer.
There are many another samples of using this library, for example this project, for example lasitha.blog