跳過到頁腳內容
使用IRONPDF

C#將圖像添加到PDF(開發者教程)

從開發人員的角度來看,由於影像格式的多樣性和操作的複雜性,以程式設計方式為 PDF 文件添加影像是一項具有挑戰性的任務。 因此,建議使用 IronPDF C# 庫以程式設計方式為 PDF 文件添加影像。 讓我們一起來了解一下IronPDF是什麼以及如何有效地使用它。

IronPDF:C# PDF 函式庫

IronPDF C# 函式庫是一個用 C# 寫的 PDF 函式庫,目標模型是 PDF 物件模型。 該程式庫為開發人員提供了一種創建、編輯和保存 PDF 文件的方法,而無需與 Adobe Acrobat 等特定 API 保持緊密的關聯性。 當您不想使用 Adobe Acrobat 或其他單獨的軟體時,可以使用 IronPDF C# 程式庫。

該程式庫為開發人員提供了大量工具,用於使用 C# 建立和編輯 PDF 文件,以及保存 PDF 文件,並且還提供了其他 .NET PDF 庫不提供的功能。 許多開發人員更喜歡 IronPDF C# 庫,因為它在 Windows、Linux 和 macOS 平台上提供了他們想要的 PDF 庫的所有功能,而且完全免費! IronPDF不斷增加功能並擴展服務,使其成為滿足您PDF需求的最佳實用工具。 對於需要瀏覽、搜尋、尋找、從 PDF 文件中提取資料或建立 PDF 文件的使用者來說,該圖書館的功能遠遠超出基本要求。 讓我們來看看如何使用 IronPDF 將影像新增到 PDF 文件中。

建立或開啟 C# 項目

若要為 PDF 文件中新增影像,建議使用最新版本的 Visual Studio 建立 C# 項目,以獲得流暢的體驗。

  • 開啟 Visual Studio。

如何使用 C# 在 PDF 中新增影像,圖 1:Visual Studio 啟動介面 Visual Studio 啟動 UI

  • 點選"建立新項目"按鈕。
  • 從專案範本中選擇"C# 控制台應用程式",然後按一下"下一步"按鈕。 您可以根據自己的需求選擇平台。

如何使用 C# 在 PDF 中新增圖像,圖 2:在 Visual Studio 中建立控制台應用程式 在 Visual Studio 中建立控制台應用程式

接下來,為你的專案命名,然後點擊"下一步"按鈕。

  • 選擇目標 .NET Framework >= .NET Core 3.1 版本,然後按一下"建立"按鈕。

按照上述步驟,您就可以輕鬆建立一個新的 C# 專案。 您可以使用現有的 C# 項目。 只需打開項目並安裝 IronPDF 庫即可。

安裝 IronPDF 函式庫

IronPDF庫可以透過多種方式安裝。

*使用 NuGet 套件管理器* 使用套件管理器控制台**

使用 NuGet 套件管理器

若要使用 NuGet 套件管理器安裝該庫,請依照下列步驟操作:

  • 從主選單選項中選擇"工具" > "NuGet 套件管理員" > "管理解決方案的 NuGet 套件"

如何使用 C# 在 PDF 中新增影像,圖 3:導覽至 NuGet 套件管理器 導覽至 NuGet 套件管理器

這將開啟 NuGet 套件管理器視窗。 前往瀏覽選項卡並蒐索 IronPDF。 選擇 IronPDF 庫,然後按一下"安裝"按鈕。

圖 4:如何使用 C# 在 PDF 中新增映像:從 NuGet 套件管理器安裝 IronPDF 套件 從 NuGet 套件管理器安裝 IronPDF 套件

使用軟體套件管理器控制台

以下是使用控制台安裝 IronPDF 庫的步驟。

  • 開啟程式套件管理員控制台(通常位於 Visual Studio 的底部)。
  • 輸入以下指令開始安裝 IronPDF 庫。
Install-Package IronPdf

它將開始安裝,您將能夠看到安裝進度。 安裝完成後,您就可以很快地在專案中使用 IronPDF 庫。

如何使用 C# 在 PDF 中新增圖像,圖 5:


庫已經安裝完畢,現在是時候編寫程式碼將圖像添加到 PDF 文件中了。 首先導入 IronPDF 命名空間。 所以,請在你的程式碼檔案中寫入以下這行程式碼:

using IronPdf;
using IronPdf;
Imports IronPdf
$vbLabelText   $csharpLabel

在 PDF 文件中新增點陣圖和影像

使用 IronPDF 為 PDF 文件添加圖像有多種方法:使用直接圖像檔案、將圖像轉換為位元組,或使用 System.Drawing.Bitmap。 此外,IronPDF 庫支援多種影像格式。

我們來看一下:

using IronPdf;
using System.IO;
using System.Drawing;

class PDFImageAdder
{
    /* This method demonstrates how to convert an image file to a PDF document in C# */
    static void Main(string[] args)
    {
        // Initialize IronPdf Renderer
        var renderer = new IronPdf.ChromePdfRenderer();

        // Read the PNG image file into binary format
        var pngBinaryData = File.ReadAllBytes("embed_me.png");

        // Convert image binary data to base64 for embedding in HTML
        var ImgDataURI = "data:image/png;base64," + Convert.ToBase64String(pngBinaryData);

        // Embed the image as a base64 data URI in an HTML <img> tag
        var ImgHtml = $"<img src='{ImgDataURI}'>";

        // Render the HTML as a PDF document
        using var pdfdoc = renderer.RenderHtmlAsPdf(ImgHtml);

        // Save the rendered PDF document
        pdfdoc.SaveAs("embedded_example_1.pdf");
    }
}
using IronPdf;
using System.IO;
using System.Drawing;

class PDFImageAdder
{
    /* This method demonstrates how to convert an image file to a PDF document in C# */
    static void Main(string[] args)
    {
        // Initialize IronPdf Renderer
        var renderer = new IronPdf.ChromePdfRenderer();

        // Read the PNG image file into binary format
        var pngBinaryData = File.ReadAllBytes("embed_me.png");

        // Convert image binary data to base64 for embedding in HTML
        var ImgDataURI = "data:image/png;base64," + Convert.ToBase64String(pngBinaryData);

        // Embed the image as a base64 data URI in an HTML <img> tag
        var ImgHtml = $"<img src='{ImgDataURI}'>";

        // Render the HTML as a PDF document
        using var pdfdoc = renderer.RenderHtmlAsPdf(ImgHtml);

        // Save the rendered PDF document
        pdfdoc.SaveAs("embedded_example_1.pdf");
    }
}
Imports IronPdf
Imports System.IO
Imports System.Drawing

Friend Class PDFImageAdder
	' This method demonstrates how to convert an image file to a PDF document in C# 
	Shared Sub Main(ByVal args() As String)
		' Initialize IronPdf Renderer
		Dim renderer = New IronPdf.ChromePdfRenderer()

		' Read the PNG image file into binary format
		Dim pngBinaryData = File.ReadAllBytes("embed_me.png")

		' Convert image binary data to base64 for embedding in HTML
		Dim ImgDataURI = "data:image/png;base64," & Convert.ToBase64String(pngBinaryData)

		' Embed the image as a base64 data URI in an HTML <img> tag
		Dim ImgHtml = $"<img src='{ImgDataURI}'>"

		' Render the HTML as a PDF document
		Dim pdfdoc = renderer.RenderHtmlAsPdf(ImgHtml)

		' Save the rendered PDF document
		pdfdoc.SaveAs("embedded_example_1.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

該程式首先會載入圖像。上述程式碼中的 ReadAllBytes 函數會將影像轉換為位元組格式。 之後,圖像資料將被編碼為 base64,並作為字串放入 HTML <img> 標籤中。 之後,將使用RenderHtmlAsPdf 方法函數將 HTML 字串渲染成 PDF。 它會在 PDF 文件中建立一個 PDF 頁面。

下一個範例將展示如何在 PDF 文件中使用點陣圖影像。 IronPDF 提供了一個有用的方法,可以將 System.Drawing.Image 嵌入到 HTML 文件中,然後可以渲染為 PDF。 請造訪以下ImageUtilities API以了解更多資訊。 以下程式碼將展示其工作原理:

using IronPdf;
using System.Drawing;

class PDFImageAdder
{
    static void Main(string[] args)
    {
        // Initialize IronPdf Renderer
        var renderer = new IronPdf.ChromePdfRenderer();

        // Create a Bitmap image
        Bitmap MyImage = new Bitmap("Path-to-Your-Image");

        // Convert the Bitmap image to a Data URI
        string DataURI = IronPdf.Util.ImageToDataUri(MyImage);

        // Embed the image as a Data URI in an HTML <img> tag
        var ImgHtml = $"<img src='{DataURI}'>";

        // Render the HTML to PDF
        using var pdfdoc2 = renderer.RenderHtmlAsPdf(ImgHtml);

        // Save the PDF document
        pdfdoc2.SaveAs("embedded_example_2.pdf");
    }
}
using IronPdf;
using System.Drawing;

class PDFImageAdder
{
    static void Main(string[] args)
    {
        // Initialize IronPdf Renderer
        var renderer = new IronPdf.ChromePdfRenderer();

        // Create a Bitmap image
        Bitmap MyImage = new Bitmap("Path-to-Your-Image");

        // Convert the Bitmap image to a Data URI
        string DataURI = IronPdf.Util.ImageToDataUri(MyImage);

        // Embed the image as a Data URI in an HTML <img> tag
        var ImgHtml = $"<img src='{DataURI}'>";

        // Render the HTML to PDF
        using var pdfdoc2 = renderer.RenderHtmlAsPdf(ImgHtml);

        // Save the PDF document
        pdfdoc2.SaveAs("embedded_example_2.pdf");
    }
}
Imports IronPdf
Imports System.Drawing

Friend Class PDFImageAdder
	Shared Sub Main(ByVal args() As String)
		' Initialize IronPdf Renderer
		Dim renderer = New IronPdf.ChromePdfRenderer()

		' Create a Bitmap image
		Dim MyImage As New Bitmap("Path-to-Your-Image")

		' Convert the Bitmap image to a Data URI
		Dim DataURI As String = IronPdf.Util.ImageToDataUri(MyImage)

		' Embed the image as a Data URI in an HTML <img> tag
		Dim ImgHtml = $"<img src='{DataURI}'>"

		' Render the HTML to PDF
		Dim pdfdoc2 = renderer.RenderHtmlAsPdf(ImgHtml)

		' Save the PDF document
		pdfdoc2.SaveAs("embedded_example_2.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

在上面的程式碼中,ImageToDataUri 函數用於將圖像轉換為 URI 格式。 然後使用 RenderHtmlAsPdf 函數在 PDF 文件中繪製圖像。 這適用於多張圖片。

此外,IronPDF 還能夠在 PDF 中渲染圖表為 PDF 文件添加條碼,使用密碼和浮水印增強 PDF 安全性,甚至以程式設計方式處理 PDF 表單

授權

IronPDF 是一個優秀的 PDF 庫,可以幫助您建立和自訂 PDF 文件,現在即可購買。 但是,IronPDF 完全免費,可用於開發目的。 您也可以啟動免費試用版用於生產環境,無需提供任何付款資訊。 購買 IronPDF 後,Iron Software 將為您提供超值優惠,只需支付兩套 Iron Software 軟體包的價格即可購買五套 Iron Software 軟體包。 是的! 你沒聽錯——現在只需兩款產品的價格,即可購買一套五款 Iron Software 產品。 現在購買! 請造訪IronPDF 許可頁面以了解更多詳情。

常見問題解答

如何使用 C# 向 PDF 添加圖片?

您可以在 C# 中將圖片轉換為 base64 格式,將其嵌入到 HTML  related to 如何使用 C# 向 PDF 添加圖片? 標籤中,並使用 IronPDF 的 RenderHtmlAsPdf 方法將 HTML 轉換為 PDF。

設置 C# 項目以向 PDF 添加圖片的步驟是什麼?

要設置 C# 項目以向 PDF 添加圖片,請打開 Visual Studio,創建一個新的 C# 控制台應用程序,並確保您的項目目標是 .NET Framework 版本 3.1 或更高版本。通過 NuGet 包管理器或包管理器控制台安裝 IronPDF 庫。

如何在 C# 中將圖片轉換為 base64 以用於 PDF 嵌入?

在 C# 中,您可以通過將圖片文件讀取到字節數組然後使用 Convert.ToBase64String 將其轉換為 base64。此 base64 字符串可以嵌入到 HTML  related to 如何在 C# 中將圖片轉換為 base64 以用於 PDF 嵌入? 標籤中,以便使用 IronPDF 進行 PDF 轉換。

哪些圖片格式支持嵌入到 PDF?

IronPDF 支持多種圖片格式,允許您將各種類型的圖片嵌入到您的 PDF 文件中,包括 JPEG、PNG 和 BMP。

在 C# 中使用哪種方法將 Bitmap 圖片嵌入到 PDF?

要在 C# 中將 Bitmap 圖片嵌入到 PDF 中,將 Bitmap 轉換為數據 URI,使用 ImageToDataUri 函數將其嵌入到 HTML  related to 在 C# 中使用哪種方法將 Bitmap 圖片嵌入到 PDF? 標籤中,並使用 IronPDF 的 RenderHtmlAsPdf 方法將其渲染為 PDF。

如何在嵌入圖片的同時增強 PDF 的安全性?

IronPDF 提供增強 PDF 安全性的功能,如添加密碼和許可權。這可以與嵌入圖片的同時通過在 PDF 創建過程中設置安全選項來實現。

我可以隨著圖片向 PDF 添加圖表和條形碼嗎?

是的,IronPDF 允許您在圖片之外向 PDF 添加圖表和條形碼。這些元素可以通過 HTML 和 CSS 創建和渲染,然後使用 IronPDF 轉換為 PDF。

IronPDF 的許可選項有哪些?

IronPDF 提供開發的免費試用版和具有成本效益的許可選項以更大範圍地使用。更多資訊可在 IronPDF 許可頁面查詢。

如何使用軟件包管理器控制台安裝 IronPDF?

要使用包管理器控制台安裝 IronPDF,請使用命令 Install-Package IronPDF

IronPDF 是否與 .NET 10 相容?

是的 — IronPDF 完全兼容 .NET 10,以及早期版本包括 .NET 9、8、7、6、.NET Core、.NET Standard 和 .NET Framework。

Curtis Chau
技術作家

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

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

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我