跳過到頁腳內容
產品比較

iTextSharp:將圖像添加到PDF中

Full Comparison

Looking for a detailed feature-by-feature breakdown? See how IronPDF stacks up against Itext on pricing, HTML support, and licensing.

View Full Comparison

IronPDF提供更簡單的圖像添加功能,程式碼行數更少,並且提供永久許可證;而 iTextSharp 提供更精細的控制,但需要更多程式碼,並且商業用途的許可協議為 AGPL,許可較為複雜。

在.NET環境下處理 PDF 文件時,向文件中添加圖像是許多應用程式的常見需求。 無論您是產生帶有公司徽標的發票、創建帶有嵌入式圖表的報告,還是製作帶有浮水印的自訂文檔,將圖像嵌入 PDF 的功能都至關重要。 C# 中兩個流行的 PDF 庫是IronPDFiTextSharp 。 本文從易用性、性能、許可模式以及圖像壓縮和格式支援等高級功能等方面,對這兩個庫在向 PDF 添加圖像方面的能力進行了比較。

IronPDF和 iTextSharp 的主要差異是什麼?

每個圖庫提供哪些圖像功能?

  • IronPDF: IronPDF簡化了在 PDF 中新增影像的操作,內建支援來自 URL 的本機和遠端影像。 其 API 提供對位置、寬度和縮放的控制。 該程式庫支援 JPEG、PNG、GIF、 SVGWebGL 內容印章功能提供了添加浮水印、信頭和疊加圖像的方法。 Chrome渲染引擎可確保複雜圖形和響應式影像的精確渲染。

    iTextSharp: iTextSharp 透過其底層 API 提供影像嵌入功能。 它提供了一個靈活的接口,可以處理 JPG 和 PNG 格式。 但是,自訂需要更多程式碼,尤其是在精確定位影像或實現透明度和旋轉時。 該庫需要手動處理座標系和比例計算。 為了實現響應式尺寸,您通常需要實作自己的視口控制

它們如何處理大型文件和複雜操作?

商業用途的成本影響是什麼?

如何使用IronPDF為 PDF 新增圖片?

如何安裝和設定IronPDF?

透過NuGet將IronPDF安裝到您的 C# 或 Visual Basic 專案中。 該程式庫支援多種安裝方法,包括Windows InstallerDocker 部署

Install-Package IronPdf

安裝完成後,您可以使用 IronPDF 的API 參考開始新增影像。 此程式庫可在WindowsLinuxmacOS平台上無縫運作。

IronPDF中的基本影像新增功能是什麼樣的呢?

快速入門:使用IronPDF為 PDF 新增圖像

使用IronPDF 的印章 API 為 PDF 新增圖像只需要編寫極少的程式碼。

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/IronPdf

    PM > Install-Package IronPdf
  2. 複製並運行這段程式碼。

    using IronPdf;
    using IronPdf.Editing;
    
    class Program
    {
        public static void Main(string[] args)
        {
            // Create a renderer to convert HTML to PDF
            ChromePdfRenderer renderer = new ChromePdfRenderer();
    
            // Render a basic PDF with some HTML content
            PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");
    
            // Create an ImageStamper with the image URL
            ImageStamper stamper = new ImageStamper(new Uri("___PROTECTED_URL_61___"));
    
            // Configure image position and size
            stamper.HorizontalAlignment = HorizontalAlignment.Center;
            stamper.VerticalAlignment = VerticalAlignment.Middle;
            stamper.Width = 200;  // Width in pixels
            stamper.Height = 100; // Height in pixels
    
            // Apply the stamp (image) to the PDF document
            pdf.ApplyStamp(stamper);
    
            // Save the modified PDF to a file
            pdf.SaveAs("output.pdf");
        }
    }
  3. 部署到您的生產環境進行測試

    今天就在您的專案中開始使用免費試用IronPDF

    arrow pointer

如何添加浮水印和背景圖片?

IronPDF擅長為現有 PDF 新增浮水印背景影像。 該庫支援多種沖壓操作自訂定位

using IronPdf;
using IronPdf.Editing;

// Load an existing PDF
PdfDocument pdf = PdfDocument.FromFile("invoice.pdf");

