跳過到頁腳內容
產品比較

如何使用IronPDF vs PDFsharp在C#中查看PDF?

IronPDF提供完整的 C# PDF 檢視功能,它具有直覺的 API 和基於 Chrome 的渲染引擎的 HTML 到 PDF 轉換功能;而 PDFsharp 提供基本的開源 PDF 操作,但文字擷取和檢視任務需要更複雜的程式碼。

在軟體開發中,處理各種格式的數據至關重要。 PDF(便攜式文件格式)是廣泛使用的文件共用標準。 在 C# 程式設計中,檢視 PDF 對於建立專業文件管理數位內容至關重要。 IronPDF庫提供了用於PDF生成和操作的完整工具。

C# 的多功能性使其在開發各種領域的應用程式方面廣受歡迎。 PDF格式可確保文件完整性,並在不同平台上保持一致的呈現效果。 將 PDF 檢視功能整合到您的應用程式中,可改善使用者體驗簡化工作流程,並為文件處理提供高效的解決方案。 對於需要從掃描文件中識別文字的應用,配套的 OCR 解決方案可以提高您的 PDF 處理能力。 IronPDF文件提供了完整的實施指南。 處理安全文件時,正確的 PDF 處理至關重要。

本文探討如何使用 C# 查看 PDF,介紹了兩個函式庫——PDFsharpIronPDF並提供了安裝和使用它們的說明。 您將研究影響您在這些PDF 生成解決方案之間做出選擇的架構差異、程式碼複雜性和維護考慮因素。 對於需要文件安全性的環境而言,了解這些差異至關重要。 IronPDF演示展示了實際應用,而API 參考提供了詳細的規範。

什麼是 PDFsharp?它如何處理 PDF 查看問題?

PDFsharp是一個用於 C# 的開源程式庫,為開發人員提供 PDF 操作工具包。 與IronPDF 基於 Chrome 的渲染引擎不同,PDFsharp 採用底層繪圖模型,可直接控制 PDF 圖元。 該庫使您能夠存取和操作PDF 文件結構。 然而,與IronPDF 的完整功能相比,PDFsharp 對於常見任務需要更多的手動編碼。

PDFsharp 的架構遵循文件物件模型方法,您可以直接操作 PDF 物件。 這與IronPDF 的 HTML 到 PDF 轉換方法不同,後者使用 Web 技術產生文件。 雖然 PDFsharp 在建立空白 PDF和繪圖操作方面表現出色,但對於添加標題嵌入圖像等任務,它需要更多的程式碼。 PDFsharp 不提供JavaScript渲染響應式 CSS 支援Web 字體整合等進階功能。

如何在 Visual Studio 中安裝 PDFsharp?

IronPDF 的安裝過程類似, PDFsharp可以使用NuGet進行安裝。 執行以下命令:

Install-Package PdfSharp

此命令會安裝 PDFsharp 庫,供您的專案使用。 與IronPDF 的進階安裝選項不同,PDFsharp 缺乏對Docker 容器Linux 部署的內建支援。 快速入門指南提供了IronPDF的其他設定說明。

PDFsharp的系統需求是什麼?

PDFsharp 針對的是較舊的.NET Framework版本,可能與較新的.NET版本和雲端環境有相容性問題。 與 IronPDF 的自動平台偵測功能不同,此程式庫需要手動配置才能進行跨平台部署。 記憶體使用模式有所不同,PDFsharp 在執行複雜操作時可能會消耗更多資源。 在高效能應用場景下,這些差異會變得明顯。 IronPDF效能指南提供了PDFsharp所沒有的最佳化策略。

為什麼 PDFsharp 架構會影響效能?

