跳過到頁腳內容
遷移指南

如何在 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的比較

特點蘇門答臘 PDFIronPDF
類型應用程式圖書館
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
SHELL

授權組態

// 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
$vbLabelText   $csharpLabel

完整的 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
$vbLabelText   $csharpLabel

核心能力對應

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.com
$vbLabelText   $csharpLabel

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!");
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

本範例展示了基本的架構差異。蘇門答臘 PDF無法直接將 HTML 轉換為 PDF,您必須使用 wkhtmltopdf 之類的外部工具作為中介,然後以單獨的程序啟動 Sumatra 來檢視結果。 這需要兩個外部執行檔和多個程序啟動。

IronPDF 使用 ChromePdfRendererRenderHtmlAsPdf() 僅僅三行代碼。 無外部工具、無流程管理、無中介檔案。 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.com
$vbLabelText   $csharpLabel

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 });
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

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);
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

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}");
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Sumatra PDF 是一個檢視器,而非文字擷取庫。 若要抽取文字,您必須使用外部指令列工具,例如 pdftotext.exe ,產生一個進程,等待進程完成,讀取輸出檔案,並處理所有相關的檔案 I/O 與清理工作。

IronPDF 提供原生文字擷取功能,可使用 ExtractAllText() 擷取整個文件的文字,或使用 ExtractTextFromPage(0) 擷取特定頁面的文字。 無外部程序、無暫存檔案、無清理需求。


功能比較

特點蘇門答臘 PDFIronPDF
創作
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.com
$vbLabelText   $csharpLabel

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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

水印。

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
$vbLabelText   $csharpLabel

密碼保護

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
$vbLabelText   $csharpLabel

遷移清單

預遷移

  • [ ] 識別所有 Sumatra 程序啟動 (Process.Start("SumatraPDF.exe", ...))
  • [ ] 文件列印工作流程 (-print-to-default 參數)
  • [ ] 注意使用的任何 Sumatra 命令列參數
  • [ ] 從 ironpdf.com 獲得IronPDF授權金鑰

程式碼更新

  • [ ] 安裝 IronPdf NuGet 套件
  • [ ] 移除 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 → 應用程式捆綁的函式庫

探索完整的 IronPDF文件教程API參考,加速您的Sumatra PDF遷移。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。