// Create a watermark with transparency
ImageStamper watermark = new ImageStamper("watermark.png")
{
    Opacity = 30,  // 30% opacity for subtle watermarking
    Rotation = -45, // Diagonal watermark
    HorizontalAlignment = HorizontalAlignment.Center,
    VerticalAlignment = VerticalAlignment.Middle
};

// Apply watermark to all pages
pdf.ApplyStamp(watermark);

// Add a header logo
ImageStamper logo = new ImageStamper("company-logo.png")
{
    HorizontalOffset = Length.Millimeters(10),
    VerticalOffset = Length.Millimeters(10),
    Width = 150,
    Height = 50
};

pdf.ApplyStamp(logo, PageSelection.FirstPage);
pdf.SaveAs("watermarked-invoice.pdf");
using IronPdf;
using IronPdf.Editing;

// Load an existing PDF
PdfDocument pdf = PdfDocument.FromFile("invoice.pdf");

// Create a watermark with transparency
ImageStamper watermark = new ImageStamper("watermark.png")
{
    Opacity = 30,  // 30% opacity for subtle watermarking
    Rotation = -45, // Diagonal watermark
    HorizontalAlignment = HorizontalAlignment.Center,
    VerticalAlignment = VerticalAlignment.Middle
};

// Apply watermark to all pages
pdf.ApplyStamp(watermark);

// Add a header logo
ImageStamper logo = new ImageStamper("company-logo.png")
{
    HorizontalOffset = Length.Millimeters(10),
    VerticalOffset = Length.Millimeters(10),
    Width = 150,
    Height = 50
};

pdf.ApplyStamp(logo, PageSelection.FirstPage);
pdf.SaveAs("watermarked-invoice.pdf");
$vbLabelText   $csharpLabel

您也可以建立包含多張圖片的複雜版面

// Add images from Base64 strings
string base64Image = Convert.ToBase64String(File.ReadAllBytes("signature.png"));
HtmlStamper signatureStamp = new HtmlStamper($"<img src='data:image/png;base64,{base64Image}'/>");
signatureStamp.HorizontalAlignment = HorizontalAlignment.Right;
signatureStamp.VerticalAlignment = VerticalAlignment.Bottom;
pdf.ApplyStamp(signatureStamp);
// Add images from Base64 strings
string base64Image = Convert.ToBase64String(File.ReadAllBytes("signature.png"));
HtmlStamper signatureStamp = new HtmlStamper($"<img src='data:image/png;base64,{base64Image}'/>");
signatureStamp.HorizontalAlignment = HorizontalAlignment.Right;
signatureStamp.VerticalAlignment = VerticalAlignment.Bottom;
pdf.ApplyStamp(signatureStamp);
$vbLabelText   $csharpLabel

PDF 檢視器顯示一個簡單的文檔,其中包含

本範例使用ChromePdfRenderer類別從HTML 字串渲染新的 PDF。 使用 IronPDF 的圖像蓋章工具,您可以從提供的 URL 建立圖像並將其套用到 PDF 中。 這示範如何用最少的程式碼向PDF 頁面新增圖像。 渲染選項可讓您完全控制輸出品質。

如何在IronPDF中控制影像位置和尺寸?

IronPDF為影像放置提供了廣泛的自訂選項。 您可以透過印章工具的定位功能設定對齊方式和偏移量來指定頁面位置。 該庫支援使用座標進行絕對定位和使用對齊選項進行相對定位。 高級功能包括自訂紙張尺寸邊距控制,以實現精確的佈局。

如何使用 iTextSharp 為 PDF 新增圖片?

如何安裝和配置 iTextSharp?

透過NuGet安裝 iTextSharp。 與 IronPDF 的全面平台支援不同,iTextSharp 的部署選項有限:

Install-Package itext7

配置完成後,即可使用 iTextSharp 的文件操作功能新增影像。 該庫需要額外的配置才能進行字體管理UTF-8 支援

iTextSharp 中的基本圖片新增功能是什麼樣的?

以下是如何使用 iTextSharp 插入圖片:

using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.IO.Image;

class Program
{
    public static void Main(string[] args)
    {
        // Initialize PDF writer
        var pdfWriter = new PdfWriter("output.pdf");

        // Initialize PDF document
        var pdfDocument = new iText.Kernel.Pdf.PdfDocument(pdfWriter);

        // Initialize document
        var document = new Document(pdfDocument);

        // Create ImageData from image file
        ImageData imageData = ImageDataFactory.Create("iText.png");

        // Create an Image instance
        Image image = new Image(imageData);

        // Set fixed position for the image in the PDF
        image.SetFixedPosition(50, 100);  // x, y coordinates

        // Scale image to fit within specified dimensions
        image.ScaleToFit(200, 200);  // Width, Height

        // Add image to document
        document.Add(image);

        // Close the document
        document.Close();
    }
}
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.IO.Image;

class Program
{
    public static void Main(string[] args)
    {
        // Initialize PDF writer
        var pdfWriter = new PdfWriter("output.pdf");

        // Initialize PDF document
        var pdfDocument = new iText.Kernel.Pdf.PdfDocument(pdfWriter);

        // Initialize document
        var document = new Document(pdfDocument);

        // Create ImageData from image file
        ImageData imageData = ImageDataFactory.Create("iText.png");

        // Create an Image instance
        Image image = new Image(imageData);

        // Set fixed position for the image in the PDF
        image.SetFixedPosition(50, 100);  // x, y coordinates

        // Scale image to fit within specified dimensions
        image.ScaleToFit(200, 200);  // Width, Height

        // Add image to document
        document.Add(image);

        // Close the document
        document.Close();
    }
}
$vbLabelText   $csharpLabel

如何添加多張圖片並處理複雜的佈局?

對於建立產品目錄相簿等複雜場景,您需要手動管理佈局。 IronPDF的目錄功能書籤支援可以整理多頁文件:

using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.IO.Image;
using iText.Layout.Properties;

// Create a grid of images
var pdfWriter = new PdfWriter("catalog.pdf");
var pdfDocument = new PdfDocument(pdfWriter);
var document = new Document(pdfDocument);

// Add images in a grid layout
float xPosition = 50;
float yPosition = 750;
int imagesPerRow = 3;
int currentImage = 0;

string[] imageFiles = { "product1.jpg", "product2.jpg", "product3.jpg", 
                       "product4.jpg", "product5.jpg", "product6.jpg" };

foreach (string imagePath in imageFiles)
{
    ImageData imageData = ImageDataFactory.Create(imagePath);
    Image image = new Image(imageData);

    // Calculate position
    int column = currentImage % imagesPerRow;
    int row = currentImage / imagesPerRow;

    float x = xPosition + (column * 180);
    float y = yPosition - (row * 250);

    image.SetFixedPosition(x, y);
    image.ScaleToFit(150, 200);

    document.Add(image);
    currentImage++;
}

document.Close();
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.IO.Image;
using iText.Layout.Properties;

// Create a grid of images
var pdfWriter = new PdfWriter("catalog.pdf");
var pdfDocument = new PdfDocument(pdfWriter);
var document = new Document(pdfDocument);

// Add images in a grid layout
float xPosition = 50;
float yPosition = 750;
int imagesPerRow = 3;
int currentImage = 0;

string[] imageFiles = { "product1.jpg", "product2.jpg", "product3.jpg", 
                       "product4.jpg", "product5.jpg", "product6.jpg" };

foreach (string imagePath in imageFiles)
{
    ImageData imageData = ImageDataFactory.Create(imagePath);
    Image image = new Image(imageData);

    // Calculate position
    int column = currentImage % imagesPerRow;
    int row = currentImage / imagesPerRow;

    float x = xPosition + (column * 180);
    float y = yPosition - (row * 250);

    image.SetFixedPosition(x, y);
    image.ScaleToFit(150, 200);

    document.Add(image);
    currentImage++;
}

document.Close();
$vbLabelText   $csharpLabel

PDF 檢視器顯示一個居中的 ITEXT 徽標,在徽標下方是

圖片被加入到座標 (50, 100) 處,並縮放至 200x200 像素大小。 PdfWriter 建立輸出文件,啟用PDF 寫入器功能以處理影像。 為了更好地組織內容,可以考慮使用頁碼頁首/頁尾

如何在iTextSharp中控制影像位置和尺寸?