PDFsharp 的底層架構會直接操作 PDF 對象,進而影響效能,因為需要更多的 CPU 週期。 該程式庫缺乏針對大量處理、平行操作或非同步工作流程的最佳化。 由於需要人工操作員解析,複雜文件的文字擷取效能會下降。 IronPDF 基於 Chrome 的引擎採用瀏覽器優化技術,實現更快的渲染速度和更高的記憶體效率。 對於非同步操作,IronPDF 的架構提供了顯著優勢。

為什麼IronPDF是更好的 PDF 檢視選擇?

IronPDF是一個功能豐富的程式庫,讓您可以輕鬆處理 PDF 操作。 IronPDF設計簡潔且功能多樣,可讓您在應用程式中建立、編輯和閱讀 PDF 文件。 除了基本功能外, IronPDF還提供HTML 轉 PDF 、支援各種影像格式以及高效處理PDF 操作等進階功能。 該庫擅長產生 PDF 報告轉換不同的文件格式

IronPDF 的架構基礎採用Chrome 渲染引擎,確保網頁內容的像素級完美渲染。 這種方法簡化了諸如管理字體處理JavaScript支援 UTF-8 字元等常見任務。 該庫支援線性化PDF壓縮數位簽名註釋

IronPDF可以處理 PDF 文件並格式化數據,支援元數據提取非同步操作。 對於Blazor應用程序, IronPDF提供流暢的整合。

如何在我的專案中安裝IronPDF ?

使用NuGet程式包管理器或套件管理器控制台安裝IronPDF :

Install-Package IronPdf

此指令安裝IronPDF及其相依性。 對於Docker 部署Linux 環境,可能需要額外的軟體包。 快速入門指南提供了設定說明。

IronPDF 的 API 設計有哪些優越之處?

IronPDF 的 API 遵循現代.NET模式,具有流暢的介面和非同步支援。 該庫提供直觀的方法名稱和完整的文檔。 錯誤訊息提供了可操作的解決方案,加快了開發速度。 API 參考文件提供了完整的方法詳情。

何時應該使用 IronPDF 的 Chrome 渲染引擎?

使用 IronPDF 的 Chrome 引擎轉換響應式 HTML 版面配置、渲染JavaScript內容或處理現代 CSS 設計。 此引擎可自動處理網頁字體、SVG 圖形和 Canvas 元素。 它非常適合使用 HTML 範本產生發票或建立具有視覺化效果的報表。 對於WebGL 內容的渲染,該引擎提供 GPU 加速。

如何使用PDFsharp檢視PDF內容?

本節示範如何使用 PDFsharp 檢視 PDF 檔案。由於 PDFsharp 的底層特性,其文字擷取過程較為複雜,這與IronPDF 的簡單方法截然不同:

using System;
using System.Text;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using PdfSharp.Pdf.Content;
using PdfSharp.Pdf.Content.Objects;

class Program
{
    static void Main()
    {
        // Specify the path to the PDF file
        string pdfFilePath = "output.pdf";

        // Open the PDF document in import mode
        // Note: PDFsharp requires specific open modes for different operations
        PdfDocument document = PdfReader.Open(pdfFilePath, PdfDocumentOpenMode.Import);

        // Create StringBuilder for extracted text
        // This approach is less efficient than IronPDF's built-in methods
        StringBuilder extractedText = new StringBuilder();

        // Iterate through each page of the document
        for (int pageIndex = 0; pageIndex < document.PageCount; pageIndex++)
        {
            // Get the current page
            PdfPage page = document.Pages[pageIndex];

            // Extract text using content reader (simplified approach)
            // Real implementation requires extensive operator parsing
            CObject content = ContentReader.ReadContent(page);

            // Parse content objects to extract text
            // This is where PDFsharp becomes significantly complex
            ExtractText(content, extractedText);

            // Note: Actual text extraction requires parsing operators
            // This is a simplified representation
            Console.WriteLine($"Page {pageIndex + 1} processed");
        }

        Console.WriteLine("Extracted Text: " + extractedText.ToString());
        Console.ReadLine(); // Wait for user input before closing the console
    }

