Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 = $"<p\\s+class=\"{classNameHeader}\"[^>]*>.*?</p>";
string patternFooter = $"<p\\s+class=\"{classNameFooter}\"[^>]*>.*?</p>";
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("<p class=\"header\">", "").Replace("</p>", "");
footer = footer.Replace("<p class=\"footer\">", "").Replace("</p>", "");

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");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.6" />
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
<PackageReference Include="Telerik.Web.Spreadsheet.Trial" Version="2023.1.426" />
<PackageReference Include="Telerik.Core.Export.Trial" Version="2023.3.1114" />
<PackageReference Include="Telerik.Web.Spreadsheet.Trial" Version="2023.3.1114" />
<PackageReference Include="Telerik.UI.for.AspNet.Core.Trial" Version="2023.3.1114" />
<PackageReference Include="Telerik.Web.Captcha.Trial" Version="1.1.2" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@{
var editorHeader = ViewData["headerString"];
var editorBody = ViewData["htmlString"];
var editorFooter = ViewData["footerString"];

var editorValue = "<p class='header'>" + editorHeader + "</p></br>" + editorBody + "</br><p class='footer'>" + editorFooter + "</p>";
}

<div>
<div id="example" class="demo-section k-content wide editor">
@(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)
)
</div>
</div>

<style>
.k-list-item:not(:first-child) {
display:none;
}
</style>
Original file line number Diff line number Diff line change
@@ -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;
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.