IRONSOFTWAREHOME
影片

如何在 ASPNET 中將視圖轉換為 PDF

Curtis Chau
Curtis Chau
Updated: 2026年7月19日

從SAP Crystal Reports遷移到IronPDF將您的報告工作流程從具有COM時代依賴性的運行時MSI再分發轉換為現代NuGet包。 本指南提供了一個逐步的遷移路徑,消除了運行時安裝程式的需求,解除SAP生態系統的鎖定,並解鎖了Crystal Reports for Visual Studio(CR4VS)不支持的.NET 6/8/9和Linux/Docker目標。

為什麼從SAP Crystal Reports遷移到IronPDF

了解SAP Crystal Reports

SAP Crystal Reports是一個廣泛用於生成"像素完美"報告的企業報告平台,具有廣泛的資料源連接能力和長期存在的設計器生態系統。 Crystal Reports Designer仍然是用於構建複雜報告布局的SAP堆棧內一個有生產力的工具。

然而,運行時的COM時代架構和僅支持Windows/.NET Framework的局限性給目標.NET 6/8/9、Linux或優先容器部署的團隊帶來阻礙。

遷移的主要原因

  1. **運行時MSI可再分發:**Crystal Reports運行時作為MSI安裝程式(通常根據32/64位和地區而定,大小從數十MB到數百MB不等)發佈,而不是NuGet包
  2. **SAP生態系統鎖定:**受SAP的發佈周期和產品路線圖約束;SAP已確認2027年末結束CR4VS的主流維護
  3. **許可:**CR4VS本身可以免費下載,運行時也可以免費再分發,但是Crystal Reports設計工具系列(CR 2020 / CR 2025)和BI平台是商業SAP的SKU
  4. **舊有架構:**依賴COM的運行時DLL目標.NET Framework 4.x,不能在.NET Core / .NET 5+下載入
  5. **不支持.NET Core / .NET 5+:**沒有針對現代.NET的Crystal Reports運行時的路線圖; 專案保持釘在.NET Framework 4.x
  6. **32位運行時停產:**SAP已確認32位CR .NET運行時在SP 39(2025年12月)後停產; 未來服務包僅提供64位
  7. **報告設計器依賴性:**需要CR4VS Visual Studio擴展或獨立的Crystal Reports設計工具來建立.rpt文件

SAP Crystal Reports的隱藏成本

成本因素SAP Crystal ReportsIronPDF
運行時分發可再分發的MSI安裝程式NuGet套件
安裝運行時安裝程式+ VS擴展dotnet add package IronPdf
部署在目標機器上運行MSIxcopy / 發佈
64位支持是(32位運行時在SP 39後結束,2025年12月)本機
.NET Core / .NET 5+不支持(僅限框架4.x)完全支持(.NET 6/8/9,框架4.6.2+)
雲端部署僅限Windows虛擬機簡單
Linux / Docker不支持(僅限Windows)支援

SAP Crystal Reports與IronPDF的比較

功能SAP Crystal ReportsIronPDF
主要功能企業報告平台HTML到PDF轉換引擎和PDF操作
整合在SAP生態系統中最好現代.NET整合,輕量級NuGet包
易用性複雜的設置和部署簡化整合,支持.NET開發者
報告設計器必需的可選(HTML/CSS)
模板格式.rpt(二進位)HTML/CSS
HTML 到 PDF完整的Chromium
URL至PDF
CSS 支援完整支持 CSS3
JavaScript完整的ES2024
PDF 操作完整(合併、拆分、編輯)
數位簽名
PDF/A合規性
現代相關性下降,被現代替代品取代現代,與當前技術良好整合

對於目標現代.NET(6、8、9)和跨平台部署的團隊,IronPDF提供了Crystal Reports運行時——鎖定到.NET Framework 4.x——無法提供的本機支持。


開始之前

前提條件

  1. .NET環境: .NET Framework 4.6.2+或.NET 6 / 8 / 9
  2. NuGet存取: 有能力安裝NuGet套件
  3. IronPDF授權:ironpdf.com獲取您的授權金鑰

NuGet包變更

