diff --git a/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Controllers/Editor/Editor_Header_And_Footer_SimulationController.cs b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Controllers/Editor/Editor_Header_And_Footer_SimulationController.cs new file mode 100644 index 0000000..8943ae9 --- /dev/null +++ b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Controllers/Editor/Editor_Header_And_Footer_SimulationController.cs @@ -0,0 +1,105 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Telerik.Examples.Mvc.Models; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using Microsoft.AspNetCore.Hosting; +using System.IO; +using System.Text.RegularExpressions; +using Telerik.Windows.Documents.Flow.FormatProviders.Docx; +using Telerik.Windows.Documents.Flow.FormatProviders.Html; +using Telerik.Windows.Documents.Flow.Model.Collections; +using Telerik.Windows.Documents.Flow.Model.Styles; +using Telerik.Windows.Documents.Flow.Model; +using Kendo.Core.Export; + +namespace Telerik.Examples.Mvc.Controllers.Editor +{ + public class Editor_Header_And_Footer_SimulationController : Controller + { + private readonly IWebHostEnvironment _hostingEnvironment; + public Editor_Header_And_Footer_SimulationController(IWebHostEnvironment hostingEnvironment) + { + _hostingEnvironment = hostingEnvironment; + } + + + public IActionResult Editor_Header_And_Footer_Simulation() + { + string docXFilePath = Path.Combine(_hostingEnvironment.WebRootPath, "files", "sample.docx"); + + DocxFormatProvider docxProvider = new(); + HtmlFormatProvider htmlProvider = new(); + RadFlowDocument? document = null; + Paragraph headers = null; + InlineCollection headerBlockContent; + InlineCollection footerBlockContent; + Paragraph footers = null; + + using (FileStream input = new FileStream(docXFilePath, FileMode.Open)) + { + document = docxProvider.Import(input); + headers = (Paragraph) document.Sections.First().Headers.Default.Blocks.First(); + headerBlockContent = headers.Inlines; + footers = (Paragraph) document.Sections.First().Footers.Default.Blocks.First(); + footerBlockContent = footers.Inlines; + } + var htmlString = htmlProvider.Export(document); + var headerString = headerBlockContent[0].ToString(); + var footerString = footerBlockContent[1].ToString(); + + ViewData["htmlString"] = htmlString; + ViewData["headerString"] = headerString; + ViewData["footerString"] = footerString; + + return View(); + } + [HttpPost] + public ActionResult Export(EditorExportData data) + { + string editorHtml = data.Value; + string encodedHtml = System.Net.WebUtility.HtmlDecode(data.Value); + string classNameHeader = "header"; + string classNameFooter = "footer"; + string patternHeader = $"]*>.*?

"; + string patternFooter = $"]*>.*?

"; + Regex regexHeader = new Regex(patternHeader); + Regex regexFooter = new Regex(patternFooter); + string header = regexHeader.Match(encodedHtml).Value; + string footer = regexFooter.Match(encodedHtml).Value; + encodedHtml = encodedHtml.Replace(header, "").Replace(footer, ""); + data.Value = System.Net.WebUtility.HtmlEncode(encodedHtml); + + header = header.Replace("

", "").Replace("

", ""); + footer = footer.Replace("

", "").Replace("

", ""); + + DocxFormatProvider docxProvider = new(); + var exportFileStreamRaw = EditorExport.Export(data); + byte[] exportFileStreamProcessed; + using (Stream output = exportFileStreamRaw.FileStream) + { + var document = docxProvider.Import(output); + Header defaultHeader = document.Sections.First().Headers.Add(); + Paragraph defaultHeaderParagraph = defaultHeader.Blocks.AddParagraph(); + defaultHeaderParagraph.TextAlignment = Alignment.Center; + defaultHeaderParagraph.Inlines.AddRun(header); + + Footer defaultFooter = document.Sections.First().Footers.Add(); + Paragraph defaultFooterParagraph = defaultFooter.Blocks.AddParagraph(); + defaultFooterParagraph.TextAlignment = Alignment.Center; + defaultFooterParagraph.Inlines.AddRun(footer); + exportFileStreamProcessed = docxProvider.Export(document); + } + try + { + return File(exportFileStreamProcessed, "application/octet-stream", data.FileName + ".docx"); + } + catch + { + return RedirectToAction("Editor_Header_And_Footer_Simulation", "Editor_Header_And_Footer_Simulation"); + } + } + } +} \ No newline at end of file diff --git a/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Telerik.Examples.Mvc.csproj b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Telerik.Examples.Mvc.csproj index 54eca7b..fd386ef 100644 --- a/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Telerik.Examples.Mvc.csproj +++ b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Telerik.Examples.Mvc.csproj @@ -21,7 +21,8 @@ - + + diff --git a/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Views/Editor/Editor_Header_And_Footer_Simulation.cshtml b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Views/Editor/Editor_Header_And_Footer_Simulation.cshtml new file mode 100644 index 0000000..226c50d --- /dev/null +++ b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Views/Editor/Editor_Header_And_Footer_Simulation.cshtml @@ -0,0 +1,32 @@ +@{ + var editorHeader = ViewData["headerString"]; + var editorBody = ViewData["htmlString"]; + var editorFooter = ViewData["footerString"]; + + var editorValue = "

" + editorHeader + "


" + editorBody + "
"; +} + +
+
+ @(Html.Kendo().Editor() + .Name("editor") + .Tools(tools => tools + .Clear() + .ExportAs() + ) + .ExportAs(exportAs => exportAs + .FileName("editor_content") + .Proxy("Export", "Editor_Header_And_Footer_Simulation") + ) + .StyleSheets(css => css.Add(Url.Content("~/css/editorstyles.css"))) + .HtmlAttributes(new { style = "width: 100%; height:840px" }) + .Value(editorValue) + ) +
+
+ + \ No newline at end of file diff --git a/Telerik.Examples.Mvc/Telerik.Examples.Mvc/wwwroot/css/editorStyles.css b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/wwwroot/css/editorStyles.css new file mode 100644 index 0000000..0591629 --- /dev/null +++ b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/wwwroot/css/editorStyles.css @@ -0,0 +1,31 @@ +.hlError +{ + background-color:#fea; + color:#f33; +} + +.header,.footer{ + color:lightslategray +} + +.footer{ + position:absolute; + bottom:0px; +} + +.hlOK +{ + background-color:#aef; + color:#060; +} + +.inlineCode +{ + font:bold 12px monospace; +} + +body > img { + margin-top: 15px; + margin-left: 15px; + margin-right: 0px; +} diff --git a/Telerik.Examples.Mvc/Telerik.Examples.Mvc/wwwroot/files/sample.docx b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/wwwroot/files/sample.docx new file mode 100644 index 0000000..29e90fe Binary files /dev/null and b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/wwwroot/files/sample.docx differ diff --git a/Telerik.Examples.Mvc/nuget/Telerik.Core.Export.Trial.2023.3.1114.nupkg b/Telerik.Examples.Mvc/nuget/Telerik.Core.Export.Trial.2023.3.1114.nupkg new file mode 100644 index 0000000..faf3599 Binary files /dev/null and b/Telerik.Examples.Mvc/nuget/Telerik.Core.Export.Trial.2023.3.1114.nupkg differ diff --git a/Telerik.Examples.Mvc/nuget/Telerik.Web.Spreadsheet.Trial.2023.1.426.nupkg b/Telerik.Examples.Mvc/nuget/Telerik.Web.Spreadsheet.Trial.2023.1.426.nupkg deleted file mode 100644 index 49b81a2..0000000 Binary files a/Telerik.Examples.Mvc/nuget/Telerik.Web.Spreadsheet.Trial.2023.1.426.nupkg and /dev/null differ diff --git a/Telerik.Examples.Mvc/nuget/Telerik.Web.Spreadsheet.Trial.2023.3.1114.nupkg b/Telerik.Examples.Mvc/nuget/Telerik.Web.Spreadsheet.Trial.2023.3.1114.nupkg new file mode 100644 index 0000000..8054d27 Binary files /dev/null and b/Telerik.Examples.Mvc/nuget/Telerik.Web.Spreadsheet.Trial.2023.3.1114.nupkg differ