IRONSOFTWAREHOME

How to Convert PDF to SVG in C#

Ahmad Sohail
Ahmad Sohail
Updated: 2026年7月24日

The PdfToSvg namespace in IronPDF provides a dedicated API for converting PDF pages to scalable vector graphics. SVG (Scalable Vector Graphics) is an XML-based format that stays sharp at any zoom level: unlike raster images (PNG, JPEG), SVG output does not pixelate when scaled up. Converting PDF pages to SVG is useful for embedding document content in web pages, generating resolution-independent previews, and extracting vector graphics from reports, invoices, or technical drawings.

The conversion works per-page: open a PDF with PdfDocument.Open(), access individual pages through the Pages collection, then call SaveAsSvg() or ToSvgString() on each PdfPage to produce SVG output. Async variants are available for all conversion methods.

This is the PDF-to-SVG direction: extracting vector SVG from existing PDF files. For the reverse workflow (embedding SVG graphics into a new PDF), see the SVG to PDF how-to.

Start a free 30-day trial to test PDF-to-SVG conversion.

NuGet使用NuGet安裝

PM > Install-Package IronPdf

Install IronPDF by running the command above in the NuGet Package Manager Console, or search for the package in the NuGet Package Manager.

The example below opens a PDF and saves its first page as an SVG file.

  1. 1Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. 2複製並運行這段程式碼片段。

    using PdfToSvg;
    
    using var pdf = PdfDocument.Open("report.pdf");
    pdf.Pages[0].SaveAsSvg("page-one.svg");
    C#
  3. 3部署以在您的實時環境中測試

    今天就開始在您的專案中使用IronPDF,透過免費試用
    arrow pointer
Quickstart

Minimal Workflow (3 Steps)

  1. Install IronPDF via NuGet: Install-Package IronPdf
  2. Open a PDF with PdfDocument.Open() from the PdfToSvg namespace
  3. Call SaveAsSvg() on any page to write the SVG file

How to Convert a Single PDF Page to SVG?

The PdfPage class provides four conversion methods:

  • SaveAsSvg(): writes the page to a file path or Stream.
  • SaveAsSvgAsync(): the non-blocking variant of SaveAsSvg().
  • ToSvgString(): returns the SVG markup as a string, useful when the output feeds a template engine, an HTTP response, or a downstream pipeline.
  • ToSvgStringAsync(): the non-blocking variant of ToSvgString().

All four accept an optional SvgConversionOptions argument and a CancellationToken.

The example below saves the first page to a file, gets the same page back as a string, and writes it to a stream. It shows all three output targets.

using PdfToSvg;

using var pdf = PdfDocument.Open("invoice.pdf");

// Save to a file
pdf.Pages[0].SaveAsSvg("invoice-page1.svg");

// Get SVG as a string
string svgMarkup = pdf.Pages[0].ToSvgString();
Console.WriteLine($"SVG length: {svgMarkup.Length} characters");

// Save to a stream
using var stream = new FileStream("invoice-page1-stream.svg", FileMode.Create);
pdf.Pages[0].SaveAsSvg(stream);
C#

PdfDocument implements IDisposable, so wrapping it in a using statement releases native resources when the conversion is finished. The Pages collection is zero-indexed, so Pages[0] is the first page.

請注意: PdfToSvg.PdfDocument is a distinct class from IronPdf.PdfDocument; the using PdfToSvg; directive brings it into scope.

When Is PDF to SVG the Right Choice?

SVG is the better output when a page will be zoomed, embedded in a web layout, or restyled after conversion: text stays selectable and searchable, and paths stay editable in vector tools. For scanned or photographic pages there is no vector content to preserve, so a raster export is smaller and loses nothing.

How to Convert All Pages in a PDF to SVG?

The Pages collection is zero-indexed, so adding one to the loop index gives a human-readable page number for each output file.

using PdfToSvg;

using var pdf = PdfDocument.Open("multi-page-report.pdf");

Console.WriteLine($"Total pages: {pdf.Pages.Count}");

foreach (var page in pdf.Pages)
{
    string outputPath = $"page_{page.PageNumber}.svg";
    page.SaveAsSvg(outputPath);
    Console.WriteLine($"Saved: {outputPath}");
}
C#
請注意: SVG does not support multi-page documents the way PDF does, so a 10-page PDF produces 10 individual SVG files.

For large documents, the async approach described in the next section avoids blocking the application thread during conversion.

我最喜歡的程式庫是IronPDF。它允許快速高效地操作PDF文件。它還有許多有價值的功能,例如導出到PDF/A格式和數位簽署PDF文件。

Milan Jovanovic

Microsoft MVP

查看案例研究

IronOCR意味著我們每年可以從手動處理中節省$40,000,同時提高生產力,釋放資源以進行高影響的任務。我會強烈推薦它。

Brent Matzelle

首席技術官,OPYN

查看案例研究

IronSuite在我們的運營中扮演著至關重要的角色。這些工具增加了包括建立平面圖和改善庫存管理在內的業務效率。