    static void ExtractText(CObject content, StringBuilder text)
    {
        // PDFsharp requires manual parsing of content streams
        // This is significantly more complex than shown here
        // Real implementation would need to handle:
        // - Text operators (Tj, TJ, ', ", etc.)
        // - Font encoding and character mapping
        // - Text positioning and transformation matrices
        // - Unicode mapping and glyph substitution
        // - Whitespace detection and word boundaries

        if (content is CArray array)
        {
            foreach (var item in array)
            {
                ExtractText(item, text);
            }
        }
        else if (content is CString str)
        {
            // Simplified text extraction - real implementation needs encoding handling
            text.Append(str.Value);
        }
        // Additional operators would need handling for complete extraction
    }
}
using System;
using System.Text;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using PdfSharp.Pdf.Content;
using PdfSharp.Pdf.Content.Objects;

class Program
{
    static void Main()
    {
        // Specify the path to the PDF file
        string pdfFilePath = "output.pdf";

        // Open the PDF document in import mode
        // Note: PDFsharp requires specific open modes for different operations
        PdfDocument document = PdfReader.Open(pdfFilePath, PdfDocumentOpenMode.Import);

        // Create StringBuilder for extracted text
        // This approach is less efficient than IronPDF's built-in methods
        StringBuilder extractedText = new StringBuilder();

        // Iterate through each page of the document
        for (int pageIndex = 0; pageIndex < document.PageCount; pageIndex++)
        {
            // Get the current page
            PdfPage page = document.Pages[pageIndex];

            // Extract text using content reader (simplified approach)
            // Real implementation requires extensive operator parsing
            CObject content = ContentReader.ReadContent(page);

            // Parse content objects to extract text
            // This is where PDFsharp becomes significantly complex
            ExtractText(content, extractedText);

            // Note: Actual text extraction requires parsing operators
            // This is a simplified representation
            Console.WriteLine($"Page {pageIndex + 1} processed");
        }

        Console.WriteLine("Extracted Text: " + extractedText.ToString());
        Console.ReadLine(); // Wait for user input before closing the console
    }

    static void ExtractText(CObject content, StringBuilder text)
    {
        // PDFsharp requires manual parsing of content streams
        // This is significantly more complex than shown here
        // Real implementation would need to handle:
        // - Text operators (Tj, TJ, ', ", etc.)
        // - Font encoding and character mapping
        // - Text positioning and transformation matrices
        // - Unicode mapping and glyph substitution
        // - Whitespace detection and word boundaries

        if (content is CArray array)
        {
            foreach (var item in array)
            {
                ExtractText(item, text);
            }
        }
        else if (content is CString str)
        {
            // Simplified text extraction - real implementation needs encoding handling
            text.Append(str.Value);
        }
        // Additional operators would need handling for complete extraction
    }
}
$vbLabelText   $csharpLabel

這段程式碼使用 PDFsharp 讀取並提取 PDF 檔案中的文字。程式以匯入模式開啟"output.pdf"文件,並遍歷頁面以提取內容。 與IronPDF 簡單易用的 API不同,此方法需要了解 PDF 的內部結構。 IronPDF 的文字擷取範例展示了一種更簡單的方法。

控制台視窗顯示 PDFsharp 的基本文字擷取嘗試,輸出為

為什麼 PDFsharp 的文字擷取更複雜?

PDFsharp 的複雜性源自於其底層方法。 與IronPDF 的高級 API不同,PDFsharp 要求您了解 PDF 的內部機制,包括運算符、編碼和內容流:

// PDFsharp: Complex font handling and text positioning
PdfDocument document = new PdfDocument();
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);

// Must manually specify fonts and handle font embedding
XFont font = new XFont("Arial", 12, XFontStyle.Regular);

// Manual text positioning with exact coordinates
gfx.DrawString("Hello World", font, XBrushes.Black,
    new XRect(0, 0, page.Width, page.Height),
    XStringFormats.Center);