# Crystal Reports for Visual Studio is not distributed by SAP on NuGet.
# Projects typically reference the GAC-installed CrystalDecisions.*.dll
# assemblies delivered by the CR4VS / CR runtime MSI. Community-maintained
#NuGetwrappers such as CrystalReports.Engine exist for build automation
# but are not official SAP packages.

# 1. Remove direct assembly references (CrystalDecisions.*.dll) from your .csproj
# 2. If using a communityNuGetwrapper, remove it:
dotnet remove package CrystalReports.Engine

# 3. Uninstall the Crystal Reports runtime MSI from build/deploy targets

# 4. Install IronPDF
dotnet add package IronPdf
SHELL

授權配置

// Add at application startup
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";

完整API參考

名稱空間變更

// Before: SAP Crystal Reports
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.ReportAppServer;

// After: IronPDF
using IronPdf;

核心API對應

SAP Crystal ReportsIronPDF注釋
ReportDocumentChromePdfRenderer核心渲染
ReportDocument.Load()RenderHtmlAsPdf()載入內容
.rpt文件HTML/CSS模板模板格式
SetDataSource()HTML和資料資料綁定
SetParameterValue()字串插值參數
ExportToDisk()pdf.SaveAs()保存文件
ExportToStream()pdf.BinaryData獲取字節
PrintToPrinter()pdf.Print()列印
Database.TablesC#資料存取資料源
FormulaFieldDefinitionsC#邏輯計算
SummaryInfopdf.MetaDataPDF元資料
ExportFormatType.PortableDocFormat預設輸出PDF本機

程式碼遷移範例

範例1:HTML轉PDF轉換

之前(SAP Crystal Reports):

// NuGet: Install-Package CrystalReports.Engine
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System;

class Program
{
    static void Main()
    {
        // Crystal Reports requires a .rpt file template
        ReportDocument reportDocument = new ReportDocument();
        reportDocument.Load("Report.rpt");
        
        // Crystal Reports doesn't directly support HTML
        // You need to bind data to the report template
        // reportDocument.SetDataSource(dataSet);
        
        ExportOptions exportOptions = reportDocument.ExportOptions;
        exportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
        exportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
        
        DiskFileDestinationOptions diskOptions = new DiskFileDestinationOptions();
        diskOptions.DiskFileName = "output.pdf";
        exportOptions.DestinationOptions = diskOptions;
        
        reportDocument.Export();
        reportDocument.Close();
        reportDocument.Dispose();
    }
}

之後(IronPDF):

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

class Program
{
    static void Main()
    {
        // Create a PDF from HTML string
        var renderer = new ChromePdfRenderer();
        
        string htmlContent = "<h1>Hello World</h1><p>This is a PDF generated from HTML.</p>";
        
        var pdf = renderer.RenderHtmlAsPdf(htmlContent);
        pdf.SaveAs("output.pdf");
        
        Console.WriteLine("PDF created successfully!");
    }
}

此範例說明了根本的範式差異。 SAP Crystal Reports需要一個在Crystal Reports設計器中建立的預設計DiskFileDestinationOptions。 該庫不直接支持HTML內容——您必須將資料綁定到報告模板。

IronPDF直接接受HTML字串:建立一個SaveAs()。 不需要設計者,不需要二進制模板,不需要複雜的導出配置。 查看更多HTML到PDF文件以獲得完整的範例。

範例2:URL轉PDF轉換

之前(SAP Crystal Reports):

// NuGet: Install-Package CrystalReports.Engine
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System;
using System.Net;

class Program
{
    static void Main()
    {
        // Crystal Reports cannot directly convert URLs to PDF
        // You need to create a report template first
        
        // Download HTML content
        WebClient client = new WebClient();
        string htmlContent = client.DownloadString("https://example.com");
        
        // Crystal Reports requires .rpt template and data binding
        // This approach is not straightforward for URL conversion
        ReportDocument reportDocument = new ReportDocument();
        reportDocument.Load("WebReport.rpt");
        
        // Manual data extraction and binding required
        // reportDocument.SetDataSource(extractedData);
        
        reportDocument.ExportToDisk(ExportFormatType.PortableDocFormat, "output.pdf");
        reportDocument.Close();
        reportDocument.Dispose();
    }
}

