如何在 C# 中從 Sumatra PDF 遷移到 IronPDF
從蘇門答臘 PDF遷移到 IronPDF:完整的 C# 遷移指南。
從蘇門答臘 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 限制。
開始之前
先決條件
1..NET 環境:.NET Framework 4.6.2+ 或 .NET Core 3.1+ / .NET 5/6/7/8/9+ 2.NuGet存取:安裝 NuGet 套件的能力 3.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";IRON VB CONVERTER ERROR developers@ironsoftware.com完整的 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;IRON VB CONVERTER ERROR developers@ironsoftware.com核心能力對應
| 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);
}
}IRON VB CONVERTER ERROR developers@ironsoftware.comAfter (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!");
}
}IRON VB CONVERTER ERROR developers@ironsoftware.com本範例展示了基本的架構差異。蘇門答臘 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}\""
}
}IRON VB CONVERTER ERROR developers@ironsoftware.comAfter (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 });
}
}IRON VB CONVERTER ERROR developers@ironsoftware.comSumatra 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);
}
}IRON VB CONVERTER ERROR developers@ironsoftware.comAfter (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}");
}
}IRON VB CONVERTER ERROR developers@ironsoftware.comSumatra 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");IRON VB CONVERTER ERROR developers@ironsoftware.comPDF 合併
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");IRON VB CONVERTER ERROR developers@ironsoftware.com水印。
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");IRON VB CONVERTER ERROR developers@ironsoftware.com密碼保護
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");IRON VB CONVERTER ERROR developers@ironsoftware.com遷移清單
預遷移
- [ ] 識別所有 Sumatra 程序啟動 (
Process.Start("SumatraPDF.exe", ...)) - [ ] 文件列印工作流程 (
-print-to-default參數) - [ ] 注意使用的任何 Sumatra 命令列參數
- [ ] 從 ironpdf.com 獲得IronPDF授權金鑰
程式碼更新
- [ ] 安裝
IronPdfNuGet 套件 - [ ] 移除 Sumatra 程序代碼
- [ ] 使用
PdfDocument.FromFile(pdfPath)取代Process.Start("SumatraPDF.exe", pdfPath)。 - [ ] 使用
ChromePdfRenderer.RenderHtmlAsPdf()取代外部wkhtmltopdf.exe呼叫 - [ ] 使用<代碼>pdf.ExtractAllText()</代碼取代外部
pdftotext.exe呼叫 - [ ] 使用<編碼>pdf.Print()</編碼取代
-print-to-default程序呼叫 - [ ] 在應用程式啟動時加入授權初始化
測試
- [ ] 測試 PDF 生成品質
- [ ] 確認列印功能
- [ ] 在所有目標平台上進行測試
- [ ] 確認沒有 Sumatra 相依性保留
清理
- [ ] 從安裝程式中移除 Sumatra
- [ ] 更新文件
- [ ] 從系統需求中移除 Sumatra
結論
總而言之,在蘇門答臘 PDF和IronPDF之間做選擇主要取決於您的需求。 對於需要快速直接 PDF 閱讀器的終端使用者而言,Sumatra PDF 可提供絕佳的使用體驗。 然而,對於需要進階 PDF 操作和整合功能的開發人員和企業而言,IronPDF 卻是一個卓越的選擇。它的函式庫設計、完整的 PDF 功能以及商業授權,讓它成為將 C# 應用程式提升到新高度的強大工具。
本次轉換的主要變更如下 1.架構:外部桌面應用程式 → 原生 .NET 函式庫 2.PDF 創作: 不可能 → ChromePdfRenderer.RenderHtmlAsPdf() 3.PDF載入:Process.Start("SumatraPDF.exe", path) → PdfDocument.FromFile(path) 4.文字萃取:外部 pdftotext.exe →<代碼>pdf.ExtractAllText()</代碼和 pdf.ExtractTextFromPage() 。 5.列印:-print-to-default CLI 參數 → pdf.Print() 6.合併:不可能 → PdfDocument.Merge() 7.Watermarks: 不可能 → pdf.ApplyWatermark() 8.安全性: 不可能 → pdf.SecuritySettings 9.授權:GPL (限制性) → 商業 (彈性) 10.依賴性:使用者必須安裝 Sumatra → 應用程式捆綁的函式庫