// Must handle coordinate transformations manually
gfx.TranslateTransform(100, 200);
gfx.RotateTransform(45);
gfx.DrawString("Rotated Text", font, XBrushes.Red, 0, 0);

// IronPDF: Simple HTML approach with CSS styling
var renderer = new ChromePdfRenderer();
var doc = renderer.RenderHtmlAsPdf(@"
    <h1 style='text-align: center;'>Hello World</h1>
    <p style='transform: rotate(45deg); margin-left: 100px; margin-top: 200px; color: red;'>
        Rotated Text
    </p>");

// IronPDF automatically handles fonts, positioning, and transformations
// PDFsharp: Complex font handling and text positioning
PdfDocument document = new PdfDocument();
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);

// Must manually specify fonts and handle font embedding
XFont font = new XFont("Arial", 12, XFontStyle.Regular);

// Manual text positioning with exact coordinates
gfx.DrawString("Hello World", font, XBrushes.Black,
    new XRect(0, 0, page.Width, page.Height),
    XStringFormats.Center);

// Must handle coordinate transformations manually
gfx.TranslateTransform(100, 200);
gfx.RotateTransform(45);
gfx.DrawString("Rotated Text", font, XBrushes.Red, 0, 0);

// IronPDF: Simple HTML approach with CSS styling
var renderer = new ChromePdfRenderer();
var doc = renderer.RenderHtmlAsPdf(@"
    <h1 style='text-align: center;'>Hello World</h1>
    <p style='transform: rotate(45deg); margin-left: 100px; margin-top: 200px; color: red;'>
        Rotated Text
    </p>");

// IronPDF automatically handles fonts, positioning, and transformations
$vbLabelText   $csharpLabel

這種建築結構上的差異對維護工作產生了重大影響。 PDFsharp 對於新增浮水印管理頁面方向等常見任務需要編寫更多樣板程式碼。

PDFsharp在檢視PDF文件方面有哪些限制?

當處理現代 PDF 需求時,PDFsharp 的限制就顯現出來了:

1.不支援 HTML 轉 PDF :無法直接轉換 HTML 程式碼。 2.複雜文字擷取:需要了解 PDF 運算符 3.影像支援有限:需要手動縮放和定位。 4.無內建OCR :缺乏OCR功能 5.缺少進階功能:不支援數位簽章或 PDF/A 合規性

PDFsharp 如何處理記憶體和效能?

PDFsharp 會將整個 PDF 檔案載入到記憶體中,這可能會導致處理大型檔案時出現問題。 流處理尚未完全支持,限制了可擴展性。 與 IronPDF 改良的渲染管線不同,PDFsharp 缺乏內建的快取機制。 效能優化指南提供了 PDFsharp 所不具備的策略。

IronPDF如何簡化 C# 中的 PDF 檢視?

使用IronPDF查看 PDF 文件只需要幾行程式碼。 該庫會自動管理基本 URL 和資源編碼

using IronPdf;
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // Load the PDF document with error handling
        var pdf = PdfDocument.FromFile("output.pdf");

        // Extract all the text content from the PDF
        string text = pdf.ExtractAllText();

        // Print the extracted text to the console
        Console.WriteLine("Full document text:");
        Console.WriteLine(text);

        // Additional extraction options demonstrating IronPDF's versatility

        // Extract text from specific pages with formatting preserved
        string pageText = pdf.ExtractTextFromPage(0);
        Console.WriteLine($"\nPage 1 text: {pageText}");

        // Extract all images and save them
        var images = pdf.ExtractAllImages();
        for (int i = 0; i < images.Count; i++)
        {
            images[i].SaveAs($"extracted_image_{i}.png");
        }

        // Access complete metadata
        string author = pdf.MetaData.Author;
        string title = pdf.MetaData.Title;
        string subject = pdf.MetaData.Subject;
        DateTime creationDate = pdf.MetaData.CreationDate;

        Console.WriteLine($"\nDocument Info:");
        Console.WriteLine($"Title: {title}");
        Console.WriteLine($"Author: {author}");
        Console.WriteLine($"Created: {creationDate}");

        // Search for specific text
        var searchResults = pdf.Search("invoice");
        foreach (var result in searchResults)
        {
            Console.WriteLine($"Found '{result.Text}' on page {result.PageIndex}");
        }

        // Extract form field data
        var form = pdf.Form;
        foreach (var field in form.Fields)
        {
            Console.WriteLine($"Field: {field.Name} = {field.Value}");
        }
    }
}
using IronPdf;
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // Load the PDF document with error handling
        var pdf = PdfDocument.FromFile("output.pdf");

        // Extract all the text content from the PDF
        string text = pdf.ExtractAllText();

        // Print the extracted text to the console
        Console.WriteLine("Full document text:");
        Console.WriteLine(text);

        // Additional extraction options demonstrating IronPDF's versatility

        // Extract text from specific pages with formatting preserved
        string pageText = pdf.ExtractTextFromPage(0);
        Console.WriteLine($"\nPage 1 text: {pageText}");

        // Extract all images and save them
        var images = pdf.ExtractAllImages();
        for (int i = 0; i < images.Count; i++)
        {
            images[i].SaveAs($"extracted_image_{i}.png");
        }

        // Access complete metadata
        string author = pdf.MetaData.Author;
        string title = pdf.MetaData.Title;
        string subject = pdf.MetaData.Subject;
        DateTime creationDate = pdf.MetaData.CreationDate;

        Console.WriteLine($"\nDocument Info:");
        Console.WriteLine($"Title: {title}");
        Console.WriteLine($"Author: {author}");
        Console.WriteLine($"Created: {creationDate}");

        // Search for specific text
        var searchResults = pdf.Search("invoice");
        foreach (var result in searchResults)
        {
            Console.WriteLine($"Found '{result.Text}' on page {result.PageIndex}");
        }

        // Extract form field data
        var form = pdf.Form;
        foreach (var field in form.Fields)
        {
            Console.WriteLine($"Field: {field.Name} = {field.Value}");
        }
    }
}
$vbLabelText   $csharpLabel

這段程式碼使用IronPDF以 ExtractAllText 方法從 "output.pdf" 中擷取文字。 此實作方案可自動處理複雜的 PDF 文件,相較於 PDFsharp 所需的手動解析,優勢顯而易見。您的應用程式無需了解 PDF 內部機制即可立即使用此功能。 完整教程提供了更多範例。

 IronPDF控制台視窗顯示成功提取文本,內容為

IronPDF提供哪些進階PDF檢視功能?

IronPDF提供完整的 PDF 分析工具:

// Advanced PDF analysis with IronPDF
var pdf = PdfDocument.FromFile("complex-document.pdf");

// Extract structured content with detailed information
foreach (var page in pdf.Pages)
{
    // Get page dimensions for layout analysis
    var width = page.Width;
    var height = page.Height;
    var rotation = page.Rotation;

    // Extract form fields with type information
    var formFields = page.GetFormFields();
    foreach (var field in formFields)
    {
        Console.WriteLine($"Field: {field.Name}, Type: {field.Type}, Value: {field.Value}");
    }

    // Extract annotations with properties
    var annotations = page.GetAnnotations();
    foreach (var annotation in annotations)
    {
        Console.WriteLine($"Annotation: {annotation.Title} - {annotation.Content}");
    }

    // Extract hyperlinks with destinations
    var links = page.GetLinks();
    foreach (var link in links)
    {
        Console.WriteLine($"Link: {link.Text} -> {link.Url}");
    }
}

