跳至頁尾內容
移民指南

如何在 C# 中將 Ghostscript GPL 遷移到 IronPDF

從 Ghostscript GPL 遷移到 IronPDF 會將您的 .NET PDF 工作流程從命令列進程產生和基於字串的開關操作轉變為類型安全的、支援 IntelliSense 的原生 .NET API。 本指南為專業的 .NET 開發人員提供了一個全面的、逐步的遷移路徑,消除了 AGPL 許可問題和外部二元依賴項。

為什麼要從 Ghostscript GPL 遷移到 IronPDF

Ghostscript GPL挑戰賽

Ghostscript GPL 是一款歷史悠久的 PostScript/PDF 解釋器,但將其用於現代 .NET 應用程式面臨著重大挑戰:

  1. AGPL 授權限制: Ghostscript GPL 的 AGPL 授權要求,如果您散佈使用該授權的軟體,則必須發佈您的原始程式碼——除非您從 Artifex 購買昂貴的商業授權。 這種"病毒式"授權模式會為專有應用程式帶來重大的法律風險。

2.命令列介面: Ghostscript GPL 本質上就是一個命令列工具。 從 C# 中使用它需要產生進程、傳遞字串參數和解析輸出——這是一種脆弱且容易出錯的方法。

3.外部二進位依賴項:您必須單獨安裝 Ghostscript GPL,管理 PATH 變量,並確保跨部署環境的版本相容性。 32 位元和 64 位元系統需要不同的 DLL 檔案( gsdll32.dllgsdll64.dll )。

4.不支援原生 HTML 轉 PDF: Ghostscript GPL 無法直接將 HTML 轉換為 PDF。 您需要先使用另一個工具將 HTML 轉換為 PostScript,然後使用 Ghostscript GPL 將 PostScript 轉換為 PDF——這是一個具有外部相依性的多步驟流程。

5.複雜的開關語法:操作透過晦澀難懂的命令列開關進行控制,例如-dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=... 。 沒有智慧感知功能,沒有輸入安全功能,容易輸錯字。

6.錯誤處理:錯誤以文字字串的形式透過 stderr 輸出,需要進行解析而不是結構化的異常處理。

7.進程管理開銷:每個操作都會產生一個單獨的進程,這增加了錯誤處理、逾時和資源清理的開銷和複雜性。

Ghostscript GPL 與 IronPDF 的比較

方面Ghostscript GPLIronPDF
執照AGPL(病毒式傳播)或昂貴的商業用途商業條款清晰明了。
一體化命令列進程生成原生 .NET 函式庫
API設計基於字串的開關類型化、支援 IntelliSense 的 API
錯誤處理解析 stderr 文本.NET 異常
HTML 轉 PDF不支援(需要外部工具)內建鉻合金引擎
依賴關係外部二進位安裝自包含的 NuGet 套件
部署配置 PATH 環境變量,複製 DLL 文件只需加入 NuGet 引用即可
螺紋安全僅過程隔離螺紋安全設計
近代 .NET支持有限完全支援 .NET 6/7/8/9/10
非同步支援基於流程原生 async/await

對於計劃在 2025 年和 2026 年採用 .NET 10 和 C# 14 的團隊,IronPDF 提供了一個面向未來的基礎,可以與現代 .NET 模式原生整合。


遷移複雜度評估

各功能預計工作量

特徵遷移複雜性筆記
PDF 轉影像低的直接 API 映射
合併PDF低的IronPDF 讓一切變得更簡單
壓縮 PDF低的內建選項
PDF優化低的不同的方法
加密中等的不同的API
頁面擷取低的0索引與1索引
PostScript 轉 PDF中高首先將 PS 文件轉換為 PDF 文件
客製化開關中高研究等效特徵

範式轉移

Ghostscript GPL 遷移的根本轉變在於從命令列程序執行轉變類型化的 .NET API 呼叫:

Ghostscript GPL:"將這些字串開關傳遞給外部程序"
IronPDF:"在 .NET 物件上呼叫這些方法"

開始之前

先決條件

  1. .NET 版本: IronPDF 支援 .NET Framework 4.6.2+ 和 .NET Core 2.0+ / .NET 5/6/7/8/9+ 2.許可證密鑰:ironpdf.com取得您的 IronPDF 許可證密鑰。 3.備份:建立一個用於遷移工作的分支

識別所有 Ghostscript GPL 使用情況

