跳過到頁腳內容
遷移指南

如何在 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 限制。


開始之前

先決條件

  1. .NET環境: .NET Framework 4.6.2+ 或.NET Core 3.1+ / .NET 5/6/7/8/9+
  2. NuGet存取權限:能夠安裝NuGet套件
  3. IronPDF許可證:請從IronPDF取得您的許可證密鑰。

安裝

# 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";
' Add at application startup
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
$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;
Imports System.Diagnostics
Imports System.IO
Imports IronPdf
$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);
    }
}
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
$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!");
    }
}
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
$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}\""
    }
}
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
$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 });
    }
}
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
$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);
    }
}
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
$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}");
    }
}
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
$vbLabelText   $csharpLabel

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")
$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");
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")
$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");
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")
$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");
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")
$vbLabelText   $csharpLabel

遷移清單

預遷移

  • 辨識所有蘇門答臘流程啟動(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

Curtis Chau
技術作家

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

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

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我