Supply linked images when reading SVG
View as MarkdownThis sample shows how to read an SVG whose images are not next to it, for example a design from an online editor that refers to its pictures by URL. The SvgReaderSettings.ResolveImage callback is called for every image element; it gets the reference through ResolveImageArgs.Href and supplies the content itself: a raster image through ResolveImageArgs.Image or vector graphics through ResolveImageArgs.GraphicsContainer. Here a photo comes from a local cache as a CompressedImage, so its JPEG data reaches the output PDF unchanged, and a seal comes from a PDF file and stays vector.
Code Snippet
// Local copies of the assets that the SVG refers to by URL.
var assets = new Dictionary<string, string>
{
["https://cdn.example.com/products/apple.jpg"] = "Apple.jpg",
["https://cdn.example.com/seals/notary.pdf"] = "Seal.pdf",
};
var settings = new SvgReaderSettings
{
ResolveImage = args =>
{
if (!assets.TryGetValue(args.Href, out var fileName))
{
// Leaving both Image and GraphicsContainer unset loads the reference as usual.
return;
}
Console.WriteLine("{0} -> {1}", args.Href, fileName);
if (fileName.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase))
{
// Vector graphics stay vector.
using (var pdfReader = new PdfReader(fileName))
using (var seal = pdfReader.Frames[0].GetContent())
{
args.GraphicsContainer = seal;
}
}
else
{
// The JPEG data is kept and written to the PDF without recompression.
using (var photo = CompressedImage.CreateFromFile(fileName))
{
args.Image = photo;
}
}
},
};
using (var reader = new SvgReader("Certificate.svg", settings))
using (var writer = new PdfWriter("ResolveSvgImages.pdf"))
using (var graphics = writer.GetGraphics())
using (var content = reader.GetContent())
{
writer.AddPage(reader.Width, reader.Height, reader.DpiX, reader.DpiY);
graphics.DrawContainer(content, 0, 0);
}
Input
Certificate.svg
Apple.jpg
DownloadSeal.pdf
DownloadOutput
ResolveSvgImages.pdf
DownloadResolveSvgImages.png
ResolveSvgImages.txt
https://cdn.example.com/products/apple.jpg -> Apple.jpg
https://cdn.example.com/seals/notary.pdf -> Seal.pdf
Used APIs
- Aurigma.GraphicsMill.Codecs.ImageReader.DpiX
- Aurigma.GraphicsMill.Codecs.ImageReader.DpiY
- Aurigma.GraphicsMill.Codecs.PdfFrame.GetContent
- Aurigma.GraphicsMill.Codecs.PdfReader
- Aurigma.GraphicsMill.Codecs.PdfReader.Frames
- Aurigma.GraphicsMill.Codecs.PdfWriter
- Aurigma.GraphicsMill.Codecs.PdfWriter.AddPage(Single,Single,Single,Single)
- Aurigma.GraphicsMill.Codecs.PdfWriter.GetGraphics
- Aurigma.GraphicsMill.Codecs.ResolveImageArgs.GraphicsContainer
- Aurigma.GraphicsMill.Codecs.ResolveImageArgs.Href
- Aurigma.GraphicsMill.Codecs.ResolveImageArgs.Image
- Aurigma.GraphicsMill.Codecs.SvgReader
- Aurigma.GraphicsMill.Codecs.SvgReader.GetContent
- Aurigma.GraphicsMill.Codecs.SvgReaderSettings
- Aurigma.GraphicsMill.Codecs.SvgReaderSettings.ResolveImage
- Aurigma.GraphicsMill.CompressedImage
- Aurigma.GraphicsMill.CompressedImage.CreateFromFile(String)
- Aurigma.GraphicsMill.Drawing.Graphics.DrawContainer(GraphicsContainer,Single,Single)