// Advanced search functionality with context
var searchResults = pdf.Search("invoice", SearchOptions.CaseInsensitive | SearchOptions.WholeWord);
foreach (var result in searchResults)
{
    Console.WriteLine($"Found on page {result.PageIndex} at position {result.Position}: {result.Text}");
    // Get surrounding text for context
    string context = pdf.ExtractTextFromPage(result.PageIndex)
        .Substring(Math.Max(0, result.Position - 50), 100);
    Console.WriteLine($"Context: ...{context}...");
}

// Extract tables as structured data
var tables = pdf.ExtractTables();
foreach (var table in tables)
{
    for (int row = 0; row < table.RowCount; row++)
    {
        for (int col = 0; col < table.ColumnCount; col++)
        {
            Console.Write($"{table[row, col]}\t");
        }
        Console.WriteLine();
    }
}
// Advanced PDF analysis with IronPDF
var pdf = PdfDocument.FromFile("complex-document.pdf");

// Extract structured content with detailed information
foreach (var page in pdf.Pages)
{
    // Get page dimensions for layout analysis
    var width = page.Width;
    var height = page.Height;
    var rotation = page.Rotation;

    // Extract form fields with type information
    var formFields = page.GetFormFields();
    foreach (var field in formFields)
    {
        Console.WriteLine($"Field: {field.Name}, Type: {field.Type}, Value: {field.Value}");
    }

    // Extract annotations with properties
    var annotations = page.GetAnnotations();
    foreach (var annotation in annotations)
    {
        Console.WriteLine($"Annotation: {annotation.Title} - {annotation.Content}");
    }

    // Extract hyperlinks with destinations
    var links = page.GetLinks();
    foreach (var link in links)
    {
        Console.WriteLine($"Link: {link.Text} -> {link.Url}");
    }
}

// Advanced search functionality with context
var searchResults = pdf.Search("invoice", SearchOptions.CaseInsensitive | SearchOptions.WholeWord);
foreach (var result in searchResults)
{
    Console.WriteLine($"Found on page {result.PageIndex} at position {result.Position}: {result.Text}");
    // Get surrounding text for context
    string context = pdf.ExtractTextFromPage(result.PageIndex)
        .Substring(Math.Max(0, result.Position - 50), 100);
    Console.WriteLine($"Context: ...{context}...");
}

// Extract tables as structured data
var tables = pdf.ExtractTables();
foreach (var table in tables)
{
    for (int row = 0; row < table.RowCount; row++)
    {
        for (int col = 0; col < table.ColumnCount; col++)
        {
            Console.Write($"{table[row, col]}\t");
        }
        Console.WriteLine();
    }
}
$vbLabelText   $csharpLabel

這些功能對於處理表單處理或滿足合規性要求的企業應用程式至關重要。 IronPDF支援PDF/A 轉換PDF/UA 無障礙標準

什麼時候應該選擇IronPDF而不是 PDFsharp 來查看 PDF 檔案?

您選擇IronPDF還是 PDFsharp 取決於您的專案需求:

在以下情況下選擇IronPDF : 您需要將 HTML 轉換為 PDF。

在以下情況下請考慮使用 PDFsharp:

  • 建立簡單的 PDF 生成工具
  • 使用靜態佈局 預算限制阻礙了商業許可。

IronPDF如何處理大規模 PDF 處理?

IronPDF憑藉其內建的最佳化功能,在企業級處理方面表現出色。 該庫支援並行處理、批次操作和記憶體高效的串流傳輸。 非同步方法使 Web 應用程式能夠執行非阻塞操作。 IronPDF 的 Chrome 引擎能夠有效率地處理並發請求,使其適用於微服務和無伺服器功能。

我的 C# 專案應該選擇哪個 PDF 函式庫?

PDFsharp 和IronPDF都為尋求多功能解決方案的開發人員提供了相應的功能。 PDFsharp 提供了一個輕量級工具包,適用於基本的 PDF 任務。 相比之下, IronPDF是為完整的 PDF 操作而設計的。 其先進的功能使其能夠有效地處理複雜的 PDF 操作。

