Idea of the project: if someone wants to order a project development, here you can send an application.
using ProjectRequestTest.Core;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.IO;
using System.Web;
namespace ProjectRequestTest.Web
{
public class GeneratePDF : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
var requestIdentifier = Helpers.GetVar("id");
if (requestIdentifier > 0)
{
var projectRequestService = new ProjectRequestService();
var requestItem = projectRequestService.GetRequestByIdentifier(requestIdentifier);
if (requestItem.Id == 0)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Request for the project not found.");
}
else
{
context.Response.Clear();
context.Response.ContentType = "application/pdf";
context.Response.AddHeader("content-disposition", $"attachment;filename=ProjectRequestTest_{requestIdentifier}.pdf");
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
var document = new Document(PageSize.A4);
var baseFont = BaseFont.CreateFont(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIAL.TTF"), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
PdfWriter.GetInstance(document, context.Response.OutputStream);
document.Open();
var titleFont = FontFactory.GetFont("Arial", 24, Font.BOLD);
var boldTextFont = new Font(baseFont, 12, Font.BOLD);
var textFont = new Font(baseFont, 10);
document.Add(new Paragraph("Sergey Drozdov :: Test", titleFont));
var paragraphTable = new Paragraph();
paragraphTable.SpacingBefore = 2;
var infoTable = new PdfPTable(2);
infoTable.TotalWidth = 500f;
infoTable.HorizontalAlignment = 0;
infoTable.SpacingBefore = 10;
infoTable.SpacingAfter = 10;
infoTable.DefaultCell.Border = 0;
infoTable.SetWidths(new float[] { 5f, 10f });
infoTable.AddCell(new Phrase("Date created:", boldTextFont));
infoTable.AddCell(new Phrase(requestItem.DateCreated.ToString("ddd dd.MM.yyyy HH:mm"), textFont));
infoTable.AddCell(new Phrase("Your name:", boldTextFont));
infoTable.AddCell(new Phrase(requestItem.ClientName, textFont));
infoTable.AddCell(new Phrase("Email:", boldTextFont));
infoTable.AddCell(new Phrase(requestItem.Email, textFont));
infoTable.AddCell(new Phrase("Phone number:", boldTextFont));
infoTable.AddCell(new Phrase(requestItem.PhoneNumber, textFont));
infoTable.AddCell(new Phrase("Project type:", boldTextFont));
infoTable.AddCell(new Phrase(requestItem.ProjectType.ToString(), textFont));
infoTable.AddCell(new Phrase("Description:", boldTextFont));
infoTable.AddCell(new Phrase(requestItem.Description, textFont));
paragraphTable.Add(infoTable);
document.Add(paragraphTable);
document.Close();
}
}
context.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}