David Jones

首席軟體工程師,Agorus Build

查看案例研究

How to Use Async Methods for PDF-to-SVG Conversion?

The async variants, SaveAsSvgAsync() and ToSvgStringAsync(), keep I/O off the main thread. PdfDocument.OpenAsync() loads the PDF asynchronously as well. These methods are the right choice in ASP.NET Core handlers, background services, or any application with a responsive UI.

The example below opens the PDF and converts every page asynchronously, so the calling thread stays free while the work runs.

using PdfToSvg;

await using var pdf = await PdfDocument.OpenAsync("dashboard.pdf");

foreach (var page in pdf.Pages)
{
    await page.SaveAsSvgAsync($"dashboard_page_{page.PageNumber}.svg");
}

// Or get the SVG markup as a string
string firstPageSvg = await pdf.Pages[0].ToSvgStringAsync();
C#

How to Configure SVG Conversion Options?

SvgConversionOptions controls how the SVG output is generated. The most commonly used properties are:

  • IncludeAnnotations: set to true to render PDF annotation layers in the SVG output
  • IncludeLinks: set to true to carry PDF hyperlinks through as <a> elements in the SVG, keeping the output navigable in a browser
  • IncludeHiddenText: surfaces text that exists in the PDF content stream but is not visible, useful for accessibility or text-extraction pipelines
  • MinStrokeWidth: sets a floor (in SVG user units) for rendered strokes, preventing hairline paths from disappearing at normal viewing scales

The code below switches on annotations and links, sets a minimum stroke width, then exports every page using those options.

using PdfToSvg;

var options = new SvgConversionOptions
{
    IncludeAnnotations = true,
    IncludeLinks = true,
    IncludeHiddenText = false,
    MinStrokeWidth = 0.5
};

using var pdf = PdfDocument.Open("annotated-report.pdf");

foreach (var page in pdf.Pages)
{
    page.SaveAsSvg($"page_{page.PageNumber}.svg", options);
}
C#

Two additional properties control how text spacing is handled. CollapseSpaceEmbeddedFont and CollapseSpaceLocalFont set thresholds (as fractions of the font size) below which gaps between adjacent characters are collapsed into a single text run. Tighter values preserve more spacing fidelity; looser values produce smaller SVG files.

For advanced use cases, the FontResolver property accepts a custom resolver that maps PDF font references to local font resources, essential when the source document references fonts that are not embedded. The ImageResolver property controls how raster images within the PDF are handled in the SVG output: inline encoding produces a self-contained SVG file, while external path resolution keeps the SVG compact. The SvgConversionOptions API reference documents every configurable property.

How to Control PDF Layer Visibility in SVG Output?

A layered PDF exposes its layers through the OptionalContentGroups collection on PdfDocument. Each entry is an OptionalContentGroup that pairs a read-only Name (the layer label from the source document) with a get/set Visible flag. Setting Visible controls whether that layer is rendered during the SVG conversion, so a single PDF can produce different SVG variants without editing the file.

You do not need to know the layer names in advance. The collection reflects the layers actually present in the document, so you can enumerate it, read each Name, and switch off the ones you want to hide. ToString() returns the same value as Name, which is convenient for logging.

The example below lists every layer in the PDF, hides the one named Watermark, and exports the first page with that layer suppressed.

using PdfToSvg;

using var pdf = PdfDocument.Open("technical-drawing.pdf");

// List the layers that actually exist in this PDF
foreach (var layer in pdf.OptionalContentGroups)
{
    Console.WriteLine($"{layer.Name} visible: {layer.Visible}");
}

// Hide the watermark layer; every other layer stays visible
foreach (var layer in pdf.OptionalContentGroups)
{
    if (layer.Name == "Watermark")
    {
        layer.Visible = false;
    }
}

// Visibility set on OptionalContentGroups is applied during conversion
pdf.Pages[0].SaveAsSvg("drawing-no-watermark.svg");
C#

A typical use case is a technical drawing, where annotation, dimension, or watermark layers need to be toggled independently. Because visibility is set per conversion, the same source PDF can produce several SVG variants with no changes to the file.

How to Open PDFs from Streams and with Passwords?

PdfDocument.Open() accepts a Stream in addition to a file path. The leaveOpen parameter (default false) controls whether the stream is closed when the document is disposed. Both path and stream overloads accept an OpenOptions object for configuration such as providing a password for encrypted documents, and a CancellationToken for cooperative cancellation in long-running workflows.

The snippet opens a PDF from a stream, saves its first page as SVG, then prints whether the file is encrypted, its title, and its page count.

using PdfToSvg;

// Open from a stream (e.g., downloaded from a web API)
using var fileStream = File.OpenRead("document.pdf");
using var pdf = PdfDocument.Open(fileStream, leaveOpen: false);
pdf.Pages[0].SaveAsSvg("from-stream.svg");

// Check if a PDF is encrypted
Console.WriteLine($"Encrypted: {pdf.IsEncrypted}");