之後(IronPDF):

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

class Program
{
    static void Main()
    {
        // Create a PDF from a URL
        var renderer = new ChromePdfRenderer();
        
        var pdf = renderer.RenderUrlAsPdf("https://example.com");
        pdf.SaveAs("output.pdf");
        
        Console.WriteLine("PDF created from URL successfully!");
    }
}

SAP Crystal Reports無法直接將URL轉換為PDF。 您需要手動下載HTML內容,使用.rpt模板中——這是一個不簡單的過程,需要大量的人工工作。

IronPDF的RenderUrlAsPdf()方法捕獲完全呈現的網頁,包含所有CSS,JavaScript和圖像,這只需要一個調用。 在我們的教程中了解更多。

範例3:具有頁碼的標頭和頁腳

之前(SAP Crystal Reports):

// NuGet: Install-Package CrystalReports.Engine
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System;

class Program
{
    static void Main()
    {
        // Crystal Reports requires design-time configuration
        ReportDocument reportDocument = new ReportDocument();
        reportDocument.Load("Report.rpt");
        
        // Headers and footers must be designed in the .rpt file
        // using Crystal Reports designer
        // You can set parameter values programmatically
        reportDocument.SetParameterValue("HeaderText", "Company Name");
        reportDocument.SetParameterValue("FooterText", "Page ");
        
        // Crystal Reports handles page numbers through formula fields
        // configured in the designer
        
        reportDocument.ExportToDisk(ExportFormatType.PortableDocFormat, "output.pdf");
        reportDocument.Close();
        reportDocument.Dispose();
    }
}

之後(IronPDF):

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

class Program
{
    static void Main()
    {
        var renderer = new ChromePdfRenderer();
        
        // Configure headers and footers
        renderer.RenderingOptions.TextHeader.CenterText = "Company Name";
        renderer.RenderingOptions.TextHeader.FontSize = 12;
        
        renderer.RenderingOptions.TextFooter.LeftText = "Confidential";
        renderer.RenderingOptions.TextFooter.RightText = "Page {page} of {total-pages}";
        renderer.RenderingOptions.TextFooter.FontSize = 10;
        
        string htmlContent = "<h1>Document Title</h1><p>Document content goes here.</p>";
        
        var pdf = renderer.RenderHtmlAsPdf(htmlContent);
        pdf.SaveAs("output.pdf");
        
        Console.WriteLine("PDF with headers and footers created!");
    }
}

SAP Crystal Reports需要為頁眉和頁腳進行設計時配置。 您必須在.rpt文件中使用Crystal Reports設計器設計它們,然後在運行時傳遞像"FooterText"一樣的參數值。頁碼必須通過設計器中的公式字段來配置。

IronPDF通過TextFooter屬性提供程式化的頁眉/頁腳配置。 可在程式碼中直接設置FontSize。 頁碼使用{total-pages}占位符——不需要設計器。


常見遷移問題

問題1:.rpt文件轉換

**SAP Crystal Reports:**二進位.rpt文件,嵌入了布局、資料、公式。

**解決方案:**不能直接轉換——必須重新建立為HTML:

  1. 在Crystal Reports設計器中打開.rpt
  2. 文件布局、字體、顏色
  3. 記錄所有公式字段
  4. 用HTML/CSS重建
  5. 將公式轉換為C#程式碼

問題2:資料庫連接

**SAP Crystal Reports:**嵌入連接字串和ODBC。

**解決方案:**使用您的應用程式的資料層:

// Instead of Crystal's database integration
var data = await _dbContext.Orders
    .Where(o => o.Date >= startDate && o.Date <= endDate)
    .ToListAsync();

// Bind to HTML template
var html = GenerateReportHtml(data);

問題3:運行時依賴性

**SAP Crystal Reports:**需要安裝Crystal Reports運行時(500MB以上)。

**解決方案:**IronPDF是自帶的:

# Just add theNuGetpackage
dotnet add package IronPdf
# That's it - no additional installs needed
SHELL

問題4:32-bit/64-bit和.NET Framework鎖定

