PDFsharp:对类型“FontFamily”的引用声称它是在“System.Drawing”中定义的,但可以找到

saf*_*f21 5 pdf pdf-generation pdfsharp asp.net-core

我在我的 Web 应用程序(ASP.NET Core 2)中使用PDFsharp 1.50rc2来生成 PDF。我已经按照PDFsharp 站点上的示例进行操作。但是生成它时遇到了问题,可能与 System.Drawing 中的 FontFamily 有关。没有把握。下面是我生成PDF的代码。

using PdfSharp.Pdf;
using PdfSharp.Drawing;

namespace WebApp.Controllers
{
  public class HomeController : Controller {
    public IActionResult Index()
    {
      // creating the PDF
      CreatePDF();

      return View();
    }

    public void CreatePDF()
    {
      // Create a new PDF document
      PdfDocument document = new PdfDocument();
      document.Info.Title = "Created with PDFsharp";

      // Create an empty page
      PdfPage page = document.AddPage();

      // Get an XGraphics object for drawing
      XGraphics gfx = XGraphics.FromPdfPage(page);

      // Create a font
      // PROBLEM IN HERE (new XFONT)
      XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic);

      // Draw the text
      gfx.DrawString("Hello, World!", font, XBrushes.Black,
      new XRect(0, 0, page.Width, page.Height),
      XStringFormats.Center);

      // Save the document...
      const string filename = "HelloWorld.pdf";
      document.Save(filename);
      // ...and start a viewer.
      Process.Start(filename);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

错误

对类型“FontFamily”的引用声称它是在“System.Drawing”中定义的,但可以找到

Je *_*not 4

您正在使用依赖于 GDI+ 的 PDFsharp 版本,并且您的项目可能必须引用,System.Drawing例如,当您需要字体、笔、画笔等时。

为了避免引用 GDI+,您可以使用 PDFsharp 的 WPF 版本: https:
//www.nuget.org/packages/PDFsharp-wpf/1.50.4845-RC2a

使用 WPF 构建时,在使用字体、笔、画笔等时,您还必须添加对 WPF 的引用。您需要WPF 构建的
参考。PresentationCore

PDFsharp 1.50 并非设计用于与 .NET Core 配合使用,并且可能无法在 Windows 以外的其他平台上运行。