empira / PDFsharp.Samples

PDFsharp 6.0 and MigraDoc Foundation samples
https://docs.pdfsharp.net/PDFsharp/Samples/About.html
Other
65 stars 13 forks source link

Hello world does not work #6

Closed APerricone closed 10 months ago

APerricone commented 10 months ago

Hi, I copied the code of hello world:

// See https://aka.ms/new-console-template for more information
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using System.Diagnostics;

// Create a new PDF document.
var document = new PdfDocument();
document.Info.Title = "Created with PDFsharp";
document.Info.Subject = "Just a simple Hello-World program.";

// Create an empty page in this document.
var page = document.AddPage();

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

// Draw two lines with a red default pen.
var width = page.Width;
var height = page.Height;
gfx.DrawLine( XPens.Red, 0, 0, width, height );
gfx.DrawLine( XPens.Red, width, 0, 0, height );

// Draw a circle with a red pen which is 1.5 point thick.
var r = width / 5;
gfx.DrawEllipse( new XPen( XColors.Red, 1.5 ), XBrushes.White, new XRect( width / 2 - r, height / 2 - r, 2 * r, 2 * r ) );

// Create a font.
var font = new XFont("Times New Roman", 20, XFontStyleEx.BoldItalic);

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

// Save the document...
var filename = "HelloWorld_tempfile.pdf";
document.Save( filename );
// ...and start a viewer.
Process.Start( new ProcessStartInfo( filename ) { UseShellExecute = true } );

and get

Exception thrown: 'System.InvalidOperationException' in PdfSharp.dll
An unhandled exception of type 'System.InvalidOperationException' occurred in PdfSharp.dll
No appropriate font found for family name "Times New Roman". Implement IFontResolver and assign to "GlobalFontSettings.FontResolver" to use fonts.

Is it normal?

TH-Soft commented 10 months ago

It is normal.

Which part of "Implement IFontResolver and assign to "GlobalFontSettings.FontResolver" to use fonts." you do not understand?

Try the GDI or WPF builds of PDFsharp to get it working without own FontResolver.

Full sample code here: https://github.com/empira/PDFsharp.Samples/blob/master/src/samples/src/PDFsharp/src/HelloWorld/Program.cs

Do not omit the important lines 12 and 13: if (Capabilities.Build.IsCoreBuild) GlobalFontSettings.FontResolver = new FailsafeFontResolver();

APerricone commented 10 months ago

Which part of "Implement IFontResolver and assign to "GlobalFontSettings.FontResolver" to use fonts." you do not understand? Everything... why is it not included? Try the GDI or WPF builds of PDFsharp to get it working without own FontResolver. I am creating a pdf without graphic interface

Full sample code here: https://github.com/empira/PDFsharp.Samples/blob/master/src/samples/src/PDFsharp/src/HelloWorld/Program.cs

Do not omit the important lines 12 and 13: if (Capabilities.Build.IsCoreBuild) GlobalFontSettings.FontResolver = new FailsafeFontResolver();

looks I forget those lines.. thanks

ThomasHoevel commented 10 months ago

Everything... why is it not included? It is included. The PDFsharp Core build can be used under Windows, MacOS, Linux, ... and we include FontResolvers that are suitable for the samples. Your app must include FontResolvers that are suitable for your app. AFAIK there are no TrueType fonts that are always installed under all Linux distributions.

Try the GDI or WPF builds of PDFsharp to get it working without own FontResolver.

I am creating a pdf without graphic interface If your code runs under Windows only, you can still use the PDFsharp builds made for Windows that will automatically use all the fonts installed under Windows. This makes life much easier. If you take the version for Windows, MacOS, and Linux, then include the fonts you need.