c# - 为什么我收到错误 CS0246:尝试使用 OpenHtmlToPdf 时找不到类型或命名空间名称?
问题描述
我正在尝试使用 OpenHtmlToPdf 将 Html 文件转换为 pdf。这是我试图运行的代码:
using System.IO;
using OpenHtmlToPdf;
namespace Converter{
class Html_to_Pdf
{
static void Main(string[] args){
var html = Pdf.From(@"C:/Temp/input.html").OfSize(PaperSize.A4);
byte[] content = html.Content();
File.WriteAllBytes(@"C:/Temp/output.pdf", content);
}
}
}
这是我尝试编译代码时遇到的错误:
[Running] dotnet "C:\Users\janik\AppData\Roaming\Code\User\cs-script.user\dotnet\cscs.dll" "d:\Github\ionesoft_ebook_to_pdf\Scripts\converter.cs"
Error: Specified file could not be compiled.
d:\Github\ionesoft_ebook_to_pdf\Scripts\converter.cs(2,7): error CS0246: The type or namespace name "OpenHtmlToPdf" could not be found (are you missing a using directive or an assembly reference?)
d:\Github\ionesoft_ebook_to_pdf\Scripts\converter.cs(2,7): error CS0246: The type or namespace name "OpenHtmlToPdf" could not be found (are you missing a using directive or an assembly reference?)
[Done] exited with code=0 in 2.183 seconds
那么我在这里做错了什么,因为根据我的说法,我什么都没有。我正在使用 Visual Studio 代码,但我不知道这是否与它有关。
解决方案
您可以使用此命令dotnet add package OpenHtmlToPdf
添加对您项目的引用。这将在您的 .csproj 文件中添加一个包引用。完成后运行此命令dotnet restore
以恢复您的软件包。
供您参考:dotnet add package
编辑:您可以添加Nuget 包管理器扩展。添加后,执行以下步骤:
- 按 Ctrl+Shift+P
- 类型 OpenHtmlToPdf
- 选择包,然后选择版本号。
推荐阅读
- javascript - 如何将javascript数组发送到php
- asp.net - 如何在没有客户端交互的情况下在 ASP.NET SERVER(或任何)上运行代码?
- javascript - 如何在博客中搜索特定标签内的帖子?
- vba - 合并 Excel 工作簿,但需要所有工作表,而不仅仅是第一个
- php - 将字符串从字符长度存储到数组中
- azure-cognitive-search - 为 Azure 搜索索引更新/添加/删除字段的最佳做法
- android - 错误:任务“:app:processDebugManifest”执行失败。添加新活动后
- ruby-on-rails - 无法让 rails 5.1.5 通过 capybara 提交具体化 css 下拉值
- openmdao - 外部代码与元数据不兼容?
- python - Pythonanywhere 上的 Flask 应用程序 - FileNotFoundError: [Errno 2] No such file or directory