|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using Microsoft.AspNetCore.Mvc; |
| 6 | +using Syncfusion.Drawing; |
| 7 | +using Syncfusion.Pdf; |
| 8 | +using Syncfusion.Pdf.Graphics; |
| 9 | +using Syncfusion.Pdf.Grid; |
| 10 | + |
| 11 | +namespace Web_API_Project.Controllers |
| 12 | +{ |
| 13 | + [ApiController] |
| 14 | + [Route("[controller]")] |
| 15 | + public class PdfController : ControllerBase |
| 16 | + { |
| 17 | + private static readonly string[] Summaries = new[] |
| 18 | + { |
| 19 | + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" |
| 20 | + }; |
| 21 | + |
| 22 | + private readonly ILogger<PdfController> _logger; |
| 23 | + |
| 24 | + public PdfController(ILogger<PdfController> logger) |
| 25 | + { |
| 26 | + _logger = logger; |
| 27 | + } |
| 28 | + |
| 29 | + [HttpGet("/api/Pdf")] |
| 30 | + public IActionResult CreatePdfDocument() |
| 31 | + { |
| 32 | + try |
| 33 | + { |
| 34 | + const string fileDownloadName = "Output.pdf"; |
| 35 | + const string contentType = "application/pdf"; |
| 36 | + var stream = ExportWeatherForecastToPdf(); |
| 37 | + stream.Position = 0; |
| 38 | + return File(stream, contentType, fileDownloadName); |
| 39 | + } |
| 40 | + catch (Exception ex) |
| 41 | + { |
| 42 | + return BadRequest($"Error occurred while creating PDF file: {ex.Message}"); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private MemoryStream ExportWeatherForecastToPdf() |
| 47 | + { |
| 48 | + var forecasts = Enumerable.Range(1, 5).Select(index => new WeatherForecast |
| 49 | + { |
| 50 | + Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), |
| 51 | + TemperatureC = Random.Shared.Next(-20, 55), |
| 52 | + Summary = Summaries[Random.Shared.Next(Summaries.Length)] |
| 53 | + }).ToList(); |
| 54 | + |
| 55 | + using (PdfDocument pdfDocument = new PdfDocument()) |
| 56 | + { |
| 57 | + int paragraphAfterSpacing = 8; |
| 58 | + int cellMargin = 8; |
| 59 | + PdfPage page = pdfDocument.Pages.Add(); |
| 60 | + PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 16); |
| 61 | + PdfTextElement title = new PdfTextElement("Weather Forecast", font, PdfBrushes.Black); |
| 62 | + PdfLayoutResult result = title.Draw(page, new PointF(0, 0)); |
| 63 | + PdfStandardFont contentFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 12); |
| 64 | + PdfTextElement content = new PdfTextElement("This component demonstrates fetching data from a service and exporting the data to a PDF document using Syncfusion .NET PDF library.", contentFont, PdfBrushes.Black); |
| 65 | + PdfLayoutFormat format = new PdfLayoutFormat |
| 66 | + { |
| 67 | + Layout = PdfLayoutType.Paginate |
| 68 | + }; |
| 69 | + result = content.Draw(page, new RectangleF(0, result.Bounds.Bottom + paragraphAfterSpacing, page.GetClientSize().Width, page.GetClientSize().Height), format); |
| 70 | + PdfGrid pdfGrid = new PdfGrid(); |
| 71 | + pdfGrid.Style.CellPadding.Left = cellMargin; |
| 72 | + pdfGrid.Style.CellPadding.Right = cellMargin; |
| 73 | + pdfGrid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable4Accent1); |
| 74 | + pdfGrid.DataSource = forecasts; |
| 75 | + pdfGrid.Style.Font = contentFont; |
| 76 | + pdfGrid.Draw(page, new PointF(0, result.Bounds.Bottom + paragraphAfterSpacing)); |
| 77 | + |
| 78 | + using (MemoryStream stream = new MemoryStream()) |
| 79 | + { |
| 80 | + pdfDocument.Save(stream); |
| 81 | + return new MemoryStream(stream.ToArray()); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments