Skip to content

Commit a4c789a

Browse files
committed
1033811: Added one class and proved code sample.
1 parent 4b53e9a commit a4c789a

2 files changed

Lines changed: 86 additions & 66 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
}
Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
using Microsoft.AspNetCore.Mvc;
2-
using Syncfusion.Drawing;
3-
using Syncfusion.Pdf;
4-
using Syncfusion.Pdf.Graphics;
5-
using Syncfusion.Pdf.Grid;
62

73
namespace Web_API_Project.Controllers
84
{
@@ -33,67 +29,5 @@ public IEnumerable<WeatherForecast> Get()
3329
})
3430
.ToArray();
3531
}
36-
37-
[HttpGet("/api/Pdf")]
38-
public IActionResult CreatePdfDocument()
39-
{
40-
try
41-
{
42-
const string fileDownloadName = "Output.pdf";
43-
const string contentType = "application/pdf";
44-
var stream = ExportWeatherForecastToPdf();
45-
stream.Position = 0;
46-
return File(stream, contentType, fileDownloadName);
47-
}
48-
catch (Exception ex)
49-
{
50-
return BadRequest($"Error occurred while creating PDF file: {ex.Message}");
51-
}
52-
}
53-
54-
private MemoryStream ExportWeatherForecastToPdf()
55-
{
56-
var forecasts = Get().ToList();
57-
58-
//Create a new PDF document.
59-
using (PdfDocument pdfDocument = new PdfDocument())
60-
{
61-
int paragraphAfterSpacing = 8;
62-
int cellMargin = 8;
63-
//Add page to the PDF document.
64-
PdfPage page = pdfDocument.Pages.Add();
65-
//Create a new font.
66-
PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 16);
67-
//Create a text element to draw a text in PDF page.
68-
PdfTextElement title = new PdfTextElement("Weather Forecast", font, PdfBrushes.Black);
69-
PdfLayoutResult result = title.Draw(page, new PointF(0, 0));
70-
PdfStandardFont contentFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 12);
71-
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);
72-
PdfLayoutFormat format = new PdfLayoutFormat
73-
{
74-
Layout = PdfLayoutType.Paginate
75-
};
76-
//Draw a text to the PDF document.
77-
result = content.Draw(page, new RectangleF(0, result.Bounds.Bottom + paragraphAfterSpacing, page.GetClientSize().Width, page.GetClientSize().Height), format);
78-
//Create a PdfGrid.
79-
PdfGrid pdfGrid = new PdfGrid();
80-
pdfGrid.Style.CellPadding.Left = cellMargin;
81-
pdfGrid.Style.CellPadding.Right = cellMargin;
82-
//Applying built-in style to the PDF grid.
83-
pdfGrid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable4Accent1);
84-
//Assign data source.
85-
pdfGrid.DataSource = forecasts;
86-
pdfGrid.Style.Font = contentFont;
87-
//Draw PDF grid into the PDF page.
88-
pdfGrid.Draw(page, new PointF(0, result.Bounds.Bottom + paragraphAfterSpacing));
89-
90-
using (MemoryStream stream = new MemoryStream())
91-
{
92-
//Saving the PDF document into the stream.
93-
pdfDocument.Save(stream);
94-
return new MemoryStream(stream.ToArray());
95-
}
96-
}
97-
}
9832
}
9933
}

0 commit comments

Comments
 (0)