如何在 C# 中將 PDF 轉換為 SVG
IronPDF 中的 PdfToSvg 命名空間提供了一個專用的 API,用於將 PDF 頁面轉換為可縮放的向量圖形。SVG(可縮放向量圖形)是一種基於 XML 的格式,在任何縮放級別下都保持清晰:與光柵圖像(PNG、JPEG)不同,SVG 輸出在放大時不會像素化。將 PDF 頁面轉換為 SVG 對於在網頁中嵌入文檔內容、生成分辨率無關的預覽以及從報告、發票或技術圖紙中提取向量圖形非常有用。
轉換是按頁進行的:使用 PdfDocument.Open() 打開 PDF,通過 Pages 集合訪問單個頁面,然後在每個 PdfPage 上調用 SaveAsSvg() 或 ToSvgString() 以生成 SVG 輸出。所有轉換方法都有異步變體。
這是 PDF 到 SVG 的方向:從現有 PDF 文件中提取向量 SVG。對於反向工作流程(將 SVG 圖形嵌入新 PDF),請參閱 SVG 到 PDF 的操作指南。
開始免費 30 天試用 以測試 PDF 到 SVG 的轉換。
下面的示例打開一個 PDF 並將其第一頁保存為 SVG 文件。
-
1Install IronPDF with NuGet Package Manager
-
2複製並運行這段程式碼片段。
using PdfToSvg; using var pdf = PdfDocument.Open("report.pdf"); pdf.Pages[0].SaveAsSvg("page-one.svg");Imports PdfToSvg Using pdf = PdfDocument.Open("report.pdf") pdf.Pages(0).SaveAsSvg("page-one.svg") End UsingImports PdfToSvg Using pdf = PdfDocument.Open("report.pdf") pdf.Pages(0).SaveAsSvg("page-one.svg") End Using -
3部署以在您的實時環境中測試
今天就開始在您的專案中使用IronPDF,透過免費試用
最小工作流程(3 步)
- 通過 NuGet 安裝 IronPDF:
Install-Package IronPdf - 使用
PdfToSvg命名空間中的PdfDocument.Open()打開 PDF - 在任何頁面上調用
SaveAsSvg()以寫入 SVG 文件
如何將單個 PDF 頁面轉換為 SVG?
PdfPage 類提供四種轉換方法:
SaveAsSvg():將頁面寫入文件路徑或Stream。SaveAsSvgAsync():SaveAsSvg()的非阻塞變體。ToSvgString():返回 SVG 標記作為string,當輸出用於模板引擎、HTTP 響應或下游管道時很有用。ToSvgStringAsync():ToSvgString()的非阻塞變體。
所有四種方法都接受可選的 SvgConversionOptions 參數和 CancellationToken。
下面的示例將第一頁保存到文件中,將同一頁作為字符串返回,並將其寫入流。它顯示了所有三個輸出目標。
using PdfToSvg;
using PdfDocument = PdfToSvg.PdfDocument;
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);using PdfToSvg;
using PdfDocument = PdfToSvg.PdfDocument;
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);Imports PdfToSvg
Imports PdfDocument = PdfToSvg.PdfDocument
Using pdf = PdfDocument.Open("invoice.pdf")
' Save to a file
pdf.Pages(0).SaveAsSvg("invoice-page1.svg")
' Get SVG as a string
Dim svgMarkup As String = pdf.Pages(0).ToSvgString()
Console.WriteLine($"SVG length: {svgMarkup.Length} characters")
' Save to a stream
Using stream As New FileStream("invoice-page1-stream.svg", FileMode.Create)
pdf.Pages(0).SaveAsSvg(stream)
End Using
End UsingImports PdfToSvg
Imports PdfDocument = PdfToSvg.PdfDocument
Using pdf = PdfDocument.Open("invoice.pdf")
' Save to a file
pdf.Pages(0).SaveAsSvg("invoice-page1.svg")
' Get SVG as a string
Dim svgMarkup As String = pdf.Pages(0).ToSvgString()
Console.WriteLine($"SVG length: {svgMarkup.Length} characters")
' Save to a stream
Using stream As New FileStream("invoice-page1-stream.svg", FileMode.Create)
pdf.Pages(0).SaveAsSvg(stream)
End Using
End UsingPdfDocument 實現了 IDisposable,因此將其包裝在 using 語句中可以在轉換完成後釋放本機資源。Pages 集合是從零開始索引的,因此 Pages[0] 是第一頁。
PdfToSvg.PdfDocument 是與 IronPdf.PdfDocument 不同的類;using PdfToSvg; 指令將其引入範圍內。什麼時候選擇 PDF 到 SVG 是正確的?
當頁面將被縮放、嵌入到網頁佈局中或在轉換後重新設計時,SVG 是更好的輸出:文本保持可選擇和可搜索,路徑在向量工具中保持可編輯。對於掃描或攝影頁面,沒有需要保留的向量內容,因此光柵導出更小且不會丟失任何內容。
如何將 PDF 中的所有頁面轉換為 SVG?
Pages 集合是從零開始索引的,因此將一添加到循環索引中可以為每個輸出文件提供人類可讀的頁碼。
using PdfToSvg;
using PdfDocument = PdfToSvg.PdfDocument;
using var pdf = PdfDocument.Open("multi-page-report.pdf");
Console.WriteLine($"Total pages: {pdf.Pages.Count}");
for (int i = 0; i < pdf.Pages.Count; i++)
{
var page = pdf.Pages[i];
string outputPath = $"page_{i + 1}.svg";
page.SaveAsSvg(outputPath);
Console.WriteLine($"Saved: {outputPath}");
}using PdfToSvg;
using PdfDocument = PdfToSvg.PdfDocument;
using var pdf = PdfDocument.Open("multi-page-report.pdf");
Console.WriteLine($"Total pages: {pdf.Pages.Count}");
for (int i = 0; i < pdf.Pages.Count; i++)
{
var page = pdf.Pages[i];
string outputPath = $"page_{i + 1}.svg";
page.SaveAsSvg(outputPath);
Console.WriteLine($"Saved: {outputPath}");
}Imports PdfToSvg
Imports PdfDocument = PdfToSvg.PdfDocument
Using pdf = PdfDocument.Open("multi-page-report.pdf")
Console.WriteLine($"Total pages: {pdf.Pages.Count}")
For i As Integer = 0 To pdf.Pages.Count - 1
Dim page = pdf.Pages(i)
Dim outputPath As String = $"page_{i + 1}.svg"
page.SaveAsSvg(outputPath)
Console.WriteLine($"Saved: {outputPath}")
Next
End UsingImports PdfToSvg
Imports PdfDocument = PdfToSvg.PdfDocument
Using pdf = PdfDocument.Open("multi-page-report.pdf")
Console.WriteLine($"Total pages: {pdf.Pages.Count}")
For i As Integer = 0 To pdf.Pages.Count - 1
Dim page = pdf.Pages(i)
Dim outputPath As String = $"page_{i + 1}.svg"
page.SaveAsSvg(outputPath)
Console.WriteLine($"Saved: {outputPath}")
Next
End Using對於大型文檔,下一節中描述的異步方法可以避免在轉換期間阻塞應用程序線程。
如何使用異步方法進行 PDF 到 SVG 的轉換?
異步變體 SaveAsSvgAsync() 和 ToSvgStringAsync() 將 I/O 保持在主線程之外。PdfDocument.OpenAsync() 也會異步加載 PDF。在 ASP.NET Core 處理程序、後台服務或任何具有響應式 UI 的應用程序中,這些方法是正確的選擇。
下面的示例異步打開 PDF 並轉換每個頁面,因此調用線程在工作運行時保持空閒。
using PdfToSvg;
using PdfDocument = PdfToSvg.PdfDocument;
using var pdf = await PdfDocument.OpenAsync("dashboard.pdf");
for (int i = 0; i < pdf.Pages.Count; i++)
{
var page = pdf.Pages[i];
await page.SaveAsSvgAsync($"dashboard_page_{i + 1}.svg");
}
// Or get the SVG markup as a string
string firstPageSvg = await pdf.Pages[0].ToSvgStringAsync();using PdfToSvg;
using PdfDocument = PdfToSvg.PdfDocument;
using var pdf = await PdfDocument.OpenAsync("dashboard.pdf");
for (int i = 0; i < pdf.Pages.Count; i++)
{
var page = pdf.Pages[i];
await page.SaveAsSvgAsync($"dashboard_page_{i + 1}.svg");
}
// Or get the SVG markup as a string
string firstPageSvg = await pdf.Pages[0].ToSvgStringAsync();Imports PdfToSvg
Imports PdfDocument = PdfToSvg.PdfDocument
Using pdf = Await PdfDocument.OpenAsync("dashboard.pdf")
For i As Integer = 0 To pdf.Pages.Count - 1
Dim page = pdf.Pages(i)
Await page.SaveAsSvgAsync($"dashboard_page_{i + 1}.svg")
Next
' Or get the SVG markup as a string
Dim firstPageSvg As String = Await pdf.Pages(0).ToSvgStringAsync()
End UsingImports PdfToSvg
Imports PdfDocument = PdfToSvg.PdfDocument
Using pdf = Await PdfDocument.OpenAsync("dashboard.pdf")
For i As Integer = 0 To pdf.Pages.Count - 1
Dim page = pdf.Pages(i)
Await page.SaveAsSvgAsync($"dashboard_page_{i + 1}.svg")
Next
' Or get the SVG markup as a string
Dim firstPageSvg As String = Await pdf.Pages(0).ToSvgStringAsync()
End Using如何配置 SVG 轉換選項?
SvgConversionOptions 控制如何生成 SVG 輸出。最常用的屬性是:
IncludeAnnotations:設置為true以在 SVG 輸出中渲染 PDF 註釋層IncludeLinks:設置為true以將 PDF 超鏈接作為<a>元素傳遞到 SVG 中,保持輸出在瀏覽器中可導航IncludeHiddenText:顯示存在於 PDF 內容流中但不可見的文本,對於可訪問性或文本提取管道很有用MinStrokeWidth:設置渲染筆劃的下限(以 SVG 用戶單位),防止細線路徑在正常查看比例下消失
下面的代碼打開註釋和鏈接,設置最小筆劃寬度,然後使用這些選項導出每個頁面。
using PdfToSvg;
using PdfDocument = PdfToSvg.PdfDocument;
var options = new SvgConversionOptions
{
IncludeAnnotations = true,
IncludeLinks = true,
IncludeHiddenText = false,
MinStrokeWidth = 0.5
};
using var pdf = PdfDocument.Open("annotated-report.pdf");
for (int i = 0; i < pdf.Pages.Count; i++)
{
var page = pdf.Pages[i];
page.SaveAsSvg($"page_{i + 1}.svg", options);
}using PdfToSvg;
using PdfDocument = PdfToSvg.PdfDocument;
var options = new SvgConversionOptions
{
IncludeAnnotations = true,
IncludeLinks = true,
IncludeHiddenText = false,
MinStrokeWidth = 0.5
};
using var pdf = PdfDocument.Open("annotated-report.pdf");
for (int i = 0; i < pdf.Pages.Count; i++)
{
var page = pdf.Pages[i];
page.SaveAsSvg($"page_{i + 1}.svg", options);
}Imports PdfToSvg
Imports PdfDocument = PdfToSvg.PdfDocument
Dim options = New SvgConversionOptions With {
.IncludeAnnotations = True,
.IncludeLinks = True,
.IncludeHiddenText = False,
.MinStrokeWidth = 0.5
}
Using pdf = PdfDocument.Open("annotated-report.pdf")
For i As Integer = 0 To pdf.Pages.Count - 1
Dim page = pdf.Pages(i)
page.SaveAsSvg($"page_{i + 1}.svg", options)
Next
End UsingImports PdfToSvg
Imports PdfDocument = PdfToSvg.PdfDocument
Dim options = New SvgConversionOptions With {
.IncludeAnnotations = True,
.IncludeLinks = True,
.IncludeHiddenText = False,
.MinStrokeWidth = 0.5
}
Using pdf = PdfDocument.Open("annotated-report.pdf")
For i As Integer = 0 To pdf.Pages.Count - 1
Dim page = pdf.Pages(i)
page.SaveAsSvg($"page_{i + 1}.svg", options)
Next
End Using另外兩個屬性控制如何處理文本間距。CollapseSpaceEmbeddedFont 和 CollapseSpaceLocalFont 設置閾值(作為字體大小的分數),低於該閾值時,將相鄰字符之間的間隙折疊為單個文本運行。更緊的值保留更多的間距保真度;更鬆的值生成更小的 SVG 文件。
對於高級用例,FontResolver 屬性接受自定義解析器,將 PDF 字體引用映射到本地字體資源,這在源文檔引用未嵌入的字體時至關重要。ImageResolver 屬性控制如何在 SVG 輸出中處理 PDF 中的光柵圖像:內聯編碼生成自包含的 SVG 文件,而外部路徑解析保持 SVG 緊湊。SvgConversionOptions API 參考 記錄了每個可配置屬性。
如何控制 SVG 輸出中的 PDF 層可見性?
分層 PDF 通過 PdfDocument 上的 OptionalContentGroups 集合公開其層。每個條目都是一個 OptionalContentGroup,它將只讀 Name(源文檔中的層標籤)與 get/set Visible 標誌配對。設置 Visible 控制該層在 SVG 轉換期間是否呈現,因此單個 PDF 可以在不編輯文件的情況下生成不同的 SVG 變體。
您不需要提前知道層名稱。該集合反映了文檔中實際存在的層,因此您可以枚舉它,讀取每個 Name,並關閉您想要隱藏的層。ToString() 返回與 Name 相同的值,這對於日誌記錄很方便。
下面的示例列出了 PDF 中的每個層,隱藏名為 Watermark 的層,並導出第一頁,該層被抑制。
using PdfToSvg;
using PdfDocument = PdfToSvg.PdfDocument;
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");using PdfToSvg;
using PdfDocument = PdfToSvg.PdfDocument;
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");Imports PdfToSvg
Imports PdfDocument = PdfToSvg.PdfDocument
Using pdf = PdfDocument.Open("technical-drawing.pdf")
' List the layers that actually exist in this PDF
For Each layer In pdf.OptionalContentGroups
Console.WriteLine($"{layer.Name} visible: {layer.Visible}")
Next
' Hide the watermark layer; every other layer stays visible
For Each layer In pdf.OptionalContentGroups
If layer.Name = "Watermark" Then
layer.Visible = False
End If
Next
' Visibility set on OptionalContentGroups is applied during conversion
pdf.Pages(0).SaveAsSvg("drawing-no-watermark.svg")
End UsingImports PdfToSvg
Imports PdfDocument = PdfToSvg.PdfDocument
Using pdf = PdfDocument.Open("technical-drawing.pdf")
' List the layers that actually exist in this PDF
For Each layer In pdf.OptionalContentGroups
Console.WriteLine($"{layer.Name} visible: {layer.Visible}")
Next
' Hide the watermark layer; every other layer stays visible
For Each layer In pdf.OptionalContentGroups
If layer.Name = "Watermark" Then
layer.Visible = False
End If
Next
' Visibility set on OptionalContentGroups is applied during conversion
pdf.Pages(0).SaveAsSvg("drawing-no-watermark.svg")
End Using一個典型的用例是技術圖紙,其中註釋、尺寸或水印層需要獨立切換。由於可見性是按轉換設置的,因此相同的源 PDF 可以生成多個 SVG 變體而不更改文件。
如何從流中打開 PDF 並使用密碼?
PdfDocument.Open() 接受 Stream 除了文件路徑。leaveOpen 參數(默認為 false)控制文檔處置時流是否關閉。路徑和流重載都接受 OpenOptions 對象,用於配置,例如為加密文檔提供密碼,以及在長時間運行的工作流程中進行協作取消的 CancellationToken。
該片段從流中打開 PDF,將其第一頁保存為 SVG,然後打印文件是否加密、其標題及其頁數。
using PdfToSvg;
using PdfDocument = PdfToSvg.PdfDocument;
// 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}");using PdfToSvg;
using PdfDocument = PdfToSvg.PdfDocument;
// 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}");Imports PdfToSvg
Imports PdfDocument = PdfToSvg.PdfDocument
' Open from a stream (e.g., downloaded from a web API)
Using fileStream = File.OpenRead("document.pdf")
Using 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}")
End Using
End UsingImports PdfToSvg
Imports PdfDocument = PdfToSvg.PdfDocument
' Open from a stream (e.g., downloaded from a web API)
Using fileStream = File.OpenRead("document.pdf")
Using 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}")
End Using
End UsingInfo 屬性公開 文檔元數據,包括標題、作者和創建日期。Permissions 屬性報告 PDF 允許的操作(打印、複製),IsEncrypted 指示文件是否需要密碼才能打開。
下一步
PdfToSvg 命名空間將 PDF 頁面轉換為 SVG 文件、流或字符串。它按頁工作,完全控制註釋、鏈接、文本可見性、筆劃寬度和層切換。所有方法都有異步變體,用於非阻塞工作流程。
探索 PdfPage API 參考 以獲取完整的轉換方法界面,SvgConversionOptions 參考 以獲取每個可配置屬性,以及 PdfDocument 參考 以獲取打開和加載選項。對於反向方向(將 SVG 嵌入 PDF),請參閱 SVG 到 PDF 的操作指南。
查看許可選項 起價 $999。
常見問題
將 PDF 文件轉換為 SVG 格式的目的何在?
將 PDF 文件轉換為 SVG 格式允許在網頁中嵌入文檔內容,生成分辨率獨立的預覽,並從報告、發票或技術圖紙中提取矢量圖形。SVG 是可擴展的,不會像素化,非常適合網頁和設計應用。
如何利用 IronPDF 的 PdfToSvg 進行 PDF 轉換為 SVG?
IronPDF 的 PdfToSvg 命名空間提供了一個專用的 API 用於將 PDF 頁面轉換為 SVG 格式。您可以使用 PdfDocument.Open() 打開 PDF,通過 Pages 集合訪問各個頁面,然後在每個 PdfPage 上調用 SaveAsSvg() 或 ToSvgString() 以生成 SVG 輸出。
使用 IronPDF 可以異步將 PDF 頁面轉換為 SVG 嗎?
是的,IronPDF 支持異步 PDF 轉換為 SVG。您可以使用 SaveAsSvgAsync() 和 ToSvgStringAsync() 方法將 I/O 操作保持在主線程之外,這在響應式應用程序中非常有用。
IronPDF 中的 SvgConversionOptions 是什麼?
IronPDF 中的 SvgConversionOptions 控制 SVG 輸出的生成方式。選項包括是否在輸出中包含註釋、鏈接和隱藏文本,以及設置最小筆劃寬度。這些選項允許自定義 SVG 轉換過程。
使用 IronPDF 將 PDF 轉換為 SVG 時可以控制圖層可見性嗎?
是的,IronPDF 允許使用 PdfDocument 上的 OptionalContentGroups 集合控制 PDF 圖層的可見性。您可以切換特定圖層的可見性,這決定了它們在 SVG 轉換過程中是否被渲染。
IronPDF 如何處理加密的 PDF 文件以進行 SVG 轉換?
IronPDF 可以使用 OpenOptions 對象打開加密的 PDF 文件,其中包括提供密碼。這允許加密的 PDF 在身份驗證後轉換為 SVG。
在網絡應用中使用 SVG 格式的 PDF 有什麼好處?
SVG 格式對於網絡應用有益,因為它保持高質量、可擴展的圖形而不會像素化,支持可選擇和可搜索的文本,並允許使用 CSS 或腳本進行樣式修改。
IronPDF 可以將 PDF 的所有頁面轉換為單獨的 SVG 文件嗎?
是的,IronPDF 可以將 PDF 的所有頁面轉換為單獨的 SVG 文件。通過遍歷 Pages 集合,您可以將每個頁面保存為單獨的 SVG 文件,這對於多頁 PDF 很有用。
使用 IronPDF 開始將 PDF 轉換為 SVG 的最小步驟是什麼?
要使用 IronPDF 將 PDF 轉換為 SVG,您需要通過 NuGet 安裝 IronPDF,使用 PdfToSvg 命名空間中的 PdfDocument.Open() 打開 PDF,然後在任何頁面上調用 SaveAsSvg() 以寫入 SVG 文件。
IronPDF 支持從流中打開 PDF 以進行轉換嗎?
是的,IronPDF 支持從流中打開 PDF。您可以使用 PdfDocument.Open() 與流輸入一起使用,這使其在 PDF 不作為文件存儲而是從其他來源流式傳輸的情況下非常靈活。