// Access document metadata
Console.WriteLine($"Title: {pdf.Info.Title}");
Console.WriteLine($"Pages: {pdf.Pages.Count}");
C#

The Info property exposes document metadata including title, author, and creation date. The Permissions property reports what operations the PDF allows (printing, copying), and IsEncrypted indicates whether the file requires a password to open.

Next Steps

The PdfToSvg namespace converts PDF pages to SVG files, streams, or strings. It works per-page, with full control over annotations, links, text visibility, stroke width, and layer toggling. All methods have async variants for non-blocking workflows.

Explore the PdfPage API reference for the complete conversion method surface, the SvgConversionOptions reference for every configurable property, and the PdfDocument reference for open and load options. For the reverse direction (embedding SVG into a PDF), see the SVG to PDF how-to.

View licensing options starting at $999.

常見問題

What is the purpose of converting PDF files to SVG format?

Converting PDF files to SVG format allows for embedding document content in web pages, generating resolution-independent previews, and extracting vector graphics from reports, invoices, or technical drawings. SVG is scalable and does not pixelate, making it ideal for web and design applications.

How can IronPDF's PdfToSvg be utilized for a PDF to SVG conversion?

IronPDF's PdfToSvg namespace provides a dedicated API for converting PDF pages to SVG format. You can open a PDF with PdfDocument.Open(), access the individual pages through the Pages collection, and then call SaveAsSvg() or ToSvgString() on each PdfPage to produce SVG output.

Can PDF pages be converted to SVG asynchronously using IronPDF?

Yes, IronPDF supports asynchronous PDF to SVG conversions. You can use the SaveAsSvgAsync() and ToSvgStringAsync() methods to keep the I/O operations off the main thread, which is beneficial in responsive applications.

What are SvgConversionOptions in IronPDF?

SvgConversionOptions in IronPDF control how the SVG output is generated. Options include whether to include annotations, links, and hidden text in the output, as well as setting minimum stroke width. These options allow for customization of the SVG conversion process.

Is it possible to control layer visibility when converting PDF to SVG using IronPDF?

Yes, IronPDF allows control over PDF layer visibility using the OptionalContentGroups collection on PdfDocument. You can toggle visibility for specific layers, which determines whether they are rendered during the SVG conversion.

How does IronPDF handle encrypted PDF files for conversion to SVG?

IronPDF can open encrypted PDF files using the OpenOptions object, which includes providing a password. This allows encrypted PDFs to be converted into SVG after authentication.

What are the benefits of using SVG format for PDFs in web applications?

SVG format is beneficial for web applications because it maintains high-quality, scalable graphics without pixelation, supports selectable and searchable text, and allows for style modifications using CSS or scripting.

Can IronPDF convert all pages of a PDF into individual SVG files?

Yes, IronPDF can convert all pages of a PDF into individual SVG files. By iterating through the Pages collection, you can save each page as a separate SVG file, which is useful for multi-page PDFs.

What are the minimal steps to start converting a PDF to SVG using IronPDF?

To convert a PDF to SVG using IronPDF, you need to install IronPDF via NuGet, open a PDF with PdfDocument.Open() from the PdfToSvg namespace, and call SaveAsSvg() on any page to write the SVG file.

Does IronPDF support opening PDFs from streams for conversion purposes?

Yes, IronPDF supports opening PDFs from streams. You can use PdfDocument.Open() with a Stream input, making it versatile for scenarios where PDFs are not stored as files but are being streamed from other sources.

Ahmad Sohail
全端開發人員

Ahmad是一位擁有C#、Python和網頁技術堅實基礎的全端開發人員。他對構建可擴展的軟體解決方案深感興趣,並喜歡探索設計和功能在現實世界應用中的結合。

...
閱讀更多

準備開始了嗎?

Nuget Downloads 20,809,720版本:2026.9剛剛發布

立即獲取您的免費30天試用密鑰
不需要信用卡或建立賬戶
C# 用於PDF的NuGet程式庫
使用NuGet安裝

版本: 2026.9

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. 在解決方案資源管理器,右鍵點選參考,管理NuGet包
  2. 選擇瀏覽並搜尋"IronPdf"
  3. 選擇套件並安裝
C# PDF DLL
下載DLL

版本: 2026.9

或者點擊此處下載Windows安裝程式。

  1. 下載並解壓IronPDF到類似~/Libs的位置,位於您的解決方案目錄中
  2. 在Visual Studio解決方案資源管理器,右鍵點選參考。選擇瀏覽,"IronPdf.dll"

授權從$999

有問題嗎?聯絡我們的開發團隊。

Key in blue circle

立即免費取得 30 天試用金鑰

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
預訂您的免費現場演示
Booking Badge

受到全球數百萬工程師的信任

Iron Software的客戶標誌
獲取您的無義務諮詢
填寫以下表格或電子郵件sales@ironsoftware.com
您的詳細資訊將始終保密
受到全球數百萬工程師的信任
Iron Software的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立