# Find all Ghostscript.NET references
grep -r "Ghostscript\.NET\|GhostscriptProcessor\|GhostscriptRasterizer\|gsdll" --include="*.cs" .

# Find direct process calls to Ghostscript
grep -r "gswin64c\|gswin32c\|gs\|ProcessStartInfo.*ghost" --include="*.cs" .

# Find package references
grep -r "Ghostscript" --include="*.csproj" .
# Find all Ghostscript.NET references
grep -r "Ghostscript\.NET\|GhostscriptProcessor\|GhostscriptRasterizer\|gsdll" --include="*.cs" .

# Find direct process calls to Ghostscript
grep -r "gswin64c\|gswin32c\|gs\|ProcessStartInfo.*ghost" --include="*.cs" .

# Find package references
grep -r "Ghostscript" --include="*.csproj" .
SHELL

NuGet 套件變更

# Remove Ghostscript.NET
dotnet remove package Ghostscript.NET

# Install IronPDF
dotnet add package IronPdf
# Remove Ghostscript.NET
dotnet remove package Ghostscript.NET

# Install IronPDF
dotnet add package IronPdf
SHELL

移除 Ghostscript GPL 依賴項

遷移後: 從伺服器卸載 Ghostscript GPL

  • 從部署移除gsdll32.dll / gsdll64.dll
  • 移除 Ghostscript GPL 的 PATH 配置
  • 刪除所有GhostscriptVersionInfo引用

快速入門遷移

步驟 1:更新許可證配置

之前(Ghostscript GPL):

Ghostscript GPL 根據 AGPL 要求要么公開原始程式碼,要么從 Artifex 購買昂貴的商業許可證。

(IronPDF 之後):

// Set once at application startup
IronPdf.License.LicenseKey = "YOUR-IRONPDF-LICENSE-KEY";
// Set once at application startup
IronPdf.License.LicenseKey = "YOUR-IRONPDF-LICENSE-KEY";
$vbLabelText   $csharpLabel

步驟 2:更新命名空間導入

// Before (Ghostscript GPL)
using Ghostscript.NET;
using Ghostscript.NET.Processor;
using Ghostscript.NET.Rasterizer;

// After (IronPDF)
using IronPdf;
// Before (Ghostscript GPL)
using Ghostscript.NET;
using Ghostscript.NET.Processor;
using Ghostscript.NET.Rasterizer;

// After (IronPDF)
using IronPdf;
$vbLabelText   $csharpLabel

完整 API 參考

核心類別映射

Ghostscript.NETIronPDF描述
GhostscriptProcessor各種PdfDocument方法PDF處理
GhostscriptRasterizerPdfDocument.ToBitmap() / RasterizeToImageFiles()PDF 轉影像
GhostscriptVersionInfo不適用(無需填寫)DLL 位置
GhostscriptStdIO不適用(可使用例外情況)I/O 處理
進程 + 命令列ChromePdfRendererHTML 轉 PDF

命令列交換器映射

Ghostscript GPL 開關IronPDF當量描述
-dNOPAUSE不適用(無需填寫)不要在翻頁之間停頓
-dBATCH不適用(無需填寫)處理完畢後退出
-dSAFER不適用(預設值)安全文件訪問
-sDEVICE=pdfwrite各種PDF方法輸出 PDF
-sDEVICE=png16mToBitmap()RasterizeToImageFiles()PNG 輸出
-sOutputFile=XSaveAs("X")輸出檔名
-r300方法中的 DPI 參數解決
-dPDFSETTINGS=/ebookCompressImages(quality: 75)中等品質
-sOwnerPassword=XSecuritySettings.OwnerPassword所有者密碼
-sUserPassword=XSecuritySettings.UserPassword使用者密碼

程式碼遷移範例

範例 1:HTML 轉 PDF

之前(Ghostscript GPL):

// NuGet: Install-Package Ghostscript.NET
using Ghostscript.NET;
using Ghostscript.NET.Processor;
using System.IO;
using System.Text;

