Cefsharp.WPF.NET Core(开发者如何使用)
开发者现在可以轻松地将Chromium强大的Web浏览器引擎集成到他们的.NET桌面应用程序和WPF应用程序中,通过CefSharp,一个创新的.NET包装器,围绕Chromium嵌入式框架。 通过CefSharp,.NET应用程序开发者可以在定制的桌面界面中实现丰富的Web体验,使他们能够利用Chromium的Web功能和开发者工具,而无需任何外部依赖。 通过其广泛的功能选项,例如自定义选项和浏览器行为控制,这个框架使得整合现代Web技术如HTML5、CSS3和JavaScript更加容易。
用户与基于Web的内容在桌面解决方案中的交互和体验,通过CefSharp的跨平台互操作性和Web内容交互能力得以转变,从而提升桌面程序的水平,并在应用环境中实现动态的Web浏览体验。 在这篇文章中,我们将通过代码片段来更深入了解CefSharp的开源版本。
如何使用CefSharp
- 从Visual Studio项目中创建一个新的Windows窗体。
- 安装CefSharp库。
- 声明CefSharp所需的对象,这些对象已准备好进行最小化编译。
- 输入需要加载的URL。
- 运行代码。
安装CefSharp.WinForms:
- 使用Visual Studio的NuGet包管理器是安装CefSharp的最简单方法。
- 在Visual Studio中启动该项目。
- 在解决方案资源管理器中右键点击你的项目。
- 选择"管理NuGet包"。
- 转到"浏览"选项卡并搜索"CefSharp"。
- 选择适合你项目的CefSharp包(CefSharp.Wpf,CefSharp.WinForms等)。
- 点击"安装"将其包含到你的项目中。
CefSharp的重要性
CefSharp 是一个框架,它通过将Chromium的强大Web浏览器引擎无缝集成到.NET应用程序中,改变了开发者构建带有集成Web浏览功能的桌面应用和自动化项目的方式。 通过CefSharp的帮助,我们可以显示远程Web内容,包括使用HTML5支持构建的嵌入式UI。
在CefSharp引言中要强调的重要内容包括:
- Chromium Embedded Framework(CEF)作为CefSharp的框架,使得Chromium浏览器的功能能够集成到其他程序中。 通过使用Google的Chromium项目,CefSharp为桌面程序带来先进的Web功能。
- .NET集成: CefSharp是一个功能强大的Cef的.NET包装。 它使程序员能够轻松地使用C#或其他.NET语言,将一个快速的Web浏览器集成到他们的桌面.NET应用程序中。
- 桌面应用中的Web浏览功能: 使用CefSharp,开发者可以轻松地将Web浏览功能集成到他们的桌面程序中。 这个功能为开发者提供无限可能,使得他们能够将在线内容融入用户界面或开发出具有Web功能的独特桌面应用。 它可以在JavaScript事件触发时接收回调。
- 富Web技术支持: 使用CefSharp,开发者可以利用Chromium提供的所有功能,包括支持最新的Web标准如HTML5,CSS3,JavaScript,WebGL和其他现代Web标准。 这使得在桌面程序中包含尖端的Web技术成为可能。
- 灵活性和自定义: CefSharp为开发者提供了极大的灵活性和自定义选项,使他们能够处理事件,执行JavaScript,与在线内容交互,并调整浏览器行为以满足特定应用需求。 支持使用WebGL的CefSharp,它利用OpenGL/DirectX进行硬件加速渲染,支持3D内容。
通过使用CefSharp,开发者可以通过促进Web浏览体验、鼓励用户交互以及在应用环境中提供丰富的现代信息来提升桌面应用程序。
示例代码:
using System;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
namespace CefSharpExample
{
public partial class MainForm : Form
{
private ChromiumWebBrowser chromeBrowser;
public MainForm()
{
InitializeComponent();
// Initialize CefSharp settings
CefSettings settings = new CefSettings();
Cef.Initialize(settings);
// Create the ChromiumWebBrowser instance
chromeBrowser = new ChromiumWebBrowser("https://ironpdf.com/"); // Load a URL
// Add the ChromiumWebBrowser control to the form
this.Controls.Add(chromeBrowser);
chromeBrowser.Dock = DockStyle.Fill; // Fill the entire form
// Handle when the browser component has finished loading
chromeBrowser.LoadingStateChanged += ChromeBrowser_LoadingStateChanged;
}
private void ChromeBrowser_LoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
{
if (!e.IsLoading)
{
// Page has finished loading
// Perform actions after the page has loaded
Console.WriteLine("Finished loading.");
}
}
// Dispose of Cef resources when the form is closed
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
Cef.Shutdown();
}
}
}using System;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
namespace CefSharpExample
{
public partial class MainForm : Form
{
private ChromiumWebBrowser chromeBrowser;
public MainForm()
{
InitializeComponent();
// Initialize CefSharp settings
CefSettings settings = new CefSettings();
Cef.Initialize(settings);
// Create the ChromiumWebBrowser instance
chromeBrowser = new ChromiumWebBrowser("https://ironpdf.com/"); // Load a URL
// Add the ChromiumWebBrowser control to the form
this.Controls.Add(chromeBrowser);
chromeBrowser.Dock = DockStyle.Fill; // Fill the entire form
// Handle when the browser component has finished loading
chromeBrowser.LoadingStateChanged += ChromeBrowser_LoadingStateChanged;
}
private void ChromeBrowser_LoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
{
if (!e.IsLoading)
{
// Page has finished loading
// Perform actions after the page has loaded
Console.WriteLine("Finished loading.");
}
}
// Dispose of Cef resources when the form is closed
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
Cef.Shutdown();
}
}
}Imports System
Imports System.Windows.Forms
Imports CefSharp
Imports CefSharp.WinForms
Namespace CefSharpExample
Partial Public Class MainForm
Inherits Form
Private chromeBrowser As ChromiumWebBrowser
Public Sub New()
InitializeComponent()
' Initialize CefSharp settings
Dim settings As New CefSettings()
Cef.Initialize(settings)
' Create the ChromiumWebBrowser instance
chromeBrowser = New ChromiumWebBrowser("https://ironpdf.com/") ' Load a URL
' Add the ChromiumWebBrowser control to the form
Me.Controls.Add(chromeBrowser)
chromeBrowser.Dock = DockStyle.Fill ' Fill the entire form
' Handle when the browser component has finished loading
AddHandler chromeBrowser.LoadingStateChanged, AddressOf ChromeBrowser_LoadingStateChanged
End Sub
Private Sub ChromeBrowser_LoadingStateChanged(ByVal sender As Object, ByVal e As LoadingStateChangedEventArgs)
If Not e.IsLoading Then
' Page has finished loading
' Perform actions after the page has loaded
Console.WriteLine("Finished loading.")
End If
End Sub
' Dispose of Cef resources when the form is closed
Private Sub MainForm_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
Cef.Shutdown()
End Sub
End Class
End Namespace这个代码使用CefSharp来开发一个基于Chromium的嵌入式Web浏览器的基本WinForms应用程序。 这是解释:
- 使用
CefSettings初始化CefSharp设置。 - ChromiumWebBrowser: 这个类代表CefSharp提供的Web浏览器控件。
LoadingStateChanged事件处理浏览器的加载状态变化,例如页面加载完成时。MainForm_FormClosing事件确保在表单关闭时正确关闭Cef资源。
对于一个基本示例,使用CefSharp构建并准备编译的应用程序,请参见GitHub上的Minimal示例项目。 项目的源代码中提供了更复杂的示例项目。
CefSharp与IronPDF
将IronPDF的PDF生成功能与CefSharp的Chromium嵌入式框架(CEF)浏览器集成在.NET应用程序中需要同时使用CefSharp和IronPDF。 然而,到2022年1月的最新版本为止,在CefSharp和IronPDF之间没有直接的开箱即用接口。
CefSharp的主要目标是将Chromium Web浏览器引擎集成到.NET程序中,通过应用程序的用户界面显示和交互在线内容。 CefSharp为WPF和Windows窗体应用程序提供浏览器控件。
虽然CefSharp和IronPDF没有直接集成,但开发者仍然可以在同一应用程序环境中使用这两个库。
IronPDF在HTML到PDF转换方面表现出色,确保精确保留原始布局和样式。 它非常适合从基于Web的内容中创建PDF,如报告、发票和文档。 利用对HTML文件、URL和原始HTML字符串的支持,IronPDF轻松生成高质量的PDF文档。
using IronPdf;
class Program
{
static void Main(string[] args)
{
var renderer = new ChromePdfRenderer();
// 1. Convert HTML String to PDF
var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>";
var pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent);
pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf");
// 2. Convert HTML File to PDF
var htmlFilePath = "path_to_your_html_file.html"; // Specify the path to your HTML file
var pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath);
pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf");
// 3. Convert URL to PDF
var url = "http://ironpdf.com"; // Specify the URL
var pdfFromUrl = renderer.RenderUrlAsPdf(url);
pdfFromUrl.SaveAs("URLToPDF.pdf");
}
}using IronPdf;
class Program
{
static void Main(string[] args)
{
var renderer = new ChromePdfRenderer();
// 1. Convert HTML String to PDF
var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>";
var pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent);
pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf");
// 2. Convert HTML File to PDF
var htmlFilePath = "path_to_your_html_file.html"; // Specify the path to your HTML file
var pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath);
pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf");
// 3. Convert URL to PDF
var url = "http://ironpdf.com"; // Specify the URL
var pdfFromUrl = renderer.RenderUrlAsPdf(url);
pdfFromUrl.SaveAs("URLToPDF.pdf");
}
}Imports IronPdf
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim renderer = New ChromePdfRenderer()
' 1. Convert HTML String to PDF
Dim htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>"
Dim pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent)
pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf")
' 2. Convert HTML File to PDF
Dim htmlFilePath = "path_to_your_html_file.html" ' Specify the path to your HTML file
Dim pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath)
pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf")
' 3. Convert URL to PDF
Dim url = "http://ironpdf.com" ' Specify the URL
Dim pdfFromUrl = renderer.RenderUrlAsPdf(url)
pdfFromUrl.SaveAs("URLToPDF.pdf")
End Sub
End Class安装IronPDF
要获取IronPDF库,你需要执行即将到来的步骤。 在包管理器中输入以下代码:
Install-Package IronPdf
dotnet add package IronPdfInstall-Package IronPdf
dotnet add package IronPdf
或者,你可以使用NuGet包管理器搜索包"IronPDF"。 从与IronPDF相关的所有NuGet包列表中,选择并下载所需的包。