iTextSharp 透過手動座標計算提供對定位和大小的控制。 您可以縮放影像,同時保持寬高比,或拉伸到特定尺寸。 SetFixedPosition 方法提供精確的放置控制,您可以操縱對齊和旋轉。 但是,它缺少 IronPDF 的視口控制響應式尺寸選項

IronPDF和 iTextSharp 的效能比較如何?

這兩個庫在處理向 PDF 添加圖像方面存在顯著差異:

  • IronPDF表現提升,能夠有效率地處理包含多張圖片的大型文件。 它能更快地渲染 PDF 文件,尤其適用於圖形密集型文件。 該庫支援非同步操作並行處理,以提高吞吐量。 記憶體流操作可減少磁碟 I/O,從而提高效能。

  • iTextSharp效能良好,但處理非常大的 PDF 檔案或大量高解析度影像時可能會遇到困難。 雖然效率很高,但一些開發人員反映,在涉及多個頁面操作的複雜用例中,渲染速度比IronPDF慢。 該庫缺乏用於網頁優化的內建線性化支援

有哪些進階影像功能可用?

它們如何處理不同的影像格式?

IronPDF支援所有影像格式:

iTextSharp支援標準格式:

  • JPEG/JPG
  • 帶有透明通道的PNG影像
  • GIF(僅第一幀)
  • TIFF(單頁)
  • BMP 和其他點陣圖格式

影像品質和壓縮情況如何?

IronPDF提供內建的壓縮選項,可在檔案大小和品質之間取得平衡。 該庫提供資料清理功能資料扁平化功能

// Configure compression when rendering
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.JpegQuality = 85; // 0-100 quality scale

// Compress existing PDFs with images
PdfDocument pdf = PdfDocument.FromFile("large-images.pdf");
pdf.CompressImages(60); // Compress to 60% quality
pdf.SaveAs("compressed.pdf");

// Additional optimization options
renderer.RenderingOptions.GrayScale = true; // Convert to grayscale
renderer.RenderingOptions.DPI = 150; // Reduce DPI for web viewing
// Configure compression when rendering
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.JpegQuality = 85; // 0-100 quality scale

// Compress existing PDFs with images
PdfDocument pdf = PdfDocument.FromFile("large-images.pdf");
pdf.CompressImages(60); // Compress to 60% quality
pdf.SaveAs("compressed.pdf");

// Additional optimization options
renderer.RenderingOptions.GrayScale = true; // Convert to grayscale
renderer.RenderingOptions.DPI = 150; // Reduce DPI for web viewing
$vbLabelText   $csharpLabel

iTextSharp需要透過影像資料處理進行手動壓縮處理,這增加了品質控制的實現複雜性。 您必須自行實現灰階轉換DPI調整

如何從現有PDF文件中提取影像?

IronPDF提供可靠的影像擷取功能

// Extract all images from a PDF
PdfDocument pdf = PdfDocument.FromFile("document.pdf");
var images = pdf.ExtractAllImages();

foreach (var image in images)
{
    // Save extracted images
    image.SaveAs($"extracted_image_{images.IndexOf(image)}.png");
}

// Extract images from specific pages
var pageImages = pdf.ExtractImagesFromPage(0); // First page
// Extract all images from a PDF
PdfDocument pdf = PdfDocument.FromFile("document.pdf");
var images = pdf.ExtractAllImages();

foreach (var image in images)
{
    // Save extracted images
    image.SaveAs($"extracted_image_{images.IndexOf(image)}.png");
}

// Extract images from specific pages
var pageImages = pdf.ExtractImagesFromPage(0); // First page
$vbLabelText   $csharpLabel

授權和定價模式有哪些?

  • IronPDF: IronPDF提供簡單直接的永久許可證,只需一次購買。 這種方法具有明顯的優勢,可以避免持續的成本或開源授權要求。 該庫提供全面的支援和定期更新許可證驗證是自動化的,並支援多種部署場景

  • iTextSharp: iTextSharp 遵循 AGPL 許可,對開源專案免費,但對 Web 應用程式要求公開原始碼發布。 商業許可不受AGPL限制。 許可模式可能會使雲端部署變得複雜。

我應該選擇哪個庫來為PDF添加圖像?

 IronPDF和 iTextSharp PDF 庫的功能對比表,展示了 IronPDF 在易用性、性能、許可靈活性、完整的圖像格式支援以及企業級 PDF 生成高級定位功能方面的優勢

IronPDF和 iTextSharp 都提供了在 C# 中在 PDF 中添加影像的有效工具,各有其獨特的優勢。 IronPDF以易用性、效能和許可靈活性著稱,是處理複雜 PDF 和影像的理想選擇,且程式碼量極少。 它為大型 PDF 文件提供了更好的可擴展性,並提供一次性購買許可,避免了重複性成本或複雜的開源限制。

該程式庫支援SVG和 WebP 等現代格式,並具有內建壓縮功能和直覺的定位 API,適合需要高品質文件產生的企業應用程式。 IronPDF 的Chrome 渲染引擎可確保在所有平台上實現像素級完美輸出

iTextSharp 適合開源項目,但需要編寫更多程式碼才能獲得相同的結果,並且處理較大的 PDF 檔案時可能會遇到效能問題。 其底層 API 提供精細的控制,但增加了開發複雜性。 該函式庫缺少響應式 CSS 支援JavaScript渲染等現代功能。

對於企業應用而言,IronPDF 的優點包括Docker 支援AWS Lambda 相容性Azure Functions 整合。 該圖書館還提供符合法規要求的PDF/A 合規性PDF/UA 可近性

想要簡化您的 PDF 圖片處理流程嗎? IronPDF可以用最少的程式碼有效率地將圖片加入 PDF 中。 試試IronPDF ,體驗在 C# 專案中簡化影像整合。 此實作方案為您處理了複雜性,節省了時間並改善了您的PDF 生成工作流程。 探索完整的功能集,加入數千名簡化 PDF 操作的開發者行列。

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

常見問題解答

如何在 .NET 應用程式中添加影像到 PDF?

您可以使用 IronPDF 的 ImageStamper 功能添加影像到 PDF,該功能允許輕鬆嵌入影像,且能精確控制其位置和縮放。

使用 IronPDF 處理大型 PDF 檔案的效能優勢是什麼?

IronPDF 對於大型 PDF 檔案和高解析度影像進行了高效能優化,特別適合需要高效能處理的伺服器端應用程式。

IronPDF 如何簡化將影像嵌入 PDF 的過程?

IronPDF 通過直觀的 API 簡化了影像嵌入過程,只需少量程式碼即可在 PDF 文件中放置和縮放影像,從而提高開發者的工作效率。

IronPDF 與 iTextSharp 的授權模型有何不同?

IronPDF 提供一次性購買的永久授權,免於定期訂閱費用,而 iTextSharp 則使用 AGPL 授權,對於專有項目則有商業選項可選。

能否提供一個使用 IronPDF 添加影像到 PDF 的程式碼範例?

當然!您可以使用 IronPDF 的 PdfDocumentImageStamper 類別以簡單的程式碼將影像嵌入 PDF,文章中的範例即是實證。

影像放置自定義在 IronPDF 和 iTextSharp 之間有什麼區別?

IronPDF 提供簡便的影像放置自定義帶有對齊和偏移設置,而 iTextSharp 使用如 SetFixedPosition 方法提供詳細的影像定位控制。

在選擇 IronPDF 和 iTextSharp 之間需要考慮哪些關鍵因素?

選擇 IronPDF 和 iTextSharp 的取決於像是易用性、大文件效能及授權需求等因素。IronPDF 以其用戶友好的方式和可擴展性受到青睞,而 iTextSharp 適合開放源項目。

為什麼推薦使用 IronPDF 處理高效能伺服器應用程式?

IronPDF 的架構設計用於高效能,能夠有效處理大型文件和影像,是需要速度和可靠性的伺服器應用程式的理想選擇。

添加影像到 PDF 時,常見的故障排除步驟有哪些?

在解決 PDF 影像嵌入問題時,確保影像路徑正確、檢查文件訪問權限,以及確認影像格式被所用 PDF 庫支援。

IronPDF 的試用版如何幫助開發者?

IronPDF 的試用版允許開發者探索其功能和測試其處理 PDF 影像嵌入的能力,提供在 C# 專案中簡化 PDF 處理的機會。

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