跳至页脚内容
迁移指南

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

从苏门答腊 PDF迁移到 IronPDF:完整的 C# 迁移指南

从苏门答腊 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对比

特征苏门答腊 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

核心能力映射

苏门答腊 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);
    }
}
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 转 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}\""
    }
}
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 内容,只能将其显示出来。

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);
    }
}
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", ...))。
  • [文档打印工作流程(<代码>-打印到默认值</代码>参数)
  • [ ] 注意使用的任何 Sumatra 命令行参数
  • [ ] 从 ironpdf.com 获取IronPDF许可证密钥

代码更新

  • [ ] 安装 IronPdf NuGet 软件包
  • [ ] 删除 Sumatra 流程代码
  • [ ] 将<代码>Process.Start("SumatraPDF.exe", pdfPath)</代码替换为 PdfDocument.FromFile(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.水印:不可能 → pdf.ApplyWatermark() 8.安全性: 不可能 → pdf.SecuritySettings 9.许可:GPL(限制性) → 商业(灵活) 10.依赖性:用户必须安装与应用程序捆绑的 Sumatra → 库

探索完整的IronPDF文档教程API参考,加速您的Sumatra PDF迁移。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。