---
title: "Supply linked images when reading SVG"
canonical: https://www.graphicsmill.com/samples/Codecs/VectorFormats/ResolveSvgImages_cs
product: "Graphics Mill"
source: "Codecs/VectorFormats/ResolveSvgImages.cs"
---

# Supply linked images when reading SVG

This 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.

## Instruction

Use the SVG API to convert an SVG that refers to its images by URL into PDF: map every image reference to a local file in a ResolveImage callback of SvgReaderSettings, supply photos as CompressedImage and vector graphics as GraphicsContainer, and draw the SVG content on a PDF page.

## Usings

```csharp
using System;
using System.Collections.Generic;
using Aurigma.GraphicsMill.Codecs;
```

## Code Snippet

```csharp
// 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);
}
```

## Explanation

The code:

- Maps the URLs used in "Certificate.svg" to local copies of the assets.
- Creates ` SvgReaderSettings ` with a ` ResolveImage ` callback that looks up ` ResolveImageArgs.Href ` in that map.
- For a PDF asset, reads the first page with ` PdfReader ` and ` PdfFrame.GetContent ` and assigns it to ` ResolveImageArgs.GraphicsContainer `, so the seal stays vector.
- For a photo, loads it with ` CompressedImage.CreateFromFile ` and assigns it to ` ResolveImageArgs.Image `, so the JPEG data is kept as is; the objects can be disposed right away because the reader takes its own copy.
- Opens the SVG with ` SvgReader ` and these settings, adds a page of the same size to a ` PdfWriter `, and draws the content from ` SvgReader.GetContent ` with ` Graphics.DrawContainer `.
- Prints every resolved reference and saves "ResolveSvgImages.pdf".

## Tags

- [Format Conversion](<https://www.graphicsmill.com/samples/tags/format-conversion_md>): Converting images between formats
- [Graphics Container](<https://www.graphicsmill.com/samples/tags/container_md>): Using graphics containers for grouped items
- [PDF](<https://www.graphicsmill.com/samples/tags/pdf_md>): Importing and exporting PDF documents
- [SVG](<https://www.graphicsmill.com/samples/tags/svg_md>): Importing and exporting SVG graphics

## Input

- [Certificate.svg](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/Certificate_svg>) (preview)
- [Apple.jpg](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/Apple_jpg>) (download)
- [Seal.pdf](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/Seal_pdf>) (download)

## Output

- [ResolveSvgImages.pdf](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/ResolveSvgImages_pdf>) (download)
- [ResolveSvgImages.png](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/ResolveSvgImages_png>) (preview)
- [ResolveSvgImages.txt](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/ResolveSvgImages_txt>) (preview)

## Used APIs

- [Aurigma.GraphicsMill.Codecs.ImageReader.DpiX](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_ImageReader_DpiX.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ImageReader.DpiY](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_ImageReader_DpiY.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfFrame.GetContent](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_PdfFrame_GetContent.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfReader](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PdfReader.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfReader.Frames](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_PdfReader_Frames.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfWriter](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PdfWriter.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfWriter.AddPage(Single,Single,Single,Single)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_PdfWriter_AddPage_4_37f6c741.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfWriter.GetGraphics](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_PdfWriter_GetGraphics.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ResolveImageArgs.GraphicsContainer](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_ResolveImageArgs_GraphicsContainer.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ResolveImageArgs.Href](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_ResolveImageArgs_Href.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ResolveImageArgs.Image](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_ResolveImageArgs_Image.htm.md>)
- [Aurigma.GraphicsMill.Codecs.SvgReader](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_SvgReader.htm.md>)
- [Aurigma.GraphicsMill.Codecs.SvgReader.GetContent](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_SvgReader_GetContent.htm.md>)
- [Aurigma.GraphicsMill.Codecs.SvgReaderSettings](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_SvgReaderSettings.htm.md>)
- [Aurigma.GraphicsMill.Codecs.SvgReaderSettings.ResolveImage](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_SvgReaderSettings_ResolveImage.htm.md>)
- [Aurigma.GraphicsMill.CompressedImage](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_CompressedImage.htm.md>)
- [Aurigma.GraphicsMill.CompressedImage.CreateFromFile(String)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_CompressedImage_CreateFromFile_1_bb3a7a4f.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Graphics.DrawContainer(GraphicsContainer,Single,Single)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Graphics_DrawContainer_3_098aa7ce.htm.md>)