class GhostscriptExample
{
    static void Main()
    {
        // Ghostscript cannot directly convert HTML to PDF
        // You need to first convert HTML to PS/EPS using another tool
        // then use Ghostscript to convert PS to PDF

        string htmlContent = "<html><body><h1>Hello World</h1></body></html>";
        string psFile = "temp.ps";
        string outputPdf = "output.pdf";

        // This is a workaround - Ghostscript primarily works with PostScript
        GhostscriptProcessor processor = new GhostscriptProcessor();

        List<string> switches = new List<string>
        {
            "-dNOPAUSE",
            "-dBATCH",
            "-dSAFER",
            "-sDEVICE=pdfwrite",
            $"-sOutputFile={outputPdf}",
            psFile
        };

        processor.Process(switches.ToArray());
    }
}
// NuGet: Install-Package Ghostscript.NET
using Ghostscript.NET;
using Ghostscript.NET.Processor;
using System.IO;
using System.Text;

class GhostscriptExample
{
    static void Main()
    {
        // Ghostscript cannot directly convert HTML to PDF
        // You need to first convert HTML to PS/EPS using another tool
        // then use Ghostscript to convert PS to PDF

        string htmlContent = "<html><body><h1>Hello World</h1></body></html>";
        string psFile = "temp.ps";
        string outputPdf = "output.pdf";

        // This is a workaround - Ghostscript primarily works with PostScript
        GhostscriptProcessor processor = new GhostscriptProcessor();

        List<string> switches = new List<string>
        {
            "-dNOPAUSE",
            "-dBATCH",
            "-dSAFER",
            "-sDEVICE=pdfwrite",
            $"-sOutputFile={outputPdf}",
            psFile
        };

        processor.Process(switches.ToArray());
    }
}
$vbLabelText   $csharpLabel

(IronPDF 之後):

// NuGet: Install-Package IronPdf
using IronPdf;

class IronPdfExample
{
    static void Main()
    {
        var renderer = new ChromePdfRenderer();

        string htmlContent = "<html><body><h1>Hello World</h1></body></html>";

        var pdf = renderer.RenderHtmlAsPdf(htmlContent);
        pdf.SaveAs("output.pdf");
    }
}
// NuGet: Install-Package IronPdf
using IronPdf;

class IronPdfExample
{
    static void Main()
    {
        var renderer = new ChromePdfRenderer();

        string htmlContent = "<html><body><h1>Hello World</h1></body></html>";

        var pdf = renderer.RenderHtmlAsPdf(htmlContent);
        pdf.SaveAs("output.pdf");
    }
}
$vbLabelText   $csharpLabel

差異很明顯:Ghostscript GPL 根本無法直接將 HTML 轉換為 PDF——它需要使用外部工具進行中間 PostScript 轉換。 IronPDF 的ChromePdfRenderer提供直接的 HTML 到 PDF 轉換,並完全支援 CSS3、JavaScript 和現代 Web 標準。 如需更多渲染選項,請參閱HTML 轉 PDF 文件

範例 2:PDF 轉影像

之前(Ghostscript GPL):

// NuGet: Install-Package Ghostscript.NET
using Ghostscript.NET;
using Ghostscript.NET.Rasterizer;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

class GhostscriptExample
{
    static void Main()
    {
        string inputPdf = "input.pdf";
        string outputPath = "output";

        GhostscriptVersionInfo gvi = new GhostscriptVersionInfo("gsdll64.dll");

        using (GhostscriptRasterizer rasterizer = new GhostscriptRasterizer())
        {
            rasterizer.Open(inputPdf, gvi, false);

            for (int pageNumber = 1; pageNumber <= rasterizer.PageCount; pageNumber++)
            {
                Image img = rasterizer.GetPage(300, pageNumber);
                img.Save($"{outputPath}_page{pageNumber}.png", ImageFormat.Png);
                img.Dispose();
            }
        }
    }
}
// NuGet: Install-Package Ghostscript.NET
using Ghostscript.NET;
using Ghostscript.NET.Rasterizer;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

class GhostscriptExample
{
    static void Main()
    {
        string inputPdf = "input.pdf";
        string outputPath = "output";

        GhostscriptVersionInfo gvi = new GhostscriptVersionInfo("gsdll64.dll");

        using (GhostscriptRasterizer rasterizer = new GhostscriptRasterizer())
        {
            rasterizer.Open(inputPdf, gvi, false);

            for (int pageNumber = 1; pageNumber <= rasterizer.PageCount; pageNumber++)
            {
                Image img = rasterizer.GetPage(300, pageNumber);
                img.Save($"{outputPath}_page{pageNumber}.png", ImageFormat.Png);
                img.Dispose();
            }
        }
    }
}
$vbLabelText   $csharpLabel

(IronPDF 之後):

