Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Grid;

namespace Web_API_Project.Controllers
{
[ApiController]
[Route("[controller]")]
public class PdfController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

private readonly ILogger<PdfController> _logger;

public PdfController(ILogger<PdfController> logger)
{
_logger = logger;
}

[HttpGet("/api/Pdf")]
public IActionResult CreatePdfDocument()
{
try
{
const string fileDownloadName = "Output.pdf";
const string contentType = "application/pdf";
var stream = ExportWeatherForecastToPdf();
stream.Position = 0;
return File(stream, contentType, fileDownloadName);
}
catch (Exception ex)
{
return BadRequest($"Error occurred while creating PDF file: {ex.Message}");
}
}

private MemoryStream ExportWeatherForecastToPdf()
{
var forecasts = Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
}).ToList();

using (PdfDocument pdfDocument = new PdfDocument())
{
int paragraphAfterSpacing = 8;
int cellMargin = 8;
PdfPage page = pdfDocument.Pages.Add();
PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 16);
PdfTextElement title = new PdfTextElement("Weather Forecast", font, PdfBrushes.Black);
PdfLayoutResult result = title.Draw(page, new PointF(0, 0));
PdfStandardFont contentFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 12);
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);
PdfLayoutFormat format = new PdfLayoutFormat
{
Layout = PdfLayoutType.Paginate
};
result = content.Draw(page, new RectangleF(0, result.Bounds.Bottom + paragraphAfterSpacing, page.GetClientSize().Width, page.GetClientSize().Height), format);
PdfGrid pdfGrid = new PdfGrid();
pdfGrid.Style.CellPadding.Left = cellMargin;
pdfGrid.Style.CellPadding.Right = cellMargin;
pdfGrid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable4Accent1);
pdfGrid.DataSource = forecasts;
pdfGrid.Style.Font = contentFont;
pdfGrid.Draw(page, new PointF(0, result.Bounds.Bottom + paragraphAfterSpacing));

using (MemoryStream stream = new MemoryStream())
{
pdfDocument.Save(stream);
return new MemoryStream(stream.ToArray());
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using Microsoft.AspNetCore.Mvc;
using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Grid;

namespace Web_API_Project.Controllers
{
Expand Down Expand Up @@ -33,67 +29,5 @@ public IEnumerable<WeatherForecast> Get()
})
.ToArray();
}

[HttpGet("/api/Pdf")]
public IActionResult CreatePdfDocument()
{
try
{
const string fileDownloadName = "Output.pdf";
const string contentType = "application/pdf";
var stream = ExportWeatherForecastToPdf();
stream.Position = 0;
return File(stream, contentType, fileDownloadName);
}
catch (Exception ex)
{
return BadRequest($"Error occurred while creating PDF file: {ex.Message}");
}
}

private MemoryStream ExportWeatherForecastToPdf()
{
var forecasts = Get().ToList();

//Create a new PDF document.
using (PdfDocument pdfDocument = new PdfDocument())
{
int paragraphAfterSpacing = 8;
int cellMargin = 8;
//Add page to the PDF document.
PdfPage page = pdfDocument.Pages.Add();
//Create a new font.
PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 16);
//Create a text element to draw a text in PDF page.
PdfTextElement title = new PdfTextElement("Weather Forecast", font, PdfBrushes.Black);
PdfLayoutResult result = title.Draw(page, new PointF(0, 0));
PdfStandardFont contentFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 12);
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);
PdfLayoutFormat format = new PdfLayoutFormat
{
Layout = PdfLayoutType.Paginate
};
//Draw a text to the PDF document.
result = content.Draw(page, new RectangleF(0, result.Bounds.Bottom + paragraphAfterSpacing, page.GetClientSize().Width, page.GetClientSize().Height), format);
//Create a PdfGrid.
PdfGrid pdfGrid = new PdfGrid();
pdfGrid.Style.CellPadding.Left = cellMargin;
pdfGrid.Style.CellPadding.Right = cellMargin;
//Applying built-in style to the PDF grid.
pdfGrid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable4Accent1);
//Assign data source.
pdfGrid.DataSource = forecasts;
pdfGrid.Style.Font = contentFont;
//Draw PDF grid into the PDF page.
pdfGrid.Draw(page, new PointF(0, result.Bounds.Bottom + paragraphAfterSpacing));

using (MemoryStream stream = new MemoryStream())
{
//Saving the PDF document into the stream.
pdfDocument.Save(stream);
return new MemoryStream(stream.ToArray());
}
}
}
}
}
Loading