---
title: "Embed SVG in a PDF as vectors in C#"
url: https://recursiveai.org/onepdf/cookbook/embed-svg-in-pdf/
description: "Embed SVG in a PDF as vectors in C#. Runnable .NET example with sample input, actual output and documented options."
version: "1.22.0"
---

# Embed SVG in a PDF as vectors in C#

**OnePDF** is a free, open-source PDF library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePdf.Layout) · [GitHub](https://github.com/recursive-ai-oss/onepdf-csharp) · [Docs](https://recursiveai.org/onepdf/docs/) · [Download example (.zip)](https://recursiveai.org/onepdf/cookbook/embed-svg-in-pdf/embed-svg-in-pdf.zip)

Draw SVG as PDF vectors without a PNG conversion.

```shell
dotnet add package RecursiveAI.OnePdf.Layout
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OnePdf.Layout;

LayoutDocument.Compose(doc =>
    doc.Page(page =>
    page.Content().Svg("""
    <svg width="200" height="100"
      xmlns="http://www.w3.org/2000/svg">
      <circle cx="100" cy="50" r="40"/>
    </svg>
    """).Contain())).Save("vector.pdf");
```

Writes `vector.pdf` with a black circle. The SVG is defined inline.

Unzip [the example](https://recursiveai.org/onepdf/cookbook/embed-svg-in-pdf/embed-svg-in-pdf.zip), then `dotnet run`.

## The full program

The full program draws a blue badge and prints the notes for the script and external image it drops. All of its SVG input is shown in `Program.cs`; no image downloads are needed. Malformed SVG throws `PdfFormatException`; input over an SVG resource limit throws `PdfLimitException` during parsing.

```csharp
using OnePdf.Layout;

string svg = """
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100" viewBox="0 0 200 100">
  <title>Blue badge</title>
  <rect width="200" height="100" rx="12" fill="#164eaa"/>
  <circle cx="100" cy="50" r="30" fill="#ffd050"/>
  <script>throw new Error('not run');</script>
  <image href="https://example.com/photo.png" width="20" height="20"/>
</svg>
""";
var doc = LayoutDocument.Compose(doc =>
{
    doc.Page(page =>
    {
        page.Margin(40);
        page.Content().Width(240).Height(120)
            .Svg(svg).Contain();
    });
});
doc.Save("badge.pdf");
Console.WriteLine("Saved badge.pdf");
foreach (var note in doc.Diagnostics)
    Console.WriteLine(note.Message);
```

```text
Saved badge.pdf
Scripts are never run; the script element is ignored.
An image refers to something outside the document (https://example.com/photo.png); references to other files or addresses are never followed.
```

## Variations

**Place SVG at explicit page coordinates.** This standalone program writes `placed.pdf` without printing anything:

```csharp
using OnePdf;
using OnePdf.Layout;

var svg = SvgImage.Parse("""
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100">
  <circle cx="100" cy="50" r="40" fill="red"/>
</svg>
""");
using var doc = PdfDocument.CreateEmpty();
var page = doc.InsertBlankPage(0, 300, 200);
using var content = page.BeginContent();
svg.Draw(content, x: 30, y: 40, width: 240, height: 120);
content.Commit();
doc.Save("placed.pdf");
```

## Related recipes

- [Add an image to a PDF](https://recursiveai.org/onepdf/cookbook/add-image-to-pdf/)
- [Create a PDF](https://recursiveai.org/onepdf/cookbook/create-pdf/)

---

## The short answer

Use the `RecursiveAI.OnePdf.Layout` package to draw SVG into PDF content. `Slot.Svg(...).Contain()` places it in a layout box; `SvgImage.Parse(...).Draw(...)` paints it onto a content builder at explicit coordinates. The package does not put an SVG file inside the PDF and ask a reader to interpret it. It translates supported SVG features into native PDF drawing operations.

That distinction matters for logos and charts. Paths, fills and strokes become PDF vector content; an embedded PNG or JPEG remains an image. The [SVG documentation](https://recursiveai.org/onepdf/docs/layout-svg/) describes the supported features and the approximations. This is not a promise that every browser-rendered SVG will have identical output.

## How can I embed an SVG image in a PDF document?

Install the layout package, pass a self-contained SVG to `Svg`, and give the layout element a size. The full program uses a 240 by 120 point box and `Contain()`. It preserves the picture’s aspect ratio inside that box rather than stretching the circle into an ellipse. For an existing page, use the standalone builder variation above and commit its content before saving.

There are two coordinate systems to keep separate. Inside the SVG, the sample’s `viewBox="0 0 200 100"` defines its drawing coordinates. Outside it, the layout box is measured in PDF points. An SVG’s natural `Width` and `Height` are in points, with one CSS pixel equal to 0.75 points. The supported sizing choices include contain, cover, stretch, fit width, fit height and a DPI for natural size.

Parse failures happen before layout. Once drawing succeeds, inspect the returned notes or `LayoutDocument.Diagnostics`: a successful save does not mean that every source feature was drawn. The example deliberately includes a script and an external image so you can see those omissions in the real output.

## Does PDF support SVG?

On this path, the PDF contains the translated drawing, not a browser or a script engine. SVG paths, basic shapes, transforms, supported CSS, gradients, patterns and clips have PDF counterparts. Linear and radial gradients become PDF shadings. Complex clips and masks may use soft masks. SVG text is shaped through the font registry and remains extractable.

Unsupported content is not silently turned into a screenshot. Scripts, animation, event attributes, `foreignObject` and external references are dropped with notes. Filters draw their unfiltered content with a note. CSS custom properties are unsupported. An external logo, stylesheet or image therefore needs to be made self-contained using supported features before conversion; adding a URL to the SVG does not fetch it.

## How can I convert an SVG file to a vector PDF?

Use the same layout call with the SVG’s bytes in place of the inline string, then save the document. Keep diagnostics with the conversion result and inspect the PDF for features important to the picture. Text painted with a gradient or pattern becomes one solid color with a note. A gradient stroke with varying stop opacity is drawn opaque; fills can handle varying opacity using a soft mask.

For tagged output, provide useful alternate text. Layout uses the author’s `AltText`, otherwise the SVG’s title, otherwise its description. A figure with none of these and not marked as an artifact records a diagnostic; PDF/UA-1 saving refuses it. The title in this example supplies “Blue badge.” This helps preserve meaning as well as the drawing when the picture is placed into a larger document.
