如何使用 C# 將 Sumatra PDF 遷移到 IronPDF
從 蘇門答臘 PDF 遷移到 IronPDF 會將您的 PDF 工作流程從使用桌面檢視器應用程式進行外部流程管理轉變為與原生 .NET 庫集成,從而實現完整的 PDF 創建、操作和提取功能。 本指南提供了一個完整的、循序漸進的遷移路徑,消除了外部依賴項、GPL 許可限制以及 蘇門答臘 PDF 只是一個檢視器而不是一個開發庫的根本限制。
為什麼要從 蘇門答臘 PDF 遷移到 IronPDF
了解蘇門答臘 PDF
Sumatra PDF 主要是一款輕量級的開源 PDF 閱讀器,以其簡潔性和速度而聞名。 但是,Sumatra PDF 除了查看 PDF 文件之外,不提供建立或操作 PDF 文件所需的功能。 作為一款免費且功能全面的 PDF 閱讀器,它深受許多追求簡潔體驗的用戶喜愛。 但是,對於需要更全面的 PDF 功能(例如在應用程式中建立 PDF 和整合 PDF 庫)的開發人員來說,由於其固有的設計限制,Sumatra PDF 就顯得力不從心了。
Sumatra PDF 是一個桌面 PDF 檢視器應用程序,而不是一個開發庫。 如果您在 .NET 應用程式中使用 Sumatra PDF,則很可能:
- 將其作為外部進程啟動以顯示 PDF 文件
- 透過命令列使用它來列印 PDF 文件
- 由於它作為依賴項,您的用戶必須安裝它。
蘇門答臘 PDF 整合的主要問題
| 問題 | 影響 |
|---|---|
| 不是圖書館 | 無法透過程式設計方式建立或編輯PDF文件 |
| 外部流程 | 需要產生單獨的進程 |
| GPL 授權 | 對商業軟體有限制 |
| 使用者依賴性 | 使用者必須單獨安裝 Sumatra。 |
| 無 API | 僅限命令列參數 |
| 僅查看 | 無法建立、編輯或操作PDF文件 |
| 無網路支援 | 僅限桌面應用程式 |
蘇門答臘 PDF 與 IronPDF 對比
| 特徵 | 蘇門答臘 PDF | IronPDF |
|---|---|---|
| 類型 | 應用 | 圖書館 |
| PDF閱讀 | 是的 | 是的 |
| PDF 建立 | 不 | 是的 |
| PDF編輯 | 不 | 是的 |
| 一體化 | 有限(獨立) | 完全整合到應用程式中 |
| 執照 | GPL | 商業的 |
| 建立PDF文件 | 不 | 是的 |
| 編輯PDF文件 | 不 | 是的 |
| HTML 轉 PDF | 不 | 是的 |
| 合併/拆分 | 不 | 是的 |
| 水印 | 不 | 是的 |
| 數位簽名 | 不 | 是的 |
| 表格填寫 | 不 | 是的 |
| 文字擷取 | 不 | 是的 |
| .NET 集成 | 沒有任何 | 本國的 |
| Web應用程式 | 不 | 是的 |
與 蘇門答臘 PDF 不同,IronPDF 不依賴任何特定的桌面應用程式或外部進程。 它為開發人員提供了一個靈活的庫,可以直接在 C# 中動態建立、編輯和操作 PDF 文件。 與外部流程的解耦帶來了一個明顯的優勢——它簡單易用且適應性強,不僅適用於查看,還適用於各種其他應用。
對於計劃在 2025 年和 2026 年採用 .NET 10 和 C# 14 的團隊,IronPDF 提供原生庫集成,消除了 蘇門答臘 PDF 的外部進程開銷和 GPL 許可限制。
開始之前
先決條件
- .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";完整 API 參考
命名空間變更
// Before: 蘇門答臘 PDF (external process)
using System.Diagnostics;
using System.IO;
// After: IronPDF
using IronPdf;// Before: 蘇門答臘 PDF (external process)
using System.Diagnostics;
using System.IO;
// After: IronPDF
using IronPdf;核心能力映射
| 蘇門答臘 PDF 方法 | IronPDF當量 | 筆記 |
|---|---|---|
Process.Start("SumatraPDF.exe", pdfPath) | PdfDocument.FromFile() | 載入PDF |
| 命令列參數 | 原生 API 方法 | 無需命令列介面 |
外部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
之前(蘇門答臘PDF):
// NuGet: Install-Package SumatraPDF (Note: Sumatra is primarily a viewer, not a generator)
// 蘇門答臘 PDF doesn't have direct C# integration for HTML 轉 PDF conversion
// You would need to use external tools or libraries and then open with Sumatra
using System.Diagnostics;
using System.IO;
class Program
{
static void Main()
{
// 蘇門答臘 PDF cannot 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);
}
}// NuGet: Install-Package SumatraPDF (Note: Sumatra is primarily a viewer, not a generator)
// 蘇門答臘 PDF doesn't have direct C# integration for HTML 轉 PDF conversion
// You would need to use external tools or libraries and then open with Sumatra
using System.Diagnostics;
using System.IO;
class Program
{
static void Main()
{
// 蘇門答臘 PDF cannot 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);
}
}(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 is HTML 轉 PDF conversion.</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 is HTML 轉 PDF conversion.</p>";
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs("output.pdf");
Console.WriteLine("PDF created successfully!");
}
}這個例子展示了架構上的根本差異。 蘇門答臘 PDF 無法直接將 HTML 轉換為 PDF——您必須使用 wkhtmltopdf 等外部工具作為中間程序,然後以單獨的進程啟動 Sumatra 來查看結果。 這需要兩個外部可執行檔和多次進程啟動。
IronPDF 使用ChromePdfRenderer和RenderHtmlAsPdf()只需三行程式碼。 無需外部工具,無需流程管理,無需中間文件。 PDF 檔案直接在記憶體中創建,並使用SaveAs()儲存。 請參閱HTML 轉 PDF 文件以取得完整範例。
範例 2:開啟和顯示 PDF 文件
之前(蘇門答臘PDF):
// NuGet: Install-Package SumatraPDF.CommandLine (or direct executable)
using System.Diagnostics;
using System.IO;
class Program
{
static void Main()
{
string pdfPath = "document.pdf";
// 蘇門答臘 PDF excels 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}\""
}
}// NuGet: Install-Package SumatraPDF.CommandLine (or direct executable)
using System.Diagnostics;
using System.IO;
class Program
{
static void Main()
{
string pdfPath = "document.pdf";
// 蘇門答臘 PDF excels 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}\""
}
}(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}");
// 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") { 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}");
// 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") { UseShellExecute = true });
}
}Sumatra PDF 在查看 PDF 檔案方面表現出色,但它僅限於透過命令列參數啟動外部進程。 您無法透過程式設計存取 PDF 內容,只能顯示它。
IronPDF 使用PdfDocument.FromFile()載入 PDF,讓您可以完全透過程式設計方式存取 PDF 檔案。 您可以讀取PageCount等屬性,操作文檔,儲存更改,然後使用系統預設的 PDF 檢視器開啟。 主要區別在於 IronPDF 提供的是真正的 API,而不僅僅是處理參數。 了解更多信息,請閱讀我們的教程。
範例 3:從 PDF 中提取文本
之前(蘇門答臘PDF):
// 蘇門答臘 PDF doesn't provide C# API for text extraction
// You would need to use command-line tools or other libraries
using System.Diagnostics;
using System.IO;
class Program
{
static void Main()
{
// 蘇門答臘 PDF is 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);
}
}// 蘇門答臘 PDF doesn't provide C# API for text extraction
// You would need to use command-line tools or other libraries
using System.Diagnostics;
using System.IO;
class Program
{
static void Main()
{
// 蘇門答臘 PDF is 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);
}
}(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}");
}
}Sumatra PDF 是一個檢視器,而不是文字擷取函式庫。 要提取文本,必須使用外部命令列工具(如pdftotext.exe ,啟動一個進程,等待其完成,讀取輸出文件,並處理所有相關的文件 I/O 和清理工作。
IronPDF 提供原生文本提取功能,可以使用ExtractAllText()提取整個文件的文本,或使用ExtractTextFromPage(0)提取特定頁面的文本。 無需外部進程,無需臨時文件,無需清理。
功能對比
| 特徵 | 蘇門答臘 PDF | IronPDF |
|---|---|---|
| 創建 | ||
| HTML 轉 PDF | 不 | 是的 |
| PDF檔案的URL | 不 | 是的 |
| 文字轉PDF | 不 | 是的 |
| 圖片轉PDF | 不 | 是的 |
| 操縱 | ||
| 合併PDF | 不 | 是的 |
| 拆分PDF | 不 | 是的 |
| 旋轉頁面 | 不 | 是的 |
| 刪除頁面 | 不 | 是的 |
| 頁面重新排序 | 不 | 是的 |
| 內容 | ||
| 添加浮水印 | 不 | 是的 |
| 新增頁首/頁尾 | 不 | 是的 |
| 印章文字 | 不 | 是的 |
| 郵票影像 | 不 | 是的 |
| 安全 | ||
| 密碼保護 | 不 | 是的 |
| 數位簽名 | 不 | 是的 |
| 加密 | 不 | 是的 |
| 權限設定 | 不 | 是的 |
| 萃取 | ||
| 提取文字 | 不 | 是的 |
| 擷取影像 | 不 | 是的 |
| 平台 | ||
| 視窗 | 是的 | 是的 |
| Linux | 不 | 是的 |
| macOS | 不 | 是的 |
| Web應用程式 | 不 | 是的 |
| Azure/AWS | 不 | 是的 |
遷移後的新功能
遷移到 IronPDF 後,您將獲得 蘇門答臘 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");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");水印
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");密碼保護
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");遷移清單
遷移前
- 識別所有 Sumatra 進程啟動(
Process.Start("SumatraPDF.exe", ...)) - 文件列印工作流程(
-print-to-default參數) - 注意所使用的任何 Sumatra 命令列參數。
- 從ironpdf.com取得 IronPDF 許可證金鑰
程式碼更新
安裝IronPdf NuGet 套件
- 刪除蘇門答臘進程代碼
- 將
Process.Start("SumatraPDF.exe", pdfPath)替換為PdfDocument.FromFile(pdfPath) - 將外部
wkhtmltopdf.exe呼叫替換為ChromePdfRenderer.RenderHtmlAsPdf() - 將外部
pdftotext.exe呼叫替換為pdf.ExtractAllText() - 將
-print-to-default進程呼叫替換為pdf.Print() - 在應用程式啟動時新增許可證初始化
測試
- 測試PDF生成質量
- 驗證列印功能
- 在所有目標平台上進行測試
- 確認不再存在對蘇門答臘的依賴。
清理
- 從安裝程式中移除 Sumatra
- 更新文檔
- 從系統需求中移除 Sumatra






