Esempio n. 1
0
        public string Document([System.Web.Http.FromBody] UploadData uploadData)
        {
            string            folder          = Request.Url.GetLeftPart(UriPartial.Authority) + "/Temp/";
            DocumentCreator   documentCreator = new DocumentCreator();
            InformativeExport data            = JsonConvert.DeserializeObject <InformativeExport>(uploadData.data);

            return(documentCreator.Create(folder, data));
        }
Esempio n. 2
0
        /// <summary>
        /// Create PDF-document
        /// </summary>
        /// <param name="folder"></param>
        /// <returns>Path to saved document</returns>
        public string Create(string folder, InformativeExport informativeExport)
        {
            this.documentFile = informativeExport.documentFile;
            this.mapFile      = informativeExport.mapFile;
            this.baseLayer    = informativeExport.baseMapId;

            string file       = String.Format("{0}App_Data\\{1}", HostingEnvironment.ApplicationPhysicalPath, this.layersFile);
            var    layersJson = System.IO.File.ReadAllText(file);

            this.layers = JsonConvert.DeserializeObject <LayerConfig>(layersJson);

            string mapFile = String.Format("{0}App_Data\\{1}", HostingEnvironment.ApplicationPhysicalPath, this.mapFile);
            var    mapJson = System.IO.File.ReadAllText(mapFile);

            this.mapConfig = JsonConvert.DeserializeObject <MapConfig>(mapJson);
            Tool t = this.mapConfig.tools.Find(tool => tool.type == "layerswitcher");

            if (t != null)
            {
                this.layerSwitcherOptions = JsonConvert.DeserializeObject <LayerSwitcherOptions>(t.options.ToString());
            }

            // Some operations will use the tmp-folder. Created files are deleted when used.
            Directory.CreateDirectory("C:\\tmp");

            // Load informative document
            string documentFile   = String.Format("{0}App_Data\\documents\\{1}", HostingEnvironment.ApplicationPhysicalPath, this.documentFile);
            string documentJson   = System.IO.File.ReadAllText(documentFile);
            var    exportDocument = JsonConvert.DeserializeObject <Document>(documentJson);

            StringBuilder html = new StringBuilder();

            String.Format("{0}App_Data\\documents\\{1}", HostingEnvironment.ApplicationPhysicalPath, this.documentFile);

            html.AppendLine("<head>");
            html.AppendLine("<meta charset='UTF-8'>");
            html.AppendLine("</head>");
            html.AppendLine("<body>");
            html.AppendLine("<style>");
            html.AppendLine("body { font-family: arial; font-size: 13pt !important;}");
            html.AppendLine("body img { width: 100%; }");
            html.AppendLine("</style>");

            // Append maps to document
            this.totalChapters = GetTotalChapters(exportDocument.chapters.ToList(), 0);
            this.AppendHtml(exportDocument.chapters.ToList(), ref html);
            html.AppendLine("</body>");

            var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();

            htmlToPdf.Margins.Bottom = 15;
            htmlToPdf.Margins.Top    = 15;

            htmlToPdf.PageFooterHtml = "<div style='text-align:center;'><span class='page'></span> </div>";
            var pdfBytes1 = htmlToPdf.GeneratePdf(html.ToString());

            //
            // Read the created document and match headers to create toc.
            // Headers must be unique
            //
            string tocHtml = "";

            iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(pdfBytes1);

            Dictionary <int, string[]> pageContents = new Dictionary <int, string[]>();

            for (int j = 1; j <= reader.NumberOfPages; j++)
            {
                string text = iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(reader, j);
                pageContents.Add(j, text.Split('\n'));
            }
            tocHtml += "<head>";
            tocHtml += "<meta charset='UTF-8'>";
            tocHtml += "</head>";
            tocHtml += "<body>";
            tocHtml += "<style>";
            tocHtml += "body { font-family: arial; font-size: 13pt !important;}";
            tocHtml += "body img { width: 100%; }";
            tocHtml += "</style>";
            tocHtml += "<h1>Innehållsförteckning</h1>";

            this.AppendHeader(exportDocument.chapters.ToList(), ref tocHtml, pageContents);

            tocHtml += "</body>";

            htmlToPdf.PageFooterHtml = "";
            var pdfBytes2 = htmlToPdf.GeneratePdf(tocHtml.ToString());

            MemoryStream stream1 = new MemoryStream(pdfBytes1);
            MemoryStream stream2 = new MemoryStream(pdfBytes2);

            PdfSharp.Pdf.PdfDocument inputDocument1 = PdfSharp.Pdf.IO.PdfReader.Open(stream1, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Import);
            PdfSharp.Pdf.PdfDocument inputDocument2 = PdfSharp.Pdf.IO.PdfReader.Open(stream2, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Import);
            PdfSharp.Pdf.PdfDocument outputDocument = new PdfSharp.Pdf.PdfDocument();

            List <PdfSharp.Pdf.PdfDocument> inputDocuments = new List <PdfSharp.Pdf.PdfDocument>();

            inputDocuments.Add(inputDocument2);
            inputDocuments.Add(inputDocument1);

            //
            // Remove unwanted pages.
            // Pages within the same chapter will remain.
            //
            List <Chapter> flattened = new List <Chapter>();

            FlattenChapters(exportDocument.chapters, ref flattened);
            int chapterCount = 0;

            this.SetChapterPages(exportDocument.chapters, flattened, ref chapterCount);
            int initialPageCount = inputDocument1.Pages.Count;

            this.RemoveSurroundingPages(informativeExport.chapterHeader, informativeExport.chapterHtml, exportDocument.chapters, inputDocument1);
            int modifiedPageCount = inputDocument1.Pages.Count;

            if (initialPageCount == modifiedPageCount)
            {
                this.RemoveLastPage(inputDocument1);
            }

            inputDocuments.ForEach(document =>
            {
                int count = document.PageCount;
                for (int idx = 0; idx < count; idx++)
                {
                    PdfSharp.Pdf.PdfPage page = document.Pages[idx];
                    outputDocument.AddPage(page);
                }
            });

            reader.Close();
            stream1.Close();
            stream2.Close();

            var    r         = new Random();
            var    i         = r.Next();
            string fileName  = "pdf-export-" + i + ".pdf";
            string localFile = HostingEnvironment.ApplicationPhysicalPath + "//Temp//" + fileName;

            outputDocument.Save(localFile);

            return(fileName);
        }