跳至页脚内容
迁移指南

如何用 C# 从 Sumatra PDF 迁移到 IronPDF

从苏门答腊 PDF迁移到IronPDF可将您的 PDF 工作流程从使用桌面查看器应用程序的外部流程管理转变为具有完整 PDF 创建、操作和提取功能的本地 .NET 库集成。 本指南提供了一个完整的、循序渐进的迁移路径,消除了外部依赖性、GPL 许可证限制以及苏门答腊 PDF是查看器而非开发库这一基本限制。

为什么要从苏门答腊 PDF迁移到 IronPDF.

了解苏门答腊 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
SHELL

许可配置

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

核心能力映射

苏门答腊 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 的转换

之前(苏门答腊 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);
    }
}
$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!");
    }
}
$vbLabelText   $csharpLabel

本例展示了基本的架构差异。苏门答腊 PDF无法直接将 HTML 转换为 PDF,您必须使用 wkhtmltopdf 等外部工具作为中介,然后作为一个单独的进程启动 Sumatra 以查看结果。 这需要两个外部可执行文件和多个进程启动。

IronPDF 使用 ChromePdfRendererRenderHtmlAsPdf() 仅三行代码。 无外部工具、无流程管理、无中间文件。 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";

        //苏门答腊 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}\""
    }
}
$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 });
    }
}
$vbLabelText   $csharpLabel

Sumatra PDF 擅长查看 PDF,但仅限于使用命令行参数启动外部进程。 您不能以编程方式访问 PDF 内容,只能将其显示出来。

IronPDF 通过PdfDocument.FromFile()加载 PDF,为您提供完全的编程访问。 您可以读取 PageCount 等属性,操作文档,保存更改,然后使用系统默认的 PDF 查看器打开。 关键区别在于IronPDF提供的是实际的 API,而不仅仅是流程参数。 在我们的教程中了解更多信息。

示例 3:从 PDF 中提取文本

之前(苏门答腊 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);
    }
}
$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}");
    }
}
$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");
$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");
$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");
$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");
$vbLabelText   $csharpLabel

迁移清单

迁移前

  • 识别所有 Sumatra 进程启动( Process.Start(&quot;SumatraPDF.exe&quot;, ...)
  • 文档打印工作流程( -print-to-default参数)
  • 注意所使用的任何 Sumatra 命令行参数。
  • ironpdf.com获取IronPDF许可证密钥

代码更新

安装IronPdf NuGet 包

  • 删除苏门答腊进程代码
  • Process.Start(&quot;SumatraPDF.exe&quot;, 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 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。