以下程式碼範例展示了 IronPDF 簡潔的架構:

// IronPDF: Clean, maintainable code following SOLID principles
public class InvoiceService
{
    private readonly ITemplateEngine _templateEngine;
    private readonly IConfiguration _config;

    public async Task<byte[]> GenerateInvoicePdf(Invoice invoice)
    {
        // Render HTML from template with full CSS support
        var html = await _templateEngine.RenderAsync("invoice.html", invoice);

        // Configure rendering with fluent API
        var renderer = new ChromePdfRenderer
        {
            RenderingOptions = new ChromePdfRenderOptions
            {
                MarginTop = 50,
                MarginBottom = 50,
                PaperSize = PdfPaperSize.A4,
                Title = $"Invoice #{invoice.Number}",
                // Enable JavaScript for dynamic content
                EnableJavaScript = true,
                // Wait for AJAX calls to complete
                WaitFor = WaitFor.NetworkIdle0,
                // Custom headers and footers
                HtmlHeader = "<div style='text-align: center;'>{page} of {total-pages}</div>",
                HtmlFooter = "<div style='text-align: center;'>© 2024 Your Company</div>"
            }
        };

        // Render with full Chrome engine support
        var pdf = await renderer.RenderHtmlAsPdfAsync(html);

        // Add security features
        pdf.SecuritySettings.SetPassword("user-password", "owner-password");
        pdf.SecuritySettings.AllowPrinting = true;
        pdf.SecuritySettings.AllowCopy = false;

        // Add digital signature for authenticity
        pdf.SignWithCertificate(certificate, "Authorized Signature");

        // Improve for web delivery
        pdf.CompressImages(90);

        return pdf.BinaryData;
    }
}
// IronPDF: Clean, maintainable code following SOLID principles
public class InvoiceService
{
    private readonly ITemplateEngine _templateEngine;
    private readonly IConfiguration _config;

    public async Task<byte[]> GenerateInvoicePdf(Invoice invoice)
    {
        // Render HTML from template with full CSS support
        var html = await _templateEngine.RenderAsync("invoice.html", invoice);

        // Configure rendering with fluent API
        var renderer = new ChromePdfRenderer
        {
            RenderingOptions = new ChromePdfRenderOptions
            {
                MarginTop = 50,
                MarginBottom = 50,
                PaperSize = PdfPaperSize.A4,
                Title = $"Invoice #{invoice.Number}",
                // Enable JavaScript for dynamic content
                EnableJavaScript = true,
                // Wait for AJAX calls to complete
                WaitFor = WaitFor.NetworkIdle0,
                // Custom headers and footers
                HtmlHeader = "<div style='text-align: center;'>{page} of {total-pages}</div>",
                HtmlFooter = "<div style='text-align: center;'>© 2024 Your Company</div>"
            }
        };

        // Render with full Chrome engine support
        var pdf = await renderer.RenderHtmlAsPdfAsync(html);

        // Add security features
        pdf.SecuritySettings.SetPassword("user-password", "owner-password");
        pdf.SecuritySettings.AllowPrinting = true;
        pdf.SecuritySettings.AllowCopy = false;

        // Add digital signature for authenticity
        pdf.SignWithCertificate(certificate, "Authorized Signature");

        // Improve for web delivery
        pdf.CompressImages(90);

        return pdf.BinaryData;
    }
}
$vbLabelText   $csharpLabel

這種方法利用現有的 HTML/CSS 技能並保持關注點分離,具有明顯的優勢。 該庫透過其完整的 API 為您管理複雜性。

雖然這兩個庫各有優點,但IronPDF以其豐富的功能和簡潔性脫穎而出。 性能基準測試表明, IronPDF可以有效地處理大型文件。 對於優先考慮長期可維護性的團隊, IronPDF提供完整的文件、專業的支援和豐富的程式碼範例

