empira / PDFsharp-1.5

A .NET library for processing PDF
MIT License
1.28k stars 588 forks source link

Unable to run Hello World example - Exception creating font #157

Closed SoftCircuits closed 1 year ago

SoftCircuits commented 2 years ago

Expected Behavior

I expected the simple Hello World example to run.

PdfDocument document = new();

PdfPage page = document.AddPage();

XGraphics gfx = XGraphics.FromPdfPage(page);

XFont font = new("Verdana", 20, XFontStyle.Bold);

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

Actual Behavior

I don't seem to be able to create the font. On the line that creates an XFont, I got a compile error.

The type 'FontFamily' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Drawing.Common, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

I was able to reference this assembly and run the program. Then I got a run-time exception on the same line.

System.NotSupportedException: 'No data is available for encoding 1252. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.'

Steps to Reproduce the Behavior

Just tried to run the basic Hello World example. When it gets to the line that creates the font, I get the exception above.

I really don't understand the error or why, apparently, the author of the example code didn't have to deal with this issue. I just want to print two words on the page. What encoding does this refer to?

Note that I'm using .NET 6.0. I'm having trouble seeing which versions the library targets. Usually, under the Dependencies section on NuGet, it shows the target libraries. This one just says This package has no dependencies.

UPDATE:

And BTW, when I download the PDF associated with this example, I get a blank page.

TH-Soft commented 2 years ago

Please try either the "PDFsharp-MigraDoc-GDI" or the "PDFsharp-MigraDoc-WPF" NuGet packages published by PDFsharp-Team.

SoftCircuits commented 2 years ago

@TH-Soft Can you elaborate why? Do you know if PDFsharp supports .NET 6.0? I seem to be finding that it does not.

bairog commented 2 years ago

System.NotSupportedException: 'No data is available for encoding 1252. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.'

@SoftCircuits .Net Core 3.x/.NET5/6 doesn't know about any non-Unicode encoding - https://stackoverflow.com/a/37870346 Add reference to System.Text.Encoding.CodePages nuget and add the following line before using any non-Unicode encoding: Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); Does this solve the problem for you?

ThomasHoevel commented 1 year ago

PDFsharp 6.0.0 now uses .NET 6.