/// <summary> /// Ensure the font resolver is only applied once (or an exception is thrown) /// </summary> internal static void Apply() { if (OurGlobalFontResolver == null || GlobalFontSettings.FontResolver == null) { if (OurGlobalFontResolver == null) { OurGlobalFontResolver = new MyFontResolver(); } GlobalFontSettings.FontResolver = OurGlobalFontResolver; } }
public IActionResult RenderPdfDocument(long clientId) { Client client = _db.Clients.FirstOrDefault(c => c.Id == clientId && c.Id == clientId); MyFontResolver.Apply(); PdfDocument document = new PdfDocument(); PdfPage page = document.AddPage(); XGraphics gfx = XGraphics.FromPdfPage(page); double x = 50, y = 100; XFont fontH1 = new XFont("Arial", 18, XFontStyle.Bold); XFont font = new XFont("Arial", 11, XFontStyle.Bold); XFont fontRegular = new XFont("Arial", 11); double ls = 10; gfx.DrawString("", fontH1, XBrushes.Black, x, x); if (client != null) { gfx.DrawString($"Я, {client.NameSurname},", fontRegular, XBrushes.Black, x, y); y += 20 + 2 * ls; gfx.DrawString("настоящим подтверждаю, что с Правилами посещения и условиями абонемента йога-центра", font, XBrushes.Black, x, y); y += ls; gfx.DrawString("Classical Yoga Ashram ознакомлен и согласен. В дальнейшем иметь претензий не буду.", font, XBrushes.Black, x, y); y += 20 + 2 * ls; gfx.DrawString("Информация о практикующем:", fontH1, XBrushes.Black, x += 115, y); y += 20 + 2 * ls; if (client.WorkPlace != null) { gfx.DrawString($"Место работы и должность: {client.WorkPlace}", fontRegular, XBrushes.Black, x -= 110, y); } else { gfx.DrawString($"", fontRegular, XBrushes.Black, x -= 110, y); } y += 5 + 2 * ls; gfx.DrawString($"Моб. тел.: {client.PhoneNumber}", fontRegular, XBrushes.Black, x, y); y += 5 + 2 * ls; gfx.DrawString($"Дата рождения: {client.DateOfBirth.ToString("dd MMMM yyyy")} г.", fontRegular, XBrushes.Black, x, y); y += 5 + 2 * ls; gfx.DrawString($"E-mail: {client.Email}", fontRegular, XBrushes.Black, x, y); y += 5 + 2 * ls; gfx.DrawString($"Наличие заболеваний: {client.Sickness.Name}", fontRegular, XBrushes.Black, x, y); y += 20 + 2 * ls; gfx.DrawString($"Дата: {DateTime.Now:dd.MM.yyyy} г.", font, XBrushes.Black, x, y); gfx.DrawString("Подпись: ______________", font, XBrushes.Black, x += 300, y); } else { y += 20 + 2 * ls; gfx.DrawString($"Нет никакого клиента", fontRegular, XBrushes.Black, x, y); } MemoryStream stream = new MemoryStream(); document.Save(stream); stream.Position = 0; FileStreamResult fileStreamResult = new FileStreamResult(stream, "application/pdf"); fileStreamResult.FileDownloadName = $"{client.NameSurname}.pdf"; return(fileStreamResult); // string filename = $"Files/contract.pdf"; // document.Save(filename); // // var fileStream = new FileStream(filename, // FileMode.Open, // FileAccess.Read // ); // var fsResult = new FileStreamResult(fileStream, "application/pdf"); // return fsResult; }
static PdfPageExtensions() { MyFontResolver.Apply(); }