在CefSharp C#中使用IronPDF
要在C#应用程序中将IronPDF与CefSharp集成,需要执行两步:利用CefSharp提供的基于Chromium的浏览器呈现HTML内容,然后使用IronPDF将该HTML信息转换为PDF文档。 以下示例演示了如何完成这种集成:
using CefSharp;
using IronPdf;
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CefSharpIronPdfIntegration
{
public partial class MainForm : Form
{
private ChromiumWebBrowser chromeBrowser;
public MainForm()
{
InitializeComponent();
// Initialize CefSharp
CefSettings settings = new CefSettings();
Cef.Initialize(settings);
// Create the ChromiumWebBrowser instance
chromeBrowser = new ChromiumWebBrowser("https://ironpdf.com/"); // Load a URL
// Add the ChromiumWebBrowser control to the form
this.Controls.Add(chromeBrowser);
chromeBrowser.Dock = DockStyle.Fill; // Fill the entire form
// Handle when the browser component has finished loading
chromeBrowser.LoadingStateChanged += ChromeBrowser_LoadingStateChanged;
}
private async void ChromeBrowser_LoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
{
if (!e.IsLoading)
{
// Page has finished loading
// Capture HTML content after page load completes
string htmlContent = await chromeBrowser.GetSourceAsync();
// Use IronPDF to generate a PDF from the captured HTML content
var Renderer = new IronPdf.HtmlToPdf();
var PDF = Renderer.RenderHtmlAsPdf(htmlContent);
PDF.SaveAs("Output.pdf"); // Save the generated PDF
Console.WriteLine("PDF generated successfully.");
}
}
// Dispose of Cef resources when the form is closed to avoid unnecessary memory usage
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
Cef.Shutdown();
}
}
}using CefSharp;
using IronPdf;
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CefSharpIronPdfIntegration
{
public partial class MainForm : Form
{
private ChromiumWebBrowser chromeBrowser;
public MainForm()
{
InitializeComponent();
// Initialize CefSharp
CefSettings settings = new CefSettings();
Cef.Initialize(settings);
// Create the ChromiumWebBrowser instance
chromeBrowser = new ChromiumWebBrowser("https://ironpdf.com/"); // Load a URL
// Add the ChromiumWebBrowser control to the form
this.Controls.Add(chromeBrowser);
chromeBrowser.Dock = DockStyle.Fill; // Fill the entire form
// Handle when the browser component has finished loading
chromeBrowser.LoadingStateChanged += ChromeBrowser_LoadingStateChanged;
}
private async void ChromeBrowser_LoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
{
if (!e.IsLoading)
{
// Page has finished loading
// Capture HTML content after page load completes
string htmlContent = await chromeBrowser.GetSourceAsync();
// Use IronPDF to generate a PDF from the captured HTML content
var Renderer = new IronPdf.HtmlToPdf();
var PDF = Renderer.RenderHtmlAsPdf(htmlContent);
PDF.SaveAs("Output.pdf"); // Save the generated PDF
Console.WriteLine("PDF generated successfully.");
}
}
// Dispose of Cef resources when the form is closed to avoid unnecessary memory usage
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
Cef.Shutdown();
}
}
}Imports CefSharp
Imports IronPdf
Imports System
Imports System.Threading.Tasks
Imports System.Windows.Forms
Namespace CefSharpIronPdfIntegration
Partial Public Class MainForm
Inherits Form
Private chromeBrowser As ChromiumWebBrowser
Public Sub New()
InitializeComponent()
' Initialize CefSharp
Dim settings As New CefSettings()
Cef.Initialize(settings)
' Create the ChromiumWebBrowser instance
chromeBrowser = New ChromiumWebBrowser("https://ironpdf.com/") ' Load a URL
' Add the ChromiumWebBrowser control to the form
Me.Controls.Add(chromeBrowser)
chromeBrowser.Dock = DockStyle.Fill ' Fill the entire form
' Handle when the browser component has finished loading
AddHandler chromeBrowser.LoadingStateChanged, AddressOf ChromeBrowser_LoadingStateChanged
End Sub
Private Async Sub ChromeBrowser_LoadingStateChanged(ByVal sender As Object, ByVal e As LoadingStateChangedEventArgs)
If Not e.IsLoading Then
' Page has finished loading
' Capture HTML content after page load completes
Dim htmlContent As String = Await chromeBrowser.GetSourceAsync()
' Use IronPDF to generate a PDF from the captured HTML content
Dim Renderer = New IronPdf.HtmlToPdf()
Dim PDF = Renderer.RenderHtmlAsPdf(htmlContent)
PDF.SaveAs("Output.pdf") ' Save the generated PDF
Console.WriteLine("PDF generated successfully.")
End If
End Sub
' Dispose of Cef resources when the form is closed to avoid unnecessary memory usage
Private Sub MainForm_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
Cef.Shutdown()
End Sub
End Class
End Namespace此代码展示了一个简单的集成,其中在加载的Chromium浏览器中拦截HTML内容,并使用IronPDF将其转换为PDF文档。 这是解释:
- 初始化CefSharp以将Chromium浏览器集成到Windows窗体应用程序中。
ChromiumWebBrowser控件: 该控件在浏览器中加载特定URL(在本例中为"https://ironpdf.com/")并创建其实例。LoadingStateChanged事件: 跟踪浏览器的加载进度。 当页面完成加载时(e.IsLoading为假),Chrome浏览器会异步捕获已加载页面的HTML内容,使用GetSourceAsync()。- IronPDF集成: 捕获的HTML内容(
htmlContent)使用IronPDF的HtmlToPdf能力转换为PDF文档。 生成的PDF被保存为"Output.pdf"。 - 表单关闭: 确保Cef资源在表单关闭后通过调用
Cef.Shutdown()正确关闭。

此代码展示了一个简单的集成,其中在加载的Chromium浏览器中拦截HTML内容,然后使用IronPDF将其转换为PDF文档。 根据你的应用程序的具体需求自定义URL、错误处理和其他方面。 有关IronPDF文档的详细信息,请参考IronPDF NuGet包。
结论
在C#应用程序中与CefSharp集成IronPDF为管理文档和在线信息打开了新的可能性。 结合IronPDF的PDF生成功能与CefSharp提供的基于Chromium的浏览器,开发者可以创建灵活的应用程序,而同时结合动态Web内容,生成高质量的PDF文档。
IronPDF的Lite包价格为$799,包含一年的软件维护合同、升级选项、永久许可证和三十天退款保证。 在三十天试用期内,用户可以通过带水印的试用版在实际场景中评估该产品。 要了解更多关于IronPDF的成本、许可和免费版本的信息,请访问IronPDF许可信息。 欲了解更多关于Iron Software的信息,请访问其Iron Software网站。
常见问题解答
如何将基于Chromium的Web浏览器集成到我的.NET桌面应用程序中?
您可以使用CefSharp,一个围绕Chromium嵌入式框架的.NET封装,将基于Chromium的Web浏览器轻松集成到您的.NET桌面应用程序中。这使您能够利用HTML5、CSS3和JavaScript等现代Web技术。
在WPF应用程序中设置CefSharp涉及哪些步骤?
要在WPF应用程序中设置CefSharp,请从Visual Studio中创建一个新项目开始。使用NuGet包管理器安装CefSharp.Wpf包,然后配置必要的设置以在您的应用程序中嵌入Chromium浏览器。
如何将Chromium浏览器中渲染的HTML内容转换为PDF文档?
要将Chromium浏览器中渲染的HTML内容转换为PDF,您可以先使用CefSharp渲染HTML,然后使用IronPDF的HtmlToPdf功能生成PDF文档。
CefSharp能与.NET应用中的PDF生成工具一起使用吗?
是的,CefSharp可以与IronPDF等PDF生成工具一起在.NET应用程序中使用。虽然没有直接集成,但您可以利用两者来渲染HTML内容并将其转换为PDF。
将CefSharp集成到.NET应用程序中有什么好处?
将CefSharp集成到.NET应用程序中提供了诸如能够直接在桌面应用程序中显示和交互现代Web内容的好处。它还允许开发人员自定义浏览器行为并使用HTML5、CSS3和JavaScript等Web技术。
在将CefSharp与.NET应用程序集成时,如何解决问题?
为解决CefSharp集成问题,请确保所有NuGet包已正确安装,检查与.NET版本的兼容性,并验证Visual Studio项目中的配置设置。
在.NET应用程序中是否可以自定义Chromium浏览器的行为?
是的,CefSharp允许开发人员自定义集成到.NET应用程序中的Chromium浏览器的行为,提供对浏览器设置的控制,并能够通过自定义代码扩展功能。
使用CefSharp和IronPDF的项目有什么好处?
需要显示动态Web内容并直接从该内容生成PDF报告或文档的项目受益于使用CefSharp和IronPDF。这种组合非常适合需要强大文件管理和Web内容渲染功能的应用程序。








