首页 > 解决方案 > 为什么我收到错误 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 代码,但我不知道这是否与它有关。

标签: c#visual-studio-codeopenhtmltopdf

解决方案


您可以使用此命令dotnet add package OpenHtmlToPdf添加对您项目的引用。这将在您的 .csproj 文件中添加一个包引用。完成后运行此命令dotnet restore以恢复您的软件包。

供您参考:dotnet add package

编辑:您可以添加Nuget 包管理器扩展。添加后,执行以下步骤:

  1. 按 Ctrl+Shift+P
  2. 类型 OpenHtmlToPdf
  3. 选择包,然后选择版本号。

推荐阅读