Aspose.Words SVG File Processor for .NET
Aspose.Words SVG File Processor for .NET lets you programmatically build, modify, and save SVG graphics from your .NET applications. Because Aspose.Words does not natively load SVG as a document format, this plugin is intended to be used together with converter plugins (e.g., DOCX/HTML/PDF → SVG) or any upstream import that produces SVG markup. You can then refine the SVG (edit shapes, paths, text, styles) and serialize standards-compliant output.
Installation and Setup
- Add the Aspose.Words for .NET NuGet package to your project. See the Installation guide.
- Configure metered licensing once at app startup. Follow Metered Licensing .
Note: This plugin provides an SVG-focused API surface that complements, not replaces, core Aspose.Words features.
Features and Capabilities
SVG Document Creation
- Instantiate a new SVG in code, set
width
,height
, andviewBox
. - Choose absolute units (px, mm, in) or rely on viewBox for responsive output.
Shapes & Paths
- Create and edit rect, circle, ellipse, line, polyline, and polygon elements.
- Full path command support (M/L/H/V/C/S/Q/T/A/Z) with segment insert/remove, transforms, and stroking/filling options.
Text & Typography
- Add
<text>
and<tspan>
nodes with font family, size, weight, baseline shift, letter/word spacing, and anchors (start/middle/end). - Apply CSS-like styling inline or via reusable class definitions.
Grouping & Layering
- Organize content with
<g>
groups; apply transforms at the group level (translate/scale/rotate/skew). - Nest groups to mirror layer-style hierarchies.
Gradients, Patterns, Filters
- Define linear/radial gradients, patterns, and reference them by ID.
- Attach filters (blur, drop shadow, color matrix) for advanced visual effects.
Image Embedding
- Insert raster images via
<image>
using external URIs or Base64 data. - Control placement, sizing (with preserveAspectRatio), and opacity.
Converter Integration
- Typical flow: Import (e.g., DOCX→SVG) → Refine with SVG File Processor (adjust colors, add watermarks, optimize paths) → Save SVG.
- Preserve or rewrite IDs, classes, and defs during post-processing.
Serialization & Export
- Save compact or pretty-printed SVG.
- Optionally inline resources (fonts/images) or keep them external.
- Ensure standards-compliant markup compatible with modern renderers.
Quick Start: Creating an SVG Document
// Pseudocode for the plugin’s SVG API surface
var svg = new SvgDocument(width: 800, height: 400, viewBox: "0 0 800 400");
// Background rectangle
var bg = svg.AddRect(x: 0, y: 0, width: 800, height: 400);
bg.Fill = "#ffffff";
// Title text
var title = svg.AddText("Quarterly Sales", x: 400, y: 40);
title.FontFamily = "Segoe UI";
title.FontSize = 24;
title.TextAnchor = SvgTextAnchor.Middle;
// A sample path (rounded shape)
var path = svg.AddPath("M40,120 C120,20 280,20 360,120 S600,220 720,120");
path.Stroke = "#0066cc";
path.StrokeWidth = 3;
path.Fill = "none";
// Group with transform
var g = svg.AddGroup();
g.Transform = "translate(60, 220) scale(1.2)";
g.AddCircle(cx: 0, cy: 0, r: 8).Fill = "#ff6a00";
g.AddText("Marker", x: 16, y: 4).FontSize = 12;
// Save to file/stream
using var fs = File.Create(@"C:\out\diagram.svg");
svg.Save(fs, prettyPrint: true);
Post-Process an Imported SVG (DOCX → SVG → tweak → save)
// Assume 'svgMarkup' comes from a converter plugin (e.g., Document → Save as SVG)
var svg = SvgDocument.LoadFromString(svgMarkup);
// Example: recolor all elements with class="accent"
foreach (var el in svg.Query(".accent"))
el.Fill = "#1abc9c";
// Add a watermark group
var wm = svg.AddGroup();
wm.Opacity = 0.08;
wm.Transform = "rotate(-20 400 200)";
var wText = wm.AddText("CONFIDENTIAL", x: 400, y: 220);
wText.TextAnchor = SvgTextAnchor.Middle;
wText.FontFamily = "Segoe UI";
wText.FontSize = 64;
wText.Fill = "#000000";
svg.Save(@"C:\out\refined.svg");
Tips and Best Practices
- Use viewBox with relative coordinates for responsive graphics.
- Group wisely: apply transforms to groups instead of per-element to simplify markup.
- Put defs (gradients, patterns, symbols, filters) at the top and reference by ID.
- Keep large bitmaps external for smaller SVGs; inline only small assets.
- Prefer pretty-print during development; minify in production.
- Initialize metered licensing early to avoid interruptions.
Error Handling & Validation
- The processor validates required attributes, IDs, and references.
- Clear exceptions for malformed paths, invalid transforms, or missing
defs
targets. - Optional “lenient” mode can skip non-critical issues and continue serialization.
Performance
- Streamed load/save for large documents.
- Reuse gradient/filter definitions to avoid duplicate nodes.
- Query via lightweight selectors (by ID/class/name) for fast edits.