**SAP Crystal Reports:**Crystal Reports運行時依賴於COM並分發於獨立的32位和64位MSI中。 SAP已確認32位CR .NET運行時在SP 39(2025年12月)後停產; 未來服務包僅提供64位。 運行時目標.NET Framework 4.x,且不能在.NET Core /.NET 5+下載入。

**解決方案:**IronPDF可在.NET Framework 4.6.2+、.NET 6、.NET 8和.NET 9上運行——包括Linux和Docker。 64位或AnyCPU不需要特殊配置。


遷移後的新能力

遷移到IronPDF後,您獲得SAP Crystal Reports無法提供的功能:

PDF合併

var pdf1 = PdfDocument.FromFile("report1.pdf");
var pdf2 = PdfDocument.FromFile("report2.pdf");
var merged = PdfDocument.Merge(pdf1, pdf2);
merged.SaveAs("complete_report.pdf");

PDF安全性

var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(reportHtml);

pdf.MetaData.Title = "Quarterly Sales Report";
pdf.MetaData.Author = "Finance Department";

pdf.SecuritySettings.OwnerPassword = "admin123";
pdf.SecuritySettings.UserPassword = "view123";
pdf.SecuritySettings.AllowUserPrinting = PdfPrintSecurity.FullPrintRights;
pdf.SecuritySettings.AllowUserCopyPasteContent = false;

pdf.SaveAs("secure_report.pdf");

數位簽名

var signature = new PdfSignature("certificate.pfx", "password");
pdf.Sign(signature);

水印

using IronPdf.Editing;

pdf.ApplyWatermark("<h1 style='color:red; opacity:0.3;'>DRAFT</h1>");

功能比較總結

功能SAP Crystal ReportsIronPDF
安裝
安裝方法MSI + Visual Studio擴展NuGet
部署在目標機器上運行MSIxcopy / 發佈
平台支持
.NET Core / .NET 5+不支持是(.NET 6/8/9)
64位本機是(32位於2025年12月SP 39後結束)
Linux / Docker不支持(僅限Windows)
Azure / AWS僅限Windows虛擬機簡單
開發
模板格式.rpt(二進位)HTML/CSS
學習曲線Crystal語法網路標準
IntelliSense完整的C#
渲染
URL到PDF
CSS支持完整支持 CSS3
JavaScript完整的ES2024
PDF功能
拆分PDF
浮水印有限完整HTML
數位簽名
PDF/A

遷移檢查表

遷移前

  • 清點所有.rpt文件
  • 截圖每個報告布局以供參考
  • 文件公式字段和計算
  • 列出所有資料源和參數
  • 確定列印要求
  • 獲取IronPDF授權金鑰來自ironpdf.com

程式碼更新

  • 移除Crystal Reports包(CrystalDecisions.CrystalReports.Engine,等)
  • 從部署中移除運行時安裝
  • 安裝IronPdf NuGet包
  • .rpt布局轉換為HTML/CSS模板
  • 將Crystal公式轉換為C#程式碼
  • SetDataSource()更新資料綁定為HTML字串插值
  • 將列印程式碼從pdf.Print()
  • 在應用程式啟動時新增授權初始化

基礎結構

  • 從伺服器中移除水晶運行時
  • 更新部署腳本
  • 移除32位相容模式
  • 更新Docker映像(如果適用)

測試

  • 將PDF輸出與原始報告進行比較
  • 驗證所有計算
  • 測試所有參數
  • 測試列印功能
  • 性能測試
  • 64位測試

請注意: SAP Crystal Reports是其相應所有者的註冊商標。 本網站未與SAP相關聯,未經SAP批准或贊助。所有產品名稱、徽標和品牌均為其相應所有者的財產。 比較僅供參考,反映撰寫時公開可用的資訊。
Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

...
閱讀更多

相關文章

Key in blue circle

立即免費取得 30 天試用金鑰

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
預訂您的免費現場演示
Booking Badge

受到全球數百萬工程師的信任

Iron Software的客戶標誌
獲取您的無義務諮詢
填寫以下表格或電子郵件sales@ironsoftware.com
您的詳細資訊將始終保密
受到全球數百萬工程師的信任
Iron Software的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立