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 System.Drawing; using Neo.LegitimateLicences.Data.Models; namespace Neo.LegitimateLicences.Web.AfxServices { /// /// Summary description for PdfInstanceDocumentsGenerator /// public class PdfInstanceDocumentsGenerator : IHttpHandler { public void ProcessRequest(HttpContext context) { if (context.Request.QueryString.Count == 0) return; var entityID = -1; var itemID = -1L; var optionalEntityID = -1; var optionalItemID = -1L; var database = new ReportDatabase(); if (!Int32.TryParse(context.Request.QueryString["entityID"], out entityID)) return; if (!Int64.TryParse(context.Request.QueryString["itemID"], out itemID)) return; if (!Int32.TryParse(context.Request.QueryString["optionalEntityID"], out optionalEntityID)) { } if (!Int64.TryParse(context.Request.QueryString["optionalItemID"], out optionalItemID)) { } var db = new WorkflowsDatabase(); var docDb = new DocumentsDatabase(); var legitimateDb = new LegitimateDatabase(); var instance = db.GetWorkflowInstanceView(entityID, itemID); var documents = db.GetWorkflowInstanceDocuments(instance.WorkflowInstanceID, UserProfile.Current.UserID); var documentTypes = legitimateDb.GetDocumentTypes(); var optionalDocuments = docDb.GetDocuments(optionalEntityID, optionalItemID).ToList(); if(optionalDocuments == null) { optionalDocuments = new List(); } using (var mergedStream = new MemoryStream()) { using (PdfDocumentProcessor pdfDocumentProcessor = new PdfDocumentProcessor()) { pdfDocumentProcessor.CreateEmptyDocument(); foreach (var doc in documents.Where(a => a.DocumentID.HasValue)) { var file = docDb.GetDocumentObject(doc.DocumentID.Value); if (file.Extension.ToLower() ==".pdf") { using (var fileStream = new MemoryStream(file.Content)) { var pageCountBeforeAdd = (pdfDocumentProcessor.Document != null) ? pdfDocumentProcessor.Document.Pages.Count() : 0; pdfDocumentProcessor.AppendDocument(fileStream); #region Add bookmark PdfDestination destination1 = pdfDocumentProcessor.CreateDestination(pageCountBeforeAdd + 1); var bookMark = new PdfBookmark() { Title = doc.DocumentDescription, Destination = destination1 }; pdfDocumentProcessor.Document.Bookmarks.Add(bookMark); #endregion } } else { var pageCountBeforeAdd = (pdfDocumentProcessor.Document != null) ? pdfDocumentProcessor.Document.Pages.Count() : 0; string message = "Unable to print document :" + Environment.NewLine + doc.DocumentDescription; using (PdfGraphics graphics = pdfDocumentProcessor.CreateGraphics()) { SolidBrush black = (SolidBrush)Brushes.Black; using (Font font = new Font("Segoe UI", 9, FontStyle.Bold)) { //graphics.DrawString(message, font, black, 10, 10); DrawGraphics(graphics, message); } pdfDocumentProcessor.RenderNewPage(PdfPaperSize.Letter, graphics); } #region Add bookmark PdfDestination destination1 = pdfDocumentProcessor.CreateDestination(pageCountBeforeAdd + 1); var bookMark = new PdfBookmark() { Title = doc.DocumentDescription, Destination = destination1 }; pdfDocumentProcessor.Document.Bookmarks.Add(bookMark); #endregion } } foreach (var doc in optionalDocuments.Where(a => a.DocumentID > 0)) { var file = docDb.GetDocumentObject(doc.DocumentID); var description = documentTypes.Where(a => a.DocumentTypeID == doc.DocumentTypeID).FirstOrDefault().Description; if (file.Extension.ToLower() == ".pdf") { using (var fileStream = new MemoryStream(file.Content)) { var pageCountBeforeAdd = (pdfDocumentProcessor.Document != null) ? pdfDocumentProcessor.Document.Pages.Count() : 0; pdfDocumentProcessor.AppendDocument(fileStream); #region Add bookmark PdfDestination destination1 = pdfDocumentProcessor.CreateDestination(pageCountBeforeAdd + 1); var bookMark = new PdfBookmark() { Title = description, Destination = destination1 }; pdfDocumentProcessor.Document.Bookmarks.Add(bookMark); #endregion } } else { var pageCountBeforeAdd = (pdfDocumentProcessor.Document != null) ? pdfDocumentProcessor.Document.Pages.Count() : 0; string message = "Unable to print document :" + Environment.NewLine + description; using (PdfGraphics graphics = pdfDocumentProcessor.CreateGraphics()) { SolidBrush black = (SolidBrush)Brushes.Black; using (Font font = new Font("Segoe UI", 9, FontStyle.Bold)) { //graphics.DrawString(message, font, black, 10, 10); DrawGraphics(graphics, message); } pdfDocumentProcessor.RenderNewPage(PdfPaperSize.Letter, graphics); } #region Add bookmark PdfDestination destination1 = pdfDocumentProcessor.CreateDestination(pageCountBeforeAdd + 1); var bookMark = new PdfBookmark() { Title = description, Destination = destination1 }; pdfDocumentProcessor.Document.Bookmarks.Add(bookMark); #endregion } } pdfDocumentProcessor.SaveDocument(mergedStream); } #region Push Data var fileName = instance.ReferenceNumber + DateTime.Now.ToString("ddMMyyyyhhmmss") + ".pdf"; context.Response.ClearHeaders(); context.Response.ClearContent(); context.Response.AddHeader("Content-Length", mergedStream.Length.ToString()); context.Response.ContentType = "application/pdf"; context.Response.Charset = "UTF-8"; mergedStream.Position = 0; context.Response.BinaryWrite(mergedStream.ToArray()); context.Response.Flush(); context.Response.Clear(); #endregion } } public bool IsReusable { get { return false; } } static void DrawGraphics(PdfGraphics graph, string message) { SolidBrush black = (SolidBrush)Brushes.Black; using (Font font = new Font("Times New Roman", 12, FontStyle.Bold)) { graph.DrawString(message, font, black, new RectangleF(100, 100, 500, 900)); } } } }