using System; using System.Configuration; using System.Web; using DevExpress.XtraPrinting; using DevExpress.XtraReports.UI; using System.Collections.Generic; using Neo.Afx.Services.Reports; using System.IO; using System.Linq; using Neo.Afx.Security; using Neo.Afx.Services.Documents; using Neo.Afx.Services.Workflows; using DevExpress.Pdf; using Neo.Afx.Services.Integration; using DevExpress.XtraRichEdit.API.Native; using Neo.LegitimateLicences.Data.Models; using System.Net; using DevExpress.Office.Utils; using DevExpress.Office; namespace Neo.LegitimateLicences.Web.AfxServices { public class GazettePackGenerator : IHttpHandler { public void ProcessRequest(HttpContext context) { if (context.Request.QueryString.Count == 0) return; var gazetteDetailID = -1L; var gazetteRegionID = -1L; if (!Int64.TryParse(context.Request.QueryString["gazetteDetailID"], out gazetteDetailID)) return; if (!Int64.TryParse(context.Request.QueryString["gazetteRegionID"], out gazetteRegionID)) return; string baseUrl = context.Request.Url.GetLeftPart(UriPartial.Authority); var db = new LegitimateDatabase(); var docDb = new DocumentsDatabase(); var generatedDocumentBytes = new byte[0]; var gazetteDetails = db.GetGazetteDetailPack(gazetteDetailID, gazetteRegionID, UserProfile.Current.UserID); var url = baseUrl + "/Content/Templates/GazettePack.docx"; Byte[] templateFileContent = null; #region Get Template File HttpWebResponse response = null; Stream remoteStream = null; MemoryStream localStream = null; int bytesProcessed = 0; HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url); request.Timeout = 300000; //5 minutes request.KeepAlive = false; request.ProtocolVersion = HttpVersion.Version10; request.Method = "GET"; response = (HttpWebResponse)request.GetResponse(); remoteStream = response.GetResponseStream(); localStream = new MemoryStream(); byte[] buffer = new byte[1024]; int bytesRead; do { bytesRead = remoteStream.Read(buffer, 0, buffer.Length); localStream.Write(buffer, 0, bytesRead); bytesProcessed += bytesRead; } while (bytesRead > 0); templateFileContent = localStream.ToArray(); #endregion using (var srv = new DevExpress.XtraRichEdit.RichEditDocumentServer()) { DevExpress.XtraRichEdit.API.Native.Document doc = srv.Document; using (var stream = new MemoryStream(templateFileContent)) { doc.AppendDocumentContent(stream); } #region Create table stylesheet doc.BeginUpdate(); TableStyle tStyleMain = doc.TableStyles.CreateNew(); tStyleMain.FontName = "Arial"; tStyleMain.FontSize = 9; doc.TableStyles.Add(tStyleMain); doc.EndUpdate(); #endregion #region Print Gazette Details Table table = doc.Tables[0]; table.BeginUpdate(); int rowIndex = 0; List indexToBeMerged = new List(); foreach (var gazetteApplication in gazetteDetails) { #region Row 1 doc.InsertSingleLineText(table.Rows[rowIndex].Cells[0].Range.Start, "1)"); doc.InsertSingleLineText(table.Rows[rowIndex].Cells[1].Range.Start, "APPLICATION NUMBER:"+ gazetteApplication.ReferenceNumber); doc.InsertSingleLineText(table.Rows[rowIndex].Cells[2].Range.Start, "2)"); doc.InsertSingleLineText(table.Rows[rowIndex].Cells[3].Range.Start, "GAZETTE NUMBER:" + gazetteApplication.GazetteNumber); table.Rows.InsertAfter(rowIndex); rowIndex++; #endregion #region Row 2 doc.InsertSingleLineText(table.Rows[rowIndex].Cells[0].Range.Start, "3)"); doc.InsertSingleLineText(table.Rows[rowIndex].Cells[1].Range.Start, "APPLICANT:" + gazetteApplication.FirstNames + " " + gazetteApplication.SurnameOrCompanyName + Characters.LineBreak.ToString() + "ID NO: "+gazetteApplication.IDNumberOrBusinessRegistrationNo + Characters.LineBreak.ToString() + "ASSOCIATION: " + gazetteApplication.AssociationName); doc.InsertSingleLineText(table.Rows[rowIndex].Cells[2].Range.Start, "4)"); doc.InsertSingleLineText(table.Rows[rowIndex].Cells[3].Range.Start, "APPLICANT ADDRESS:" + gazetteApplication.PostalAddress + Characters.LineBreak.ToString() + gazetteApplication.PostalAddressCode); table.Rows.InsertAfter(rowIndex); rowIndex++; #endregion #region Row 3 doc.InsertSingleLineText(table.Rows[rowIndex].Cells[0].Range.Start, "5)"); doc.InsertSingleLineText(table.Rows[rowIndex].Cells[1].Range.Start, "EXISTING LICENCE HOLDER:" + gazetteApplication.ExistingFirstname + " " + gazetteApplication.ExistingSurname + Characters.LineBreak.ToString() + "ID NO: " + gazetteApplication.ExistingIDNumber); doc.InsertSingleLineText(table.Rows[rowIndex].Cells[2].Range.Start, "6)"); doc.InsertSingleLineText(table.Rows[rowIndex].Cells[3].Range.Start, "EXISTING LICENCE HOLDER ADDRESS:" + gazetteApplication.ExistingPostalAddress + Characters.LineBreak.ToString() + gazetteApplication.ExistingPostalCode); table.Rows.InsertAfter(rowIndex); rowIndex++; #endregion #region Row 4 doc.InsertSingleLineText(table.Rows[rowIndex].Cells[0].Range.Start, "7)"); doc.InsertSingleLineText(table.Rows[rowIndex].Cells[1].Range.Start, "TYPE OF APPLICATION:" + gazetteApplication.ApplicationRequestTypesTitle); doc.InsertSingleLineText(table.Rows[rowIndex].Cells[2].Range.Start, "8)"); doc.InsertSingleLineText(table.Rows[rowIndex].Cells[3].Range.Start, "OPERATING LICENCE NUMBER:" + gazetteApplication.OperatingLicenceNumber); table.Rows.InsertAfter(rowIndex); rowIndex++; #endregion #region Row 5 doc.InsertSingleLineText(table.Rows[rowIndex].Cells[0].Range.Start, "9)"); doc.InsertSingleLineText(table.Rows[rowIndex].Cells[1].Range.Start, "VEHICLE TYPE:" + gazetteApplication.VehicleType); doc.InsertSingleLineText(table.Rows[rowIndex].Cells[2].Range.Start, "10)"); doc.InsertSingleLineText(table.Rows[rowIndex].Cells[3].Range.Start, gazetteApplication.CapacityDescription); table.Rows.InsertAfter(rowIndex); rowIndex++; #endregion #region Row 6 doc.InsertSingleLineText(table.Rows[rowIndex].Cells[0].Range.Start, "11)"); doc.InsertSingleLineText(table.Rows[rowIndex].Cells[1].Range.Start, "PROPOSED LIST OF ROUTES"); indexToBeMerged.Add(rowIndex); table.Rows.InsertAfter(rowIndex); rowIndex++; #endregion #region Row 7 Route Details var licenceRouteDetails = db.GetApplicationGazetteRoutes(gazetteApplication.ApplicationDetailID, gazetteApplication.ApplicationLicenceID, UserProfile.Current.UserID); var ServiceTypeNumbers = licenceRouteDetails.OrderBy(z => z.ServiceTypeNumber).Select(z => z.ServiceTypeNumber).Distinct().ToList(); #region Spin through services foreach (var ServiceTypeNumber in ServiceTypeNumbers) { var serviceType = licenceRouteDetails.Where(a => a.ServiceTypeNumber == ServiceTypeNumber).FirstOrDefault(); doc.InsertSingleLineText(table.Rows[rowIndex].Cells[0].Range.Start, "11."+ serviceType.ServiceTypeNumber+ ")"); doc.InsertSingleLineText(table.Rows[rowIndex].Cells[1].Range.Start, serviceType.ServiceType); indexToBeMerged.Add(rowIndex); table.Rows.InsertAfter(rowIndex); rowIndex++; foreach (var route in licenceRouteDetails.Where(a => a.ServiceTypeNumber == ServiceTypeNumber).ToList().OrderBy(o => o.RouteNumber)) { doc.InsertSingleLineText(table.Rows[rowIndex].Cells[0].Range.Start, "11." + ServiceTypeNumber + "." + route.RouteNumber + ")"); doc.InsertSingleLineText(table.Rows[rowIndex].Cells[1].Range.Start, route.RouteDescription); indexToBeMerged.Add(rowIndex); table.Rows.InsertAfter(rowIndex); rowIndex++; } } #endregion #endregion } foreach(var index in indexToBeMerged) { table.MergeCells(table[index, 1], table[index, 3]); } table.Style = tStyleMain; table.EndUpdate(); #endregion doc.ReplaceAll("$Date_Now$", DateTime.Now.ToString("dd MM yyyy"), DevExpress.XtraRichEdit.API.Native.SearchOptions.None); doc.ReplaceAll("$GazetteNumber$", gazetteDetails.FirstOrDefault().GazetteNumber, DevExpress.XtraRichEdit.API.Native.SearchOptions.None); doc.ReplaceAll("$Region$", gazetteDetails.FirstOrDefault().Region, DevExpress.XtraRichEdit.API.Native.SearchOptions.None); using (var generatedocumentStream = new MemoryStream()) { var pdfOptions = new PdfExportOptions(); pdfOptions.DocumentOptions.Author = "Mark Jones"; pdfOptions.Compressed = false; pdfOptions.ImageQuality = PdfJpegImageQuality.Highest; srv.ExportToPdf(generatedocumentStream, pdfOptions); generatedDocumentBytes = new byte[generatedocumentStream.Length]; generatedDocumentBytes = generatedocumentStream.ToArray(); } } #region Push Data var fileName = DateTime.Now.ToString("ddMMyyyyhhmmss") + ".pdf"; var contentType = "application/pdf"; context.Response.ClearHeaders(); context.Response.ClearContent(); context.Response.AddHeader("Content-Length", generatedDocumentBytes.Length.ToString()); context.Response.ContentType = contentType; context.Response.Charset = "UTF-8"; context.Response.BinaryWrite(generatedDocumentBytes); context.Response.Flush(); context.Response.Clear(); #endregion } #region IsReusable public bool IsReusable { get { return false; } } #endregion } }