IronPDF可免費開髮用途,並提供免費試用版,供使用者探索進階功能。 要了解有關查看 PDF 內容的更多信息,請訪問提取文字和圖像指南。 更多範例請參考IronPDF HTML 轉 PDF 程式碼範例。 您的應用程式可以立即受益於 IronPDF 的現代化架構和完整功能。

請注意PDFsharp 是其各自所有者的註冊商標。 本網站與PDFsharp無任何關聯,亦未獲得其認可或贊助。所有產品名稱、標誌和品牌均為其各自所有者的財產。 比較僅供參考,反映的是撰寫本文時可公開取得的資訊。

常見問題解答

在 C# 應用程式中查看 PDF 的好處是什麼?

在 C# 應用程式中查看 PDF 通過提供易於導航和操作的標準化文檔格式來增強用戶體驗。像 IronPDF 這樣的庫為開發人員提供了無縫集成 PDF 查看功能到他們的應用程式中的工具,簡化了工作流程並提高了效率。

如何在 C# 中查看 PDF 文檔?

您可以使用像 IronPDF 這樣的庫在 C# 中查看 PDF 文檔。它通過在您的 C# 專案中提供加載和渲染 PDF 文件的方法來幫助您集成 PDF 查看功能。

如何選擇適用於 C# 的最佳 PDF 庫?

選擇 C# 的 PDF 庫時,應考慮功能集、使用方便性和是否支持高級功能。IronPDF 因其全面的解決方案而推薦,其中包括 HTML 到 PDF 的轉換和對各種圖像格式的支持,這可以簡化復雜的 PDF 任務。

我能使用 C# 庫來修改 PDF 嗎?

是的,可以使用像 IronPDF 這樣的 C# 庫來修改 PDF。它提供了強大的工具來編輯和操作 PDF 文檔,讓開發人員能夠有效地添加、移除或更新 PDF 文件中的內容。

如何在 C# 專案中安裝 PDF 庫?

要在 C# 專案中安裝像 IronPDF 這樣的 PDF 庫,使用 NuGet 程式包管理器並在程式包管理器控制台中運行指令 Install-Package IronPDF。該指令會將庫及其依賴項添加到您的專案中。

在為 C# 選擇 PDF 庫時應該尋找什麼功能?

選擇 C# 的 PDF 庫時,應尋找 PDF 查看、編輯、HTML 到 PDF 的轉換以及支持各種圖像格式等功能。IronPDF 提供了一組豐富的功能,能滿足這些需求,提供了處理 PDF 的多功能性解決方案。

是否有 C# 的 PDF 庫提供免費試用版?

是的,IronPDF 提供了免費試用版,供開發人員探索其先進的 PDF 功能。這使您能在購買之前測試庫的功能並將其功能集成到 C# 項目中。

如何使用 C# 庫從 PDF 中提取文本?

要從 PDF 中提取文本,使用 C# 中的 IronPDF,在 PdfDocument.FromFile() 中載入 PDF 文檔,然後使用 ExtractAllText() 來獲取文本內容。這種簡單明瞭的方法展示了 IronPDF 處理 PDF 文本提取的簡便性。

在哪裡可以找到更多使用 C# 處理 PDF 的代碼示例?

使用 IronPDF 在 C# 中處理 PDF 的其他代碼示例可以在 'IronPDF HTML to PDF Code Examples' 頁面上找到。該資源提供了實用實現,並提供了將 IronPDF 功能集成到您的 C# 項目中的見解。

IronPDF 為什麼是一個推薦的 C# PDF 處理選擇?

IronPDF 以其廣泛的功能集、簡單性和多功能性而被推薦。它為高級 PDF 功能提供了全面的解決方案,使其成為尋求將複雜的 PDF 功能集成到 C# 應用程式中的開發人員的首選。

Curtis Chau
技術作家

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

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

Iron Support Team

We're online 24 hours, 5 days a week.
Chat
Email
Call Me