Manipulating SVG Files
Saving SVG with Fonts embedded or Exported
Using Aspose.Imaging for .NET, developers can save SVG with fonts embedded or exported. Using this sample example we should be able to set fonts to be exported. When this setting is on, all fonts used in SVG drawing should be saved as separate font files. We should be able to provide a stream for saving this font files, and be able to set the path (url) for the reference to this font files.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.ImageOptions; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
string FontFolderName = dataDir + "fonts"; | |
// ReadFileWithEmbeddedFontsAndExportToPdf | |
ReadAndExportToPdf(dataDir + "template.svg"); | |
//SaveWithEmbeddedFonts | |
Save(true, dataDir + "template.svg", 0); | |
//SaveWithExportFonts | |
Save(false, dataDir + "template.svg", 6); | |
void ReadAndExportToPdf(string inputFileName) | |
{ | |
string inputFile = inputFileName; | |
string outFile = dataDir + "result.pdf"; | |
using (Image image = Image.Load(inputFile)) | |
{ | |
image.Save(outFile, | |
new PdfOptions { VectorRasterizationOptions = new SvgRasterizationOptions { PageSize = image.Size } }); | |
} | |
File.Delete(outFile); | |
} | |
void Save(bool useEmbedded, string fileName, int expectedCountFonts) | |
{ | |
string fontStoreType = useEmbedded ? "Embedded" : "Stream"; | |
string inputFile = fileName; | |
string outFileName = dataDir + "result" + "_" + fontStoreType + ".svg"; | |
string outputFile = outFileName; | |
string fontFolder = string.Empty; | |
using (Image image = Image.Load(inputFile)) | |
{ | |
EmfRasterizationOptions emfRasterizationOptions = new EmfRasterizationOptions(); | |
emfRasterizationOptions.BackgroundColor = Color.White; | |
emfRasterizationOptions.PageWidth = image.Width; | |
emfRasterizationOptions.PageHeight = image.Height; | |
string testingFileName = Path.GetFileNameWithoutExtension(inputFile); | |
fontFolder = FontFolderName; | |
image.Save(outputFile, | |
new SvgOptions | |
{ | |
VectorRasterizationOptions = emfRasterizationOptions, | |
Callback = | |
new SvgCallbackFontTest(useEmbedded, fontFolder) | |
{ | |
Link = FontFolderName + "/" + testingFileName | |
} | |
}); | |
} | |
if (!useEmbedded) | |
{ | |
string[] files = Directory.GetFiles(fontFolder); | |
if (files.Length != expectedCountFonts) | |
{ | |
throw new Exception(string.Format( | |
"Expected count font files = {0}, Current count font files = {1}", expectedCountFonts, | |
files.Length)); | |
} | |
} | |
File.Delete(outFileName); | |
} | |
class SvgCallbackFontTest : SvgResourceKeeperCallback | |
{ | |
#region Fields | |
/// <summary> | |
/// The out folder | |
/// </summary> | |
private readonly string outFolder; | |
/// <summary> | |
/// The use embedded font | |
/// </summary> | |
private readonly bool useEmbeddedFont; | |
/// <summary> | |
/// The font counter | |
/// </summary> | |
private int fontCounter = 0; | |
#endregion | |
#region Constructors | |
/// <summary> | |
/// Initializes a new instance of the <see cref="SvgTests.SvgCallbackFontTest" /> class. | |
/// </summary> | |
/// <param name="useEbeddedFont">if set to <c>true</c> [use ebedded font].</param> | |
/// <param name="outFolder">The out folder.</param> | |
public SvgCallbackFontTest(bool useEbeddedFont, string outFolder) | |
{ | |
this.useEmbeddedFont = useEbeddedFont; | |
this.outFolder = outFolder; | |
} | |
#endregion | |
#region Properties | |
public string Link { get; set; } | |
#endregion | |
#region Methods | |
/// <summary> | |
/// Called when font resource ready to be saved to storage. | |
/// </summary> | |
/// <param name="args">The arguments.</param> | |
/// <returns> | |
/// Returns path to saved resource. Path should be relative to target SVG document. | |
/// </returns> | |
/// <exception cref="System.NotImplementedException"></exception> | |
public override void OnFontResourceReady(FontStoringArgs args) | |
{ | |
if (this.useEmbeddedFont) | |
{ | |
args.FontStoreType = FontStoreType.Embedded; | |
} | |
else | |
{ | |
args.FontStoreType = FontStoreType.Stream; | |
string fontFolder = this.outFolder; | |
if (!Directory.Exists(fontFolder)) | |
{ | |
Directory.CreateDirectory(fontFolder); | |
} | |
string fName = args.SourceFontFileName; | |
if (!File.Exists(fName)) | |
{ | |
fName = string.Format("font_{0}.ttf", this.fontCounter++); | |
} | |
string fileName = fontFolder + @"\" + Path.GetFileName(fName); | |
args.DestFontStream = new FileStream(fileName, FileMode.OpenOrCreate); | |
args.DisposeStream = true; | |
args.FontFileUri = "./" + this.Link + "/" + Path.GetFileName(fName); | |
} | |
} | |
#endregion | |
} |
Converting Image Formats into SVG
Using Aspose.Imaging for .NET, developers can save different file formats to SVG. Below sample code is provided.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.ImageOptions; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
using (Image image = Image.Load(dataDir + "template.svg")) | |
{ | |
using (FileStream fs = new FileStream(dataDir + "result.svg", FileMode.Create, FileAccess.ReadWrite)) | |
{ | |
image.Save(fs); | |
} | |
} | |
File.Delete(dataDir + "result.svg"); |
Converting SVG into EMF
Using Aspose.Imaging for .NET, developers can convert SVG file format to EMF. Below sample code is provided.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.ImageOptions; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
using (Image image = Image.Load(dataDir + "template.svg")) | |
{ | |
image.Save(dataDir + "result.emf", | |
new EmfOptions | |
{ | |
VectorRasterizationOptions = new SvgRasterizationOptions | |
{ | |
PageSize = image.Size | |
} | |
}); | |
} | |
File.Delete(dataDir + "result.emf"); |