Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Aspose.Imaging .NET API allows to easy manipulate your svg images or photos in your .NET application or Web service.

You can:

  • Convert svg image to another formats;
  • Create svg image;
  • Export to svg from another formats.

Interested ?

You may go further at : https://products.aspose.com/imaging/net/

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");
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");
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment