如何將 IronPDF 庫安裝到 .NET 專案中
安裝這個C# PDF庫耗時不到 5 分鐘。
開發免費軟體可以透過 NuGet 和直接下載取得,透過這個教程,我們會讓你在 Visual Studio 中開始運行。 請按照以下說明開始在您的.NET專案中將HTML轉換為PDF。
逐步安裝 C# PDF 函式庫
如何教程
1.1. 通過 NuGet 安裝 IronPDF
使用以下幾個步驟來安裝IronPDF NuGet 庫從 Visual Studio 內進行。
在方案總管中,右鍵點選 參考,管理 NuGet 套件
選擇瀏覽並搜尋
"IronPdf"
- 選擇套件並安裝。
Install-Package IronPdf
亦有適用於特定部署的 IronPDF NuGet 套件可用。Mac, Linux, Azure, Docker和 AWS 在我們的文件中記錄的内容IronPDF 高級 NuGet 安裝指南.
1.2. 通過DLL下載安裝IronPDF
安裝IronPDF的第二種方法是下載它。 按照以下快速簡便的步驟:
下載並解壓縮 WindowsIronPDF 檔案至您的解決方案目錄下的 ~/Libs 等位置
在 Visual Studio 的解決方案總管中,右鍵點擊『相依性』,然後選擇『加入專案參考』。 選擇瀏覽並包含從 zip 中提取的所有 dll。
以下是針對特定平台可用的其他 IronPDF DLL 壓縮包:
應用授權金鑰
在使用IronPDF之前,請在應用程序啟動時包含此程式碼。 此方法普遍有效且易於實施。
IronPdf.License.LicenseKey = "YOUR-IRONPDF-LICENSE-KEY";
IronPdf.License.LicenseKey = "YOUR-IRONPDF-LICENSE-KEY";
IronPdf.License.LicenseKey = "YOUR-IRONPDF-LICENSE-KEY"
如果您不希望使用內嵌程式碼來應用授權金鑰,請訪問「IronPDF 授權金鑰探索替代方法的文章。
2. 授予檔案或資料夾的必要存取權限
有時可能需要在您的電腦上為某些用戶或角色添加權限。
例如,每個應用程式域需要自己的TempFolderPath和相同中的應用程序應用程式集區不能共享 TempFolderPath。
這是什麼意思?
AppDomain 只是提供了一個在某個進程中的隔離層。 您所認為的一切都是每程式,實際上是每個 AppDomain。 這些應用程式池中的每個應用程式都需要各自的臨時資料夾,以便能完全獨立運作。
如果需要上述任何疑难排解选项,可以通过以下方式设置权限:
在檔案或資料夾上點擊右鍵
選擇屬性
選擇安全性
按一下編輯…
- 選擇所需的權限。
3. 設定安裝路徑
渲染HTML 作為 PDF,IronPDF 必須嵌入 Chromium,這是一個安全的網路瀏覽器。 幸運的是,這個過程是完全自動化的。
如果 IronPDF 的 HTML 轉 PDF 拋出「渲染失敗」異常(這種情況極不可能發生),您可能需要將本機瀏覽器二進位檔解壓縮到合適的位置。 臨時資料夾通常是理想的選擇。
注意:Program Files 永遠不是合適的位置。
設置 IronPDF.Installation.TempFolderPath
您可以通过设置 TempFolderPath 属性将文件解压到正确的位置IronPdf.Installation物件如下所示:
IronPdf.Installation.TempFolderPath = @"C:\My\Safe\Path";
在更新路徑後,請務必清除開發環境和伺服器上的所有暫存和快取資料夾。 重新部署您的應用程式的乾淨版本。
在應用程式範圍設置臨時資料夾環境變數
IronPDF 在呈現和編輯 PDF 文件時,可能會偶爾在系統臨時文件夾中生成臨時文件。 我們也可以設置 IronPdf.Installation.TempFolderPath 來解決這個問題,但環境臨時路徑目錄有時仍可能被第三方套件使用。
為了解決此問題,我們可以在 C# 應用程序啟動時設置 TempPath 環境變量。這確保您的應用程序創建的所有臨時文件都存儲在一個可控的位置。
using IronPdf;
// Set Application scope Temp Files Path.
// This changes System.IO.Path.GetTempFileName and System.IO.Path.GetTempPath behavior for the entire .NET application
var MyTempPath = @"C:\Safe\Path\";
Environment.SetEnvironmentVariable("TEMP", MyTempPath, EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("TMP", MyTempPath, EnvironmentVariableTarget.Process);
// Set IronPDF Temp Path
IronPdf.Installation.TempFolderPath = System.IO.Path.Combine(MyTempPath, "IronPdf");
// Your PDF Generation and editing code here..E.G.
var Renderer = new IronPdf.ChromePdfRenderer();
using var Doc = Renderer.RenderHtmlAsPdf("<h1>Html with CSS and Images</h1>");
Doc.SaveAs("example.pdf");
using IronPdf;
// Set Application scope Temp Files Path.
// This changes System.IO.Path.GetTempFileName and System.IO.Path.GetTempPath behavior for the entire .NET application
var MyTempPath = @"C:\Safe\Path\";
Environment.SetEnvironmentVariable("TEMP", MyTempPath, EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("TMP", MyTempPath, EnvironmentVariableTarget.Process);
// Set IronPDF Temp Path
IronPdf.Installation.TempFolderPath = System.IO.Path.Combine(MyTempPath, "IronPdf");
// Your PDF Generation and editing code here..E.G.
var Renderer = new IronPdf.ChromePdfRenderer();
using var Doc = Renderer.RenderHtmlAsPdf("<h1>Html with CSS and Images</h1>");
Doc.SaveAs("example.pdf");
Imports IronPdf
' Set Application scope Temp Files Path.
' This changes System.IO.Path.GetTempFileName and System.IO.Path.GetTempPath behavior for the entire .NET application
Private MyTempPath = "C:\Safe\Path\"
Environment.SetEnvironmentVariable("TEMP", MyTempPath, EnvironmentVariableTarget.Process)
Environment.SetEnvironmentVariable("TMP", MyTempPath, EnvironmentVariableTarget.Process)
' Set IronPDF Temp Path
IronPdf.Installation.TempFolderPath = System.IO.Path.Combine(MyTempPath, "IronPdf")
' Your PDF Generation and editing code here..E.G.
Dim Renderer = New IronPdf.ChromePdfRenderer()
Dim Doc = Renderer.RenderHtmlAsPdf("<h1>Html with CSS and Images</h1>")
Doc.SaveAs("example.pdf")
4. 權限和 IIS
在伺服器被鎖定的情況下,您可能需要將權限賦予IIS使用者(IUSER)對您的安裝路徑資料夾,以及您的Windows和Temp資料夾擁有讀寫權限。
5. Microsoft Visual C++ 和 Windows 相容性
IronPDF 需要在目標 Windows 機器上安裝 Microsoft Visual C++。如果需要,可以將其與應用程序安裝程序(如 MSI)一起打包。
Microsoft Visual C++ 是 Windows 的一個組件,通常在現代版本的 Windows 中都會存在,除非已被故意刪減。
.NET Framework 可以以 32 位元執行(即使在64位平台上),因此有必要安裝32位和64位版本的Visual C++。
在這裡您可以下載 Microsoft Visual C++.
6. Linux 相容性
- IronPDF 支援 Linux。 我們正式支援:Ubuntu、Debian、CentOS、Fedora 及 Amazon Linux 2.
- 在 Linux 上部署 IronPDF文件齊全,是 Azure 等雲端部署的熱門選擇。
7. Docker 相容性
- 在 Docker 上部署 IronPDF文檔齊全。
- 我們正式支持以下 Docker 平台:Windows、Ubuntu、Debian、CentOS 和 Amazon Linux 2 並提供工作中的 Docker 文件.
8. 與 Azure 相容性
- 正式支援 Azure WebApps、Azure WebJobs、Azure Functions 以及 Azure Docker 實例和 Azure 虛擬機。
- 閱讀IronPDFAzure 及 Azure Function 設定指南.
9. Amazon AWS Lambda 相容性
- 一個Amazon AWS Lambda 教學與支援已包含。
10. macOS 相容性
- 正式支援 macOS 的部署與開發使用 Rider 和 "Visual Studio for Mac" 是受支持的
- 請閱讀我們的macOS 綜合指南.