Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDFsharp: Reference to type 'FontFamily' claims it is defined in 'System.Drawing', but it could be found

I'm using PDFsharp 1.50rc2 in my web application (ASP.NET Core 2) to generate PDF. I already follow the example on the PDFsharp site. But got problem to generate it, probably related to the FontFamily in the System.Drawing. Not sure. Below is my code to generate the 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);
    }
  }
}

Error

Reference to type 'FontFamily' claims it is defined in 'System.Drawing', but it could be found

like image 519
saf21 Avatar asked Nov 26 '22 01:11

saf21


1 Answers

You are using a version of PDFsharp that relies on GDI+ and your project may have to reference System.Drawing e.g. when you need fonts, pens, brushes, and such.

To avoid references to GDI+, you can use the WPF build of PDFsharp:
https://www.nuget.org/packages/PDFsharp-wpf/1.50.4845-RC2a

When using the WPF build you also have to add references to WPF when it comes to using fonts, pens, brushes, &c.
You need a reference to PresentationCore for the WPF build.

PDFsharp 1.50 was not designed to work with .NET Core and may not work on other platforms than Windows.

like image 165
I liked the old Stack Overflow Avatar answered Dec 05 '22 20:12

I liked the old Stack Overflow