// NuGet: Install-Package IronPdf
using IronPdf;
using System;

class IronPdfExample
{
    static void Main()
    {
        var pdf = PdfDocument.FromFile("input.pdf");

        var images = pdf.ToBitmap();

        for (int i = 0; i < images.Length; i++)
        {
            images[i].Save($"output_page{i + 1}.png");
        }
    }
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;

class IronPdfExample
{
    static void Main()
    {
        var pdf = PdfDocument.FromFile("input.pdf");

        var images = pdf.ToBitmap();

        for (int i = 0; i < images.Length; i++)
        {
            images[i].Save($"output_page{i + 1}.png");
        }
    }
}
$vbLabelText   $csharpLabel

Ghostscript GPL 方法需要找到外部gsdll64.dll ,建立一個GhostscriptVersionInfo對象,並使用從 1 開始的頁碼。 IronPDF 的ToBitmap()方法提供了一個簡潔的單行方法,沒有任何外部相依性。 請注意頁面索引的差異:Ghostscript GPL 使用索引為 1 的頁面,而 IronPDF 使用索引為 0 的頁面(標準的 .NET 約定)。

範例 3:合併 PDF 文件

之前(Ghostscript GPL):

// NuGet: Install-Package Ghostscript.NET
using Ghostscript.NET;
using Ghostscript.NET.Processor;
using System.Collections.Generic;

class GhostscriptExample
{
    static void Main()
    {
        string outputPdf = "merged.pdf";
        string[] inputFiles = { "file1.pdf", "file2.pdf", "file3.pdf" };

        GhostscriptProcessor processor = new GhostscriptProcessor();

        List<string> switches = new List<string>
        {
            "-dNOPAUSE",
            "-dBATCH",
            "-dSAFER",
            "-sDEVICE=pdfwrite",
            $"-sOutputFile={outputPdf}"
        };

        switches.AddRange(inputFiles);

        processor.Process(switches.ToArray());
    }
}
// NuGet: Install-Package Ghostscript.NET
using Ghostscript.NET;
using Ghostscript.NET.Processor;
using System.Collections.Generic;

class GhostscriptExample
{
    static void Main()
    {
        string outputPdf = "merged.pdf";
        string[] inputFiles = { "file1.pdf", "file2.pdf", "file3.pdf" };

        GhostscriptProcessor processor = new GhostscriptProcessor();

        List<string> switches = new List<string>
        {
            "-dNOPAUSE",
            "-dBATCH",
            "-dSAFER",
            "-sDEVICE=pdfwrite",
            $"-sOutputFile={outputPdf}"
        };

        switches.AddRange(inputFiles);

        processor.Process(switches.ToArray());
    }
}
$vbLabelText   $csharpLabel

(IronPDF 之後):

// NuGet: Install-Package IronPdf
using IronPdf;
using System.Collections.Generic;

class IronPdfExample
{
    static void Main()
    {
        var pdfs = new List<PdfDocument>
        {
            PdfDocument.FromFile("file1.pdf"),
            PdfDocument.FromFile("file2.pdf"),
            PdfDocument.FromFile("file3.pdf")
        };

        var merged = PdfDocument.Merge(pdfs);
        merged.SaveAs("merged.pdf");
    }
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System.Collections.Generic;

class IronPdfExample
{
    static void Main()
    {
        var pdfs = new List<PdfDocument>
        {
            PdfDocument.FromFile("file1.pdf"),
            PdfDocument.FromFile("file2.pdf"),
            PdfDocument.FromFile("file3.pdf")
        };

        var merged = PdfDocument.Merge(pdfs);
        merged.SaveAs("merged.pdf");
    }
}
$vbLabelText   $csharpLabel

Ghostscript GPL 方法需要記住 switch 語法( -dNOPAUSE-dBATCH-sDEVICE=pdfwrite )並將檔案路徑連接成字串陣列。 IronPDF 的靜態Merge方法提供類型安全、支援 IntelliSense 的合併功能,可合併正確的PdfDocument物件。 了解更多關於合併和拆分PDF的資訊


關鍵遷移說明

頁面索引轉換

Ghostscript GPL 遷移中最重要的變化之一是頁面索引方式的差異:

// Ghostscript GPL: 1-indexed pages
for (int pageNumber = 1; pageNumber <= rasterizer.PageCount; pageNumber++)
{
    Image img = rasterizer.GetPage(300, pageNumber);
}

// IronPDF: 0-indexed pages (standard .NET)
for (int i = 0; i < images.Length; i++)
{
    images[i].Save($"output_page{i + 1}.png");
}
// Ghostscript GPL: 1-indexed pages
for (int pageNumber = 1; pageNumber <= rasterizer.PageCount; pageNumber++)
{
    Image img = rasterizer.GetPage(300, pageNumber);
}

// IronPDF: 0-indexed pages (standard .NET)
for (int i = 0; i < images.Length; i++)
{
    images[i].Save($"output_page{i + 1}.png");
}
$vbLabelText   $csharpLabel

AGPL許可問題消除

Ghostscript GPL 的 AGPL 授權具有"病毒式傳播"的特性,要求在分發應用程式時揭露原始程式碼。 IronPDF的商業許可條款明確,沒有此類要求。

沒有外部二進位文件

IronPDF 是一個完全獨立的系統。 遷移後請刪除這些檔案:

  • gsdll32.dllgsdll64.dll文件 從伺服器安裝 Ghostscript GPL
  • PATH 環境變數配置
  • 程式碼中的GhostscriptVersionInfo引用

PostScript 文件

IronPDF 不會直接處理 PostScript (.ps) 檔案。 如果您的工作流程需要處理 PostScript 程式碼,則可以:

  1. 在 IronPDF 處理之前,先使用其他工具將 PostScript 轉換為 PDF。
  2. 將來源內容轉換為 HTML 格式,並使用 IronPDF 的 HTML 渲染功能。

性能考量

沒有進程生成

Ghostscript GPL 操作會產生外部流程,並產生相關的開銷。 IronPDF 在您的 .NET 流程中運作:

// Ghostscript GPL: Process spawning overhead
processor.Process(switches.ToArray());  // Creates new OS process

// IronPDF: In-process execution
var merged = PdfDocument.Merge(pdfs);  // Native .NET method call
// Ghostscript GPL: Process spawning overhead
processor.Process(switches.ToArray());  // Creates new OS process

// IronPDF: In-process execution
var merged = PdfDocument.Merge(pdfs);  // Native .NET method call
$vbLabelText   $csharpLabel

螺紋安全

IronPDF 從設計上就保證了線程安全。 多個線程可以同時使用ChromePdfRendererPdfDocument ,而無需擔心同步問題。


遷移清單

遷移前

  • 清點程式碼庫中所有 Ghostscript GPL 的使用情況
  • 記錄目前使用的命令列開關
  • 識別任何 PostScript 處理(需要特殊處理)
  • 審查 AGPL 許可合規性
  • 取得 IronPDF 許可證密鑰
  • 在版本控制系統中建立遷移分支

程式碼遷移

  • 刪除 Ghostscript.NET NuGet 套件: dotnet remove package Ghostscript.NET 安裝 IronPdf NuGet 套件: dotnet add package IronPdf
  • 移除外部 Ghostscript GPL 二進位依賴項
  • 刪除GhostscriptVersionInfo和 DLL 引用 將GhostscriptProcessor.Process()轉換為 IronPDF 方法
  • GhostscriptRasterizer轉換為pdf.ToBitmap()
  • 用 API 呼叫取代命令列開關
  • 將錯誤處理方式從解析標準錯誤輸出 (stderr) 改為拋出例外 將以 1 為索引的頁碼轉換為以 0 為索引的頁碼

測試

  • 測試 PDF 轉圖像轉換
  • 測試 PDF 合併
  • 測試頁面擷取
  • 測試壓縮質量
  • 測試密碼保護
  • 驗證輸出品質是否符合預期
  • 效能基準關鍵路徑

部署

從伺服器中移除 Ghostscript GPL

  • 刪除 PATH 配置
  • 從部署中移除gsdll*.dll文件
  • 驗證應用程式在未安裝 Ghostscript GPL 的情況下是否能正常運作

移民後

  • 移除 Ghostscript GPL 許可證(如果是商業用途)
  • 更新文檔
  • 訓練團隊使用 IronPDF API
  • 監控生產過程,發現任何問題

柯蒂斯·週
技術撰稿人

Curtis Chau擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

除了開發工作之外,柯蒂斯對物聯網 (IoT) 也抱有濃厚的興趣,致力於探索硬體和軟體整合的創新方法。閒暇時,他喜歡玩遊戲和製作 Discord 機器人,將他對科技的熱愛與創造力結合。