如何在 C# 中從 Sumatra PDF 遷移到 IronPDF
從蘇門答臘 PDF遷移到 IronPDF,可將您的 PDF 工作流程從使用桌面檢視器應用程式的外部流程管理,轉換為具有完整 PDF 建立、操作和擷取功能的原生 .NET 圖庫整合。 本指南提供完整的逐步遷移路徑,排除外部依賴、GPL 授權限制,以及蘇門答臘 PDF是檢視器而非開發庫的基本限制。
為什麼要從蘇門答臘 PDF遷移到 IronPDF?
瞭解 Sumatra PDF
Sumatra PDF 主要是一款輕量級的開放原始碼 PDF 閱讀器,以簡單快速著稱。 然而,Sumatra PDF 並未提供除了檢視 PDF 檔案之外,建立或處理 PDF 檔案所需的功能。 作為閱讀 PDF 的免費且多功能的選擇,它受到許多追求簡潔體驗的使用者的喜愛。 但是,當開發人員需要更全面的 PDF 功能(例如在應用程式中建立和整合資料庫)時,Sumatra PDF 因其固有的設計限制而無法滿足需求。
Sumatra PDF 是 桌面 PDF 檢視器應用程式,而非開發資料庫。 如果您在 .NET 應用程式中使用 Sumatra PDF,您很可能:
1.將其作為外部程序啟動以顯示 PDF 2.使用它透過指令列列印 PDF 3.依賴它作為您的使用者必須安裝的依賴物
蘇門答臘 PDF整合的主要問題
| 問題 | 影響力 |
|---|---|
| 不是圖書館 | 無法程式化地建立或編輯 PDF |
| 外部流程 | 需要產生獨立的進程 |
| GPL 授權條款。 | 對商業軟體有限制 |
| 使用者依賴性 | 使用者必須另外安裝 Sumatra |
| 無 API | 僅限於命令列參數 |
| 僅供檢視 | 無法建立、編輯或處理 PDF |
| 無網頁支援 | 純桌面應用程式 |
蘇門答臘 PDF與IronPDF的比較
| 特點 | 蘇門答臘 PDF | IronPDF |
|---|---|---|
| 類型 | 應用程式 | 圖書館 |
| PDF閱讀 | 是 | 是 |
| PDF製作 | 無 | 是 |
| PDF編輯 | 無 | 是 |
| 整合。 | 有限(獨立) | 在應用程式中完全整合 |
| 執照 | GPL | 商業的 |
| 建立 PDF 文件 | 無 | 是 |
| 編輯 PDF 文件 | 無 | 是 |
| HTML至PDF | 無 | 是 |
| 合併/分割 | 無 | 是 |
| 水印。 | 無 | 是 |
| 數位簽名 | 無 | 是 |
| 表格填寫 | 無 | 是 |
| 文字萃取 | 無 | 是 |
| .NET 整合。 | 無 | 原生語言 |
| 網路應用程式 | 無 | 是 |
IronPDF 與蘇門答臘 PDF不同,不與任何特定的桌面應用程式或外部程序綁定。 它為開發人員提供了一個靈活的函式庫,可直接在 C# 中動態建立、編輯和處理 PDF 文件。 這種與外部流程解耦的方式提供了一個明顯的優勢 - 簡單直接、適應性強,除了觀看之外,還適用於廣泛的應用。
對於計劃在 2025 年和 2026 年之前採用 .NET 10 和 C# 14 的團隊而言,IronPDF 可提供原生庫整合,省去蘇門答臘 PDF的外部程序開銷和GPLLicense 限制。
開始之前
先決條件
- .NET環境: .NET Framework 4.6.2+ 或.NET Core 3.1+ / .NET 5/6/7/8/9+
- NuGet存取權限:能夠安裝NuGet套件
- IronPDF許可證:請從IronPDF取得您的許可證密鑰。
安裝
# 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:蘇門答臘 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;
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):
// NuGet: Install-Package SumatraPDF (Note: Sumatra is primarily a viewer, not a generator)
//蘇門答臘 PDFdoesn't have direct C# integration forHTML 至 PDFconversion
// 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()
{
//蘇門答臘 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);
}
}
// NuGet: Install-Package SumatraPDF (Note: Sumatra is primarily a viewer, not a generator)
//蘇門答臘 PDFdoesn't have direct C# integration forHTML 至 PDFconversion
// 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()
{
//蘇門答臘 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()
' 蘇門答臘 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
After (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
本範例展示了基本的架構差異。蘇門答臘 PDF無法直接將 HTML 轉換為 PDF,您必須使用 wkhtmltopdf 之類的外部工具作為中介,然後以單獨的程序啟動 Sumatra 來檢視結果。 這需要兩個外部執行檔和多個程序啟動。
IronPDF 只用三行程式碼就實現了 ChromePdfRenderer 與 RenderHtmlAsPdf() 的整合。 無外部工具、無流程管理、無中介檔案。 PDF 直接在記憶體中創建,並以 SaveAs() 儲存。 請參閱 HTML to PDF 文件,以瞭解全面的範例。
範例 2:開啟和顯示 PDFs
之前 (Sumatra PDF):
// NuGet: Install-Package SumatraPDF.CommandLine (or direct executable)
using System.Diagnostics;
using System.IO;
class Program
{
static void Main()
{
string pdfPath = "document.pdf";
//蘇門答臘 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}\""
}
}
// NuGet: Install-Package SumatraPDF.CommandLine (or direct executable)
using System.Diagnostics;
using System.IO;
class Program
{
static void Main()
{
string pdfPath = "document.pdf";
//蘇門答臘 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"
'蘇門答臘 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
' .Arguments = $"-page 5 ""{pdfPath}"""
End Sub
End Module
After (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 內容,只能顯示 PDF 內容。
IronPDF 使用 PdfDocument.FromFile() 載入 PDF,為您提供完整的程式存取權。 您可以讀取類似 PageCount 的屬性,操作文檔,儲存更改,然後使用系統的預設 PDF 檢視器開啟。 關鍵差異在於IronPDF提供實際的 API,而非僅是處理參數。 請參閱我們的 教學,瞭解更多資訊。
範例 3:從 PDF 擷取文字
之前 (Sumatra PDF):
//蘇門答臘 PDFdoesn'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()
{
//蘇門答臘 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);
}
}
//蘇門答臘 PDFdoesn'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()
{
//蘇門答臘 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.Diagnostics
Imports System.IO
Class Program
Shared Sub Main()
'蘇門答臘 PDFis 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 Class
After (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 提供原生文本提取功能,使用 ExtractAllText() 提取整個文件的文本,或使用 ExtractTextFromPage(0) 提取特定頁面的文本。 無外部程序、無暫存檔案、無清理需求。
功能比較
| 特點 | 蘇門答臘 PDF | IronPDF | |||
|---|---|---|---|---|---|
| :創造: | HTML 至 PDF | 無 | 是 | ||
| URL 至 PDF | 無 | 是 | |||
| 文字至 PDF | 無 | 是 | |||
| 圖片至 PDF | 無 | 是 | |||
| 操縱 | 合併 PDF | 無 | 是 | ||
| 分割 PDF | 無 | 是 | |||
| 旋轉頁面 | 無 | 是 | |||
| 刪除頁面 | 無 | 是 | |||
| 重新排序頁面 | 無 | 是 | |||
| :內容: | 添加水印 | 無 | 是 | ||
| 新增標題/頁腳 | 無 | 是 | |||
| 圖章文字 | 無 | 是 | |||
| 圖章 | 無 | 是 | |||
| :安全: | 密碼保護 | 無 | 是 | ||
| 數位簽名 | 無 | 是 | |||
| 加密 | 無 | 是 | |||
| 權限設定 | 無 | 是 | |||
| 提取: | 擷取文字 | 無 | 是 | ||
| 擷取圖片 | 無 | 是 | |||
| :平台: | 視窗 | 是 | 是 | ||
| Linux | 無 | 是 | |||
| MacOS | 無 | 是 | |||
| 網路應用程式 | 無 | 是 | |||
| 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");
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")
遷移清單
預遷移
- 辨識所有蘇門答臘流程啟動(
Process.Start("SumatraPDF.exe", ...)) - 文件列印工作流程(
-print-to-default參數) - 注意所使用的任何 Sumatra 命令列參數。
- 從ironpdf.com取得IronPDF許可證金鑰
程式碼更新
- 安裝
IronPdfNuGet 套件 - 刪除蘇門答臘進程代碼
- 將
Process.Start("SumatraPDF.exe", pdfPath)替換為PdfDocument.FromFile(pdfPath) - 將外部
wkhtmltopdf.exe呼叫替換為ChromePdfRenderer.RenderHtmlAsPdf() - 將外部
pdftotext.exe呼叫替換為pdf.ExtractAllText() - 將
-print-to-default進程呼叫替換為pdf.Print() - 在應用程式啟動時新增許可證初始化
測試
- 測試PDF生成質量
- 驗證列印功能
- 在所有目標平台上進行測試
- 確認不再存在對蘇門答臘的依賴。
清理
- 從安裝程式中移除 Sumatra
- 更新文件
- 從系統需求中移除 Sumatra

