產品比較 探索.NET中向PDF添加圖像的最佳替代方案 Curtis Chau 更新日期:8月 31, 2025 Download IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article 在.NET環境中處理PDF文件時,向文件中添加圖像是許多應用程序的常見需求。 無論是生成發票、報告還是自定義文件,將圖像嵌入PDF的功能都是必不可少的。 Two of the most popular PDF libraries in C# are IronPDF and iTextSharp. 在本文中,我們將比較兩個庫在將圖像添加到PDF方面的能力,考慮使用的便捷性、性能、許可證和功能。 IronPDF和iTextSharp的主要區別 與添加圖像相關的功能 IronPDF: IronPDF使得在PDF中添加圖像變得簡單,內建支持嵌入本地和遠程圖像。 其API允許對圖像在文檔中的位置、原始寬度和縮放進行精細控制。 iTextSharp: iTextSharp也提供嵌入圖像的功能。 它提供了一個靈活而強大的API,可以處理包括JPG和PNG在內的多種圖像類型。 然而,在自定義方面,它可能需要更多的代碼行,特別是在定位或調整圖像大小時。 性能考量 IronPDF: 因其效率而聞名,IronPDF可以流暢地處理大型PDF和圖像。 當生成或修改包含嵌入圖形的PDF時,其性能更為出色,這使其成為需要高性能的服務器應用程序的絕佳選擇。 iTextSharp: 雖然iTextSharp是一個可靠的庫,但在某些情況下會比較慢,特別是對於大文件或複雜的圖像操作。 然而,對於大多數應用程序來說,它仍然是一個合適的選擇。 許可證和定價模式 IronPDF: IronPDF提供永久許可證,這意味著只需一次性購買即可無限期使用。 這對於那些希望避免經常性訂閱費用的開發者非常理想。 iTextSharp: iTextSharp適用於開源使用的AGPL(Affero General Public License),這意味著如果您在項目中使用它,則必須發布您的源代碼,或者可以選擇商業許可證來避免這種情況。 使用IronPDF向PDF添加圖像 在C#項目中設置IronPDF 在添加圖像之前,您需要在最新版本的C#或Visual Basic項目中安裝IronPDF。 您可以通過NuGet進行安裝: Install-Package IronPdf 安裝完成後,您就可以開始向PDF文檔中添加圖像。 使用IronPDF將圖像添加到PDF:代碼示例 以下代碼示例演示瞭如何使用IronPDF向PDF中添加圖像: 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("https://ironpdf.com/img/svgs/iron-pdf-logo.svg")); // Apply the stamp (image) to the PDF document pdf.ApplyStamp(stamper); // Save the modified PDF to a file pdf.SaveAs("output.pdf"); } } 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("https://ironpdf.com/img/svgs/iron-pdf-logo.svg")); // Apply the stamp (image) to the PDF document pdf.ApplyStamp(stamper); // Save the modified PDF to a file pdf.SaveAs("output.pdf"); } } Imports IronPdf Imports IronPdf.Editing Friend Class Program Public Shared Sub Main(ByVal args() As String) ' Create a renderer to convert HTML to PDF Dim renderer As New ChromePdfRenderer() ' Render a basic PDF with some HTML content Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>") ' Create an ImageStamper with the image URL Dim stamper As New ImageStamper(New Uri("https://ironpdf.com/img/svgs/iron-pdf-logo.svg")) ' Apply the stamp (image) to the PDF document pdf.ApplyStamp(stamper) ' Save the modified PDF to a file pdf.SaveAs("output.pdf") End Sub End Class $vbLabelText $csharpLabel 在此示例中,我們首先使用ChromePdfRenderer類從HTML字符串中渲染一個新的PDF。 然後,使用IronPDF的圖像戳記工具,我們從提供的字符串URL創建一個新圖像並將其應用到PDF中。 此示例展示了如何僅用幾行代碼就可以使用IronPDF將圖像添加到您的PDF頁面中。 自定義圖像放置和大小 IronPDF在圖片放置方面提供了廣泛的自定義。 您可以通過設置圖像戳記工具的對齊和偏移功能來指定圖像在頁面上的位置。 使用iTextSharp向PDF添加圖像 在C#項目中設置iTextSharp 要開始使用iTextSharp,您需要通過NuGet安裝此庫: Install-Package itext7 設置完成後,您可以繼續向您的PDF中添加圖像。 使用iTextSharp將圖像添加到PDF:代碼示例 以下是如何使用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(); } } Imports iText.Kernel.Pdf Imports iText.Layout Imports iText.Layout.Element Imports iText.IO.Image Friend Class Program Public Shared Sub Main(ByVal args() As String) ' Initialize PDF writer Dim pdfWriter As New PdfWriter("output.pdf") ' Initialize PDF document Dim pdfDocument = New iText.Kernel.Pdf.PdfDocument(pdfWriter) ' Initialize document Dim document As New Document(pdfDocument) ' Create ImageData from image file Dim imageData As ImageData = ImageDataFactory.Create("iText.png") ' Create an Image instance Dim image As 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() End Sub End Class $vbLabelText $csharpLabel 在此示例中,圖像被添加到坐標(50, 100),並縮放以適應200x200像素的區域。 PdfWriter用於創建輸出文件,使得可以操縱PDF編寫器功能以處理由圖像引起的內容。 自定義圖像放置和大小 iTextSharp提供了更多的圖像定位和大小控制權限。您可以在保持圖像縱橫比的同時進行縮放,或者將其拉伸到特定尺寸。 SetFixedPosition方法提供了精確的定位控制,您還可以操作圖像的對齊和旋轉。 性能比較:IronPDF vs iTextSharp 就性能而言,兩個庫都能處理將圖像添加到PDF的任務,但它們之間存在一些差異: IronPDF在性能上得到了優化,可以高效地處理多個圖像的巨大文檔。 它在渲染PDF時更快,特別是對於需要大量圖形內容的文檔。 iTextSharp提供了良好的性能,但可能在非常大的PDF文件或大量高分辨率圖像中遇到困難。 雖然它仍然相當高效,但一些開發人員報告說,與IronPDF相比,它們的渲染時間較慢,特別是在更複雜的用例中。 許可證和定價模式 IronPDF: IronPDF提供了簡單的永久許可證,只需要一次購買。 這對於那些偏好不涉及持續成本或開源許可證要求的開發者來說,是有利的。 iTextSharp: iTextSharp遵循AGPL許可證,這對於開源項目是免費使用的,但如果在網絡應用程序中使用該庫,則要求公開源代碼。 對於商業用途,iTextSharp提供有償商業許可證來避免AGPL的限制。 結論 無論是IronPDF還是iTextSharp,都可以為在C#中向PDF添加圖像提供功能強大的工具,但它們各自具有不同的優勢。 IronPDF因使用便捷、性能和許可證靈活性而脫穎而出,這使其成為希望用更少的代碼處理複雜的PDF和圖像的開發者的理想選擇。 它還為大型PDF提供更好的可擴展性,並提供一次性購買的許可證模式,避免了需要經常性成本或複雜的開源許可證限制。 另一方面,iTextSharp可能是開源項目的好選擇,儘管它可能需要更多的代碼來實現相同的結果,並且在面對更大的PDF時可能會遇到性能問題。 準備好簡化您的PDF圖像處理了嗎? IronPDF為您提供了一個以極少代碼行添加圖像到PDF的無縫高效的體驗。 嘗試IronPDF看看將圖像支持集成到您的C#項目是多麼容易。 使用IronPDF,您可以節省時間,降低復雜性,並增強您的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 的 PdfDocument 和 ImageStamper 類別以簡單的程式碼將影像嵌入 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 機器人,結合科技與創意的樂趣。 相關文章 發表日期 11月 13, 2025 C# HTML 與 PDF 開源版本比較 IronPDF 將開源 HTML 轉 PDF 庫與 IronPDF for C# 進行比較。探索哪種解決方案能為您的 .NET 專案提供最佳的 PDF 生成功能。 閱讀更多 發表日期 10月 27, 2025 哪個 ASP.NET Core PDF 庫具有最佳價值? 發現適用於 ASP.NET Core 應用程式的最佳 PDF 庫。比較 IronPDF 的 Chrome 引擎與 Aspose 和 Syncfusion 的替代方案。 閱讀更多 發表日期 10月 27, 2025 如何使用 Aspose C# 和 IronPDF 創建 PDF 通過這份針對開發人員設計的分步指南,學習如何使用 Aspose C# 與 IronPDF 創建 PDF。 閱讀更多 探索PDFsharp向PDF添加水印的最佳替代方案探索PDFsharp的最佳PDF到圖像...
發表日期 11月 13, 2025 C# HTML 與 PDF 開源版本比較 IronPDF 將開源 HTML 轉 PDF 庫與 IronPDF for C# 進行比較。探索哪種解決方案能為您的 .NET 專案提供最佳的 PDF 生成功能。 閱讀更多
發表日期 10月 27, 2025 哪個 ASP.NET Core PDF 庫具有最佳價值? 發現適用於 ASP.NET Core 應用程式的最佳 PDF 庫。比較 IronPDF 的 Chrome 引擎與 Aspose 和 Syncfusion 的替代方案。 閱讀更多
發表日期 10月 27, 2025 如何使用 Aspose C# 和 IronPDF 創建 PDF 通過這份針對開發人員設計的分步指南,學習如何使用 Aspose C# 與 IronPDF 創建 PDF。 閱讀更多