PDFsharp:对类型 'FontFamily' 的引用声称它在 'System.Drawing' 中定义,但可以找到
PDFsharp: Reference to type 'FontFamily' claims it is defined in 'System.Drawing', but it could be found
我在我的网络应用程序 (ASP.NET Core 2) 中使用 PDFsharp 1.50rc2 来生成 PDF。
我已经关注 PDFsharp 网站上的 example。但是生成它时遇到问题,可能与 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);
}
}
}
错误
Reference to type 'FontFamily' claims it is defined in
'System.Drawing', but it could be found
您正在使用依赖于 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 以外的其他平台上工作。
我找到了 XFont 的第二个参数 class
需要是 float 类型而不是 int
// force a float value
let font1 = new XFont("arial", 12.0, XFontStyle.Bold)
更正编译器错误后消失
我在我的网络应用程序 (ASP.NET Core 2) 中使用 PDFsharp 1.50rc2 来生成 PDF。 我已经关注 PDFsharp 网站上的 example。但是生成它时遇到问题,可能与 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);
}
}
}
错误
Reference to type 'FontFamily' claims it is defined in 'System.Drawing', but it could be found
您正在使用依赖于 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 以外的其他平台上工作。
我找到了 XFont 的第二个参数 class 需要是 float 类型而不是 int
// force a float value
let font1 = new XFont("arial", 12.0, XFontStyle.Bold)
更正编译器错误后消失