如何在 C# 中從 Sumatra PDF 遷移到 IronPDF
從Sumatra PDF遷移到IronPDF,將您的PDF工作流程從使用桌面查看器應用程式的外部過程管理轉變為與具有完整PDF建立、操作和提取功能的本地.NET程式庫整合。 本指南提供一個完整的、逐步的遷移路徑,消除了外部依賴性、AGPLv3共用版權限制和Sumatra PDF是查看器/列印機而非開發程式庫的基本限制。
為什麼從Sumatra PDF遷移到IronPDF
了解Sumatra PDF
Sumatra PDF是一款輕量級、開源的Windows PDF閱讀器/列印機,以其簡單和速度聞名。 它還顯示EPUB、MOBI、CBZ/CBR、FB2、CHM、XPS和DjVu文件。 然而,Sumatra PDF不提供建立或操作PDF文件所需的功能,超出了查看和列印範圍,且沒有官方的.NET SDK或NuGet封裝——社群封裝僅執行SumatraPDF.exe。
Sumatra PDF是一個獨立的Windows桌面查看器/列印機應用程式,而非開發程式庫。 如果您在.NET應用程式中使用Sumatra PDF,您可能會:
- 將
SumatraPDF.exe作為外部過程啟動來顯示PDF - 通過命令列列印PDF (
-print-to-default,-print-to "<printer>") - 將其作為使用者必須安裝的依賴項
Sumatra PDF整合的主要問題
| 問題 | 影響 |
|---|---|
| 不是程式庫 | 無法以程式化方式建立或編輯PDF |
| 外部過程 | 需要生成SumatraPDF.exe; 沒有程式內API |
| AGPLv3授權 | 強烈的共用版權(部分文件採用BSD授權); bundling into closed-source products imposes obligations most commercial vendors avoid |
| 使用者依賴性 | 使用者(或您的安裝程式)必須將SumatraPDF.exe放置在磁碟中 |
| 僅限CLI | 僅限於記錄的命令列參數 |
| 僅查看/列印 | 無法建立、編輯或操作PDF |
| 僅限Windows | 無Linux或macOS版本 |
Sumatra PDF與IronPDF比較
| 功能 | Sumatra PDF | IronPDF |
|---|---|---|
| 型別 | 應用程式 | 程式庫 |
| PDF閱讀 | 是 | 是 |
| PDF建立 | 否 | 是 |
| PDF編輯 | 否 | 是 |
| 整合 | 有限(獨立) | 完整整合至應用程式 |
| 授權 | AGPLv3(部分文件BSD) | 商業 |
| 建立PDF | 否 | 是 |
| 編輯PDF | 否 | 是 |
| HTML 到 PDF | 否 | 是 |
| 合併/分割 | 否 | 是 |
| 水印 | 否 | 是 |
| 數位簽名 | 否 | 是 |
| 填寫表單 | 否 | 是 |
| 文字提取 | 否 | 是 |
| .NET整合 | None | 本機 |
| Web應用程式 | 否 | 是 |
IronPDF,與Sumatra PDF不同,不依賴於任何特定的桌面應用程式或外部過程。 它為開發者提供了一個靈活的程式庫,可以直接在C#中動態建立、編輯和操作PDF文件。 這種不依賴外部過程的優勢是顯著的——它簡單且適應性強,適用於廣泛的應用程式,遠不止於查看。
對於以現代.NET為目標的團隊而言,IronPDF提供了本地程式庫整合,消除了Sumatra PDF的外部過程開銷和AGPLv3共用版權限制。
開始之前
前提條件
- .NET環境:.NET Framework 4.6.2+ 或.NET Core 3.1+ / .NET 5/6/7/8/9+
- NuGet存取: 有能力安裝NuGet套件
- IronPDF授權: 從ironpdf.com獲取您的授權金鑰
安裝
# Install IronPDF
dotnet add package IronPdf
# Install IronPDF
dotnet add package IronPdf
授權配置
// Add at application startup
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
// Add at application startup
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
' Add at application startup
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
完整API參考
名稱空間變更
// Before:Sumatra PDF(external process)
using System.Diagnostics;
using System.IO;
// After: IronPDF
using IronPdf;
// Before:Sumatra PDF(external process)
using System.Diagnostics;
using System.IO;
// After: IronPDF
using IronPdf;
Imports System.Diagnostics
Imports System.IO
Imports IronPdf
核心能力對應
| Sumatra PDF方法 | IronPDF等價 | 注釋 |
|---|---|---|
Process.Start("SumatraPDF.exe", pdfPath) |
PdfDocument.FromFile() |
載入PDF |
| 指令行參數 | 本地API方法 | 不需要CLI |
外部pdftotext.exe |
pdf.ExtractAllText() |
文字提取 |
外部wkhtmltopdf.exe |
renderer.RenderHtmlAsPdf() |
HTML轉PDF |
-print-to-default參數 |
pdf.Print() |
列印 |
| 不可能 | PdfDocument.Merge() |
合併PDF |
| 不可能 | pdf.ApplyWatermark() |
浮水印 |
| 不可能 | pdf.SecuritySettings |
密碼保護 |
程式碼遷移範例
範例1:HTML轉PDF轉換
之前(Sumatra PDF):
//Sumatra PDFis a standaloneWindowsapp (AGPLv3) — there is NO official NuGet package.
// Download SumatraPDF.exe from https://www.sumatrapdfreader.org/and shell out to it.
// Sumatra is a viewer/printer only; it cannot convert HTML to PDF, so you must use a
// separate HTML-to-PDF tool (e.g. wkhtmltopdf) and then view/print with Sumatra.
using System.Diagnostics;
using System.IO;
class Program
{
static void Main()
{
//Sumatra PDFcannot directly convert HTML to PDF
// You'd need to use wkhtmltopdf or similar, then view in Sumatra
string htmlFile = "input.html";
string pdfFile = "output.pdf";
// Using wkhtmltopdf as intermediary
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = "wkhtmltopdf.exe",
Arguments = $"{htmlFile} {pdfFile}",
UseShellExecute = false
};
Process.Start(psi)?.WaitForExit();
// Then open with Sumatra
Process.Start("SumatraPDF.exe", pdfFile);
}
}
//Sumatra PDFis a standaloneWindowsapp (AGPLv3) — there is NO official NuGet package.
// Download SumatraPDF.exe from https://www.sumatrapdfreader.org/and shell out to it.
// Sumatra is a viewer/printer only; it cannot convert HTML to PDF, so you must use a
// separate HTML-to-PDF tool (e.g. wkhtmltopdf) and then view/print with Sumatra.
using System.Diagnostics;
using System.IO;
class Program
{
static void Main()
{
//Sumatra PDFcannot directly convert HTML to PDF
// You'd need to use wkhtmltopdf or similar, then view in Sumatra
string htmlFile = "input.html";
string pdfFile = "output.pdf";
// Using wkhtmltopdf as intermediary
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = "wkhtmltopdf.exe",
Arguments = $"{htmlFile} {pdfFile}",
UseShellExecute = false
};
Process.Start(psi)?.WaitForExit();
// Then open with Sumatra
Process.Start("SumatraPDF.exe", pdfFile);
}
}
Imports System.Diagnostics
Imports System.IO
Module Program
Sub Main()
' Sumatra PDF cannot directly convert HTML to PDF
' You'd need to use wkhtmltopdf or similar, then view in Sumatra
Dim htmlFile As String = "input.html"
Dim pdfFile As String = "output.pdf"
' Using wkhtmltopdf as intermediary
Dim psi As New ProcessStartInfo With {
.FileName = "wkhtmltopdf.exe",
.Arguments = $"{htmlFile} {pdfFile}",
.UseShellExecute = False
}
Process.Start(psi)?.WaitForExit()
' Then open with Sumatra
Process.Start("SumatraPDF.exe", pdfFile)
End Sub
End Module
之後(IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string htmlContent = "<h1>Hello World</h1><p>This isHTML轉PDFconversion.</p>";
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs("output.pdf");
Console.WriteLine("PDF created successfully!");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string htmlContent = "<h1>Hello World</h1><p>This isHTML轉PDFconversion.</p>";
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs("output.pdf");
Console.WriteLine("PDF created successfully!");
}
}
Imports IronPdf
Imports System
Class Program
Shared Sub Main()
Dim renderer = New ChromePdfRenderer()
Dim htmlContent As String = "<h1>Hello World</h1><p>This isHTML轉PDFconversion.</p>"
Dim pdf = renderer.RenderHtmlAsPdf(htmlContent)
pdf.SaveAs("output.pdf")
Console.WriteLine("PDF created successfully!")
End Sub
End Class
此範例說明基本的架構差異。 Sumatra PDF不能直接將HTML轉換為PDF——您必須使用類似wkhtmltopdf的外部工具作為中介,然後啟動Sumatra作為一個單獨的過程來查看結果。 這需要兩個外部可執行文件和多個過程啟動。
IronPDF使用RenderHtmlAsPdf()僅需三行程式碼。 無外部工具,無過程管理,無中介文件。 PDF直接在記憶體中建立並使用SaveAs()保存。 查看更多HTML到PDF文件以獲得完整的範例。
例子2:打開和顯示PDF
之前(Sumatra PDF):
//Sumatra PDFis a standaloneWindowsapp (AGPLv3) — no official NuGet package.
// Download SumatraPDF.exe from https://www.sumatrapdfreader.org/and call it via Process.Start.
// Useful CLI flags: -page <n>, -print-to-default, -print-to "<printer>", -print-settings, -silent.
using System.Diagnostics;
using System.IO;
class Program
{
static void Main()
{
string pdfPath = "document.pdf";
//Sumatra PDFexcels at viewing PDFs
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "SumatraPDF.exe",
Arguments = $"\"{pdfPath}\"",
UseShellExecute = true
};
Process.Start(startInfo);
// Optional: Open specific page
// Arguments = $"-page 5 \"{pdfPath}\""
}
}
//Sumatra PDFis a standaloneWindowsapp (AGPLv3) — no official NuGet package.
// Download SumatraPDF.exe from https://www.sumatrapdfreader.org/and call it via Process.Start.
// Useful CLI flags: -page <n>, -print-to-default, -print-to "<printer>", -print-settings, -silent.
using System.Diagnostics;
using System.IO;
class Program
{
static void Main()
{
string pdfPath = "document.pdf";
//Sumatra PDFexcels at viewing PDFs
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "SumatraPDF.exe",
Arguments = $"\"{pdfPath}\"",
UseShellExecute = true
};
Process.Start(startInfo);
// Optional: Open specific page
// Arguments = $"-page 5 \"{pdfPath}\""
}
}
Imports System.Diagnostics
Imports System.IO
Module Program
Sub Main()
Dim pdfPath As String = "document.pdf"
' Sumatra PDF excels at viewing PDFs
Dim startInfo As New ProcessStartInfo With {
.FileName = "SumatraPDF.exe",
.Arguments = $"""{pdfPath}""",
.UseShellExecute = True
}
Process.Start(startInfo)
' Optional: Open specific page
' startInfo.Arguments = $"-page 5 ""{pdfPath}"""
End Sub
End Module
之後(IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
using System.Diagnostics;
class Program
{
static void Main()
{
var pdf = PdfDocument.FromFile("document.pdf");
// Extract information
Console.WriteLine($"Page Count: {pdf.PageCount}");
//IronPDFcan manipulate and save, then open with default viewer
pdf.SaveAs("modified.pdf");
// Open with default PDF viewer
Process.Start(new ProcessStartInfo("modified.pdf") { UseShellExecute = true });
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
using System.Diagnostics;
class Program
{
static void Main()
{
var pdf = PdfDocument.FromFile("document.pdf");
// Extract information
Console.WriteLine($"Page Count: {pdf.PageCount}");
//IronPDFcan manipulate and save, then open with default viewer
pdf.SaveAs("modified.pdf");
// Open with default PDF viewer
Process.Start(new ProcessStartInfo("modified.pdf") { UseShellExecute = true });
}
}
Imports IronPdf
Imports System
Imports System.Diagnostics
Class Program
Shared Sub Main()
Dim pdf = PdfDocument.FromFile("document.pdf")
' Extract information
Console.WriteLine($"Page Count: {pdf.PageCount}")
' IronPDF can manipulate and save, then open with default viewer
pdf.SaveAs("modified.pdf")
' Open with default PDF viewer
Process.Start(New ProcessStartInfo("modified.pdf") With {.UseShellExecute = True})
End Sub
End Class
Sumatra PDF在查看PDF方面表現出色,但僅限於使用命令列參數啟動外部過程。 您無法以程式化方式存取PDF內容——只能顯示它。
IronPDF用PdfDocument.FromFile()載入PDF,讓您可以完全以程式化方式存取。 您可以讀取屬性如PageCount,操作文件,保存更改,然後用系統的預設PDF查看器打開。 關鍵區別在於IronPDF提供了一個真正的API,而不僅僅是過程參數。 在我們的教程中了解更多。
例子3:從PDF中提取文字
之前(Sumatra PDF):
//Sumatra PDFis a standaloneWindowsviewer (AGPLv3) — no official NuGet package
// and no programmatic text-extraction API. To extract text you must shell out to a
// separate tool such as pdftotext (Xpdf / Poppler).
using System;
using System.Diagnostics;
using System.IO;
class Program
{
static void Main()
{
//Sumatra PDFis a viewer, not a text extraction library
// You'd need to use PDFBox, iTextSharp, or similar for extraction
string pdfFile = "document.pdf";
// This would require external tools like pdftotext
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = "pdftotext.exe",
Arguments = $"{pdfFile} output.txt",
UseShellExecute = false
};
Process.Start(psi)?.WaitForExit();
string extractedText = File.ReadAllText("output.txt");
Console.WriteLine(extractedText);
}
}
//Sumatra PDFis a standaloneWindowsviewer (AGPLv3) — no official NuGet package
// and no programmatic text-extraction API. To extract text you must shell out to a
// separate tool such as pdftotext (Xpdf / Poppler).
using System;
using System.Diagnostics;
using System.IO;
class Program
{
static void Main()
{
//Sumatra PDFis a viewer, not a text extraction library
// You'd need to use PDFBox, iTextSharp, or similar for extraction
string pdfFile = "document.pdf";
// This would require external tools like pdftotext
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = "pdftotext.exe",
Arguments = $"{pdfFile} output.txt",
UseShellExecute = false
};
Process.Start(psi)?.WaitForExit();
string extractedText = File.ReadAllText("output.txt");
Console.WriteLine(extractedText);
}
}
Imports System
Imports System.Diagnostics
Imports System.IO
Module Program
Sub Main()
' Sumatra PDF is a viewer, not a text extraction library
' You'd need to use PDFBox, iTextSharp, or similar for extraction
Dim pdfFile As String = "document.pdf"
' This would require external tools like pdftotext
Dim psi As New ProcessStartInfo With {
.FileName = "pdftotext.exe",
.Arguments = $"{pdfFile} output.txt",
.UseShellExecute = False
}
Process.Start(psi)?.WaitForExit()
Dim extractedText As String = File.ReadAllText("output.txt")
Console.WriteLine(extractedText)
End Sub
End Module
之後(IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
var pdf = PdfDocument.FromFile("document.pdf");
// Extract text from all pages
string allText = pdf.ExtractAllText();
Console.WriteLine("Extracted Text:");
Console.WriteLine(allText);
// Extract text from specific page
string pageText = pdf.ExtractTextFromPage(0);
Console.WriteLine($"\nFirst Page Text:\n{pageText}");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
var pdf = PdfDocument.FromFile("document.pdf");
// Extract text from all pages
string allText = pdf.ExtractAllText();
Console.WriteLine("Extracted Text:");
Console.WriteLine(allText);
// Extract text from specific page
string pageText = pdf.ExtractTextFromPage(0);
Console.WriteLine($"\nFirst Page Text:\n{pageText}");
}
}
Imports IronPdf
Imports System
Class Program
Shared Sub Main()
Dim pdf = PdfDocument.FromFile("document.pdf")
' Extract text from all pages
Dim allText As String = pdf.ExtractAllText()
Console.WriteLine("Extracted Text:")
Console.WriteLine(allText)
' Extract text from specific page
Dim pageText As String = pdf.ExtractTextFromPage(0)
Console.WriteLine(vbCrLf & "First Page Text:" & vbCrLf & pageText)
End Sub
End Class
Sumatra PDF是一個查看器,而不是文字提取程式庫。 要提取文字,您必須使用類似pdftotext.exe的外部命令列工具,生成一個過程,等待其完成,讀取輸出文件,並處理所有相關的文件I/O和清理。
IronPDF提供本地文字提取,使用ExtractTextFromPage(0)提取特定頁面。 無外部過程,無暫存文件,無需清理。
功能比較
| 功能 | Sumatra PDF | IronPDF | |||
|---|---|---|---|---|---|
| :建立: | HTML轉PDF | 否 | 是 | ||
| URL到PDF | 否 | 是 | |||
| 文字轉PDF | 否 | 是 | |||
| 圖片轉PDF | 否 | 是 | |||
| :操作: | 合併PDF | 否 | 是 | ||
| 拆分PDF | 否 | 是 | |||
| 旋轉頁面 | 否 | 是 | |||
| 刪除頁面 | 否 | 是 | |||
| 重排頁面 | 否 | 是 | |||
| :內容: | 新增水印 | 否 | 是 | ||
| 新增頁眉/頁腳 | 否 | 是 | |||
| 文字印章 | 否 | 是 | |||
| 圖片印章 | 否 | 是 | |||
| :安全性: | 密碼保護 | 否 | 是 | ||
| 數位簽名 | 否 | 是 | |||
| 加密 | 否 | 是 | |||
| 權限設置 | 否 | 是 | |||
| :提取: | 提取文字 | 否 | 是 | ||
| 提取圖片 | 否 | 是 | |||
| :平台: | Windows | 是 | 是 | ||
| Linux | 否 | 是 | |||
| macOS | 否 | 是 | |||
| Web應用程式 | 否 | 是 | |||
| Azure/AWS | 否 | 是 |
遷移後的新能力
遷移到IronPDF後,您將獲得Sumatra PDF無法提供的功能:
從HTML建立PDF
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(@"
<html>
<head><style>body { font-family: Arial; }</style></head>
<body>
<h1>Invoice #12345</h1>
<p>Thank you for your purchase.</p>
</body>
</html>");
pdf.SaveAs("invoice.pdf");
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(@"
<html>
<head><style>body { font-family: Arial; }</style></head>
<body>
<h1>Invoice #12345</h1>
<p>Thank you for your purchase.</p>
</body>
</html>");
pdf.SaveAs("invoice.pdf");
Dim renderer = New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("
<html>
<head><style>body { font-family: Arial; }</style></head>
<body>
<h1>Invoice #12345</h1>
<p>Thank you for your purchase.</p>
</body>
</html>")
pdf.SaveAs("invoice.pdf")
PDF合併
var pdf1 = PdfDocument.FromFile("chapter1.pdf");
var pdf2 = PdfDocument.FromFile("chapter2.pdf");
var pdf3 = PdfDocument.FromFile("chapter3.pdf");
var book = PdfDocument.Merge(pdf1, pdf2, pdf3);
book.SaveAs("complete_book.pdf");
var pdf1 = PdfDocument.FromFile("chapter1.pdf");
var pdf2 = PdfDocument.FromFile("chapter2.pdf");
var pdf3 = PdfDocument.FromFile("chapter3.pdf");
var book = PdfDocument.Merge(pdf1, pdf2, pdf3);
book.SaveAs("complete_book.pdf");
Dim pdf1 = PdfDocument.FromFile("chapter1.pdf")
Dim pdf2 = PdfDocument.FromFile("chapter2.pdf")
Dim pdf3 = PdfDocument.FromFile("chapter3.pdf")
Dim book = PdfDocument.Merge(pdf1, pdf2, pdf3)
book.SaveAs("complete_book.pdf")
水印
var pdf = PdfDocument.FromFile("document.pdf");
pdf.ApplyWatermark(@"
<div style='
font-size: 60pt;
color: rgba(255, 0, 0, 0.3);
transform: rotate(-45deg);
'>
CONFIDENTIAL
</div>");
pdf.SaveAs("watermarked.pdf");
var pdf = PdfDocument.FromFile("document.pdf");
pdf.ApplyWatermark(@"
<div style='
font-size: 60pt;
color: rgba(255, 0, 0, 0.3);
transform: rotate(-45deg);
'>
CONFIDENTIAL
</div>");
pdf.SaveAs("watermarked.pdf");
Dim pdf = PdfDocument.FromFile("document.pdf")
pdf.ApplyWatermark("
<div style='
font-size: 60pt;
color: rgba(255, 0, 0, 0.3);
transform: rotate(-45deg);
'>
CONFIDENTIAL
</div>")
pdf.SaveAs("watermarked.pdf")
密碼保護
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Sensitive Data</h1>");
pdf.SecuritySettings.OwnerPassword = "owner123";
pdf.SecuritySettings.UserPassword = "user456";
pdf.SecuritySettings.AllowUserCopyPasteContent = false;
pdf.SecuritySettings.AllowUserPrinting = PdfPrintSecurity.NoPrint;
pdf.SaveAs("protected.pdf");
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Sensitive Data</h1>");
pdf.SecuritySettings.OwnerPassword = "owner123";
pdf.SecuritySettings.UserPassword = "user456";
pdf.SecuritySettings.AllowUserCopyPasteContent = false;
pdf.SecuritySettings.AllowUserPrinting = PdfPrintSecurity.NoPrint;
pdf.SaveAs("protected.pdf");
Dim renderer As New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Sensitive Data</h1>")
pdf.SecuritySettings.OwnerPassword = "owner123"
pdf.SecuritySettings.UserPassword = "user456"
pdf.SecuritySettings.AllowUserCopyPasteContent = False
pdf.SecuritySettings.AllowUserPrinting = PdfPrintSecurity.NoPrint
pdf.SaveAs("protected.pdf")
遷移檢查表
遷移前
- 識別所有Sumatra過程啟動 (
Process.Start("SumatraPDF.exe", ...)) - 記錄列印工作流程 (
-print-to-default參數) - 注意使用的Sumatra命令列參數
- 獲取IronPDF授權金鑰來自ironpdf.com
程式碼更新
- 安裝
IronPdfNuGet封裝 - 移除Sumatra過程程式碼
- 用
Process.Start("SumatraPDF.exe", pdfPath) - 用
wkhtmltopdf.exe調用 - 用
pdftotext.exe調用 - 將
-print-to-default過程調用 - 在應用程式啟動時新增授權初始化
測試
- 測試PDF生成質量
- 驗證列印功能
- 在所有目標平台上進行測試
- 驗證沒有Sumatra依賴性存在
清理
- 從安裝程式中移除Sumatra
- 更新文件
- 從系統要求中移除Sumatra

