1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using System . Threading . Tasks ;
5+ using Telerik . Examples . Mvc . Models ;
6+ using Microsoft . AspNetCore . Mvc ;
7+ using Microsoft . EntityFrameworkCore ;
8+ using Microsoft . AspNetCore . Hosting ;
9+ using System . IO ;
10+ using System . Text . RegularExpressions ;
11+ using Telerik . Windows . Documents . Flow . FormatProviders . Docx ;
12+ using Telerik . Windows . Documents . Flow . FormatProviders . Html ;
13+ using Telerik . Windows . Documents . Flow . Model . Collections ;
14+ using Telerik . Windows . Documents . Flow . Model . Styles ;
15+ using Telerik . Windows . Documents . Flow . Model ;
16+ using Kendo . Core . Export ;
17+
18+ namespace Telerik . Examples . Mvc . Controllers . Editor
19+ {
20+ public class Editor_Header_And_Footer_SimulationController : Controller
21+ {
22+ private readonly IWebHostEnvironment _hostingEnvironment ;
23+ public Editor_Header_And_Footer_SimulationController ( IWebHostEnvironment hostingEnvironment )
24+ {
25+ _hostingEnvironment = hostingEnvironment ;
26+ }
27+
28+
29+ public IActionResult Editor_Header_And_Footer_Simulation ( )
30+ {
31+ string docXFilePath = Path . Combine ( _hostingEnvironment . WebRootPath , "files" , "sample.docx" ) ;
32+
33+ DocxFormatProvider docxProvider = new ( ) ;
34+ HtmlFormatProvider htmlProvider = new ( ) ;
35+ RadFlowDocument ? document = null ;
36+ Paragraph headers = null ;
37+ InlineCollection headerBlockContent ;
38+ InlineCollection footerBlockContent ;
39+ Paragraph footers = null ;
40+
41+ using ( FileStream input = new FileStream ( docXFilePath , FileMode . Open ) )
42+ {
43+ document = docxProvider . Import ( input ) ;
44+ headers = ( Paragraph ) document . Sections . First ( ) . Headers . Default . Blocks . First ( ) ;
45+ headerBlockContent = headers . Inlines ;
46+ footers = ( Paragraph ) document . Sections . First ( ) . Footers . Default . Blocks . First ( ) ;
47+ footerBlockContent = footers . Inlines ;
48+ }
49+ var htmlString = htmlProvider . Export ( document ) ;
50+ var headerString = headerBlockContent [ 0 ] . ToString ( ) ;
51+ var footerString = footerBlockContent [ 1 ] . ToString ( ) ;
52+
53+ ViewData [ "htmlString" ] = htmlString ;
54+ ViewData [ "headerString" ] = headerString ;
55+ ViewData [ "footerString" ] = footerString ;
56+
57+ return View ( ) ;
58+ }
59+ [ HttpPost ]
60+ public ActionResult Export ( EditorExportData data )
61+ {
62+ string editorHtml = data . Value ;
63+ string encodedHtml = System . Net . WebUtility . HtmlDecode ( data . Value ) ;
64+ string classNameHeader = "header" ;
65+ string classNameFooter = "footer" ;
66+ string patternHeader = $ "<p\\ s+class=\" { classNameHeader } \" [^>]*>.*?</p>";
67+ string patternFooter = $ "<p\\ s+class=\" { classNameFooter } \" [^>]*>.*?</p>";
68+ Regex regexHeader = new Regex ( patternHeader ) ;
69+ Regex regexFooter = new Regex ( patternFooter ) ;
70+ string header = regexHeader . Match ( encodedHtml ) . Value ;
71+ string footer = regexFooter . Match ( encodedHtml ) . Value ;
72+ encodedHtml = encodedHtml . Replace ( header , "" ) . Replace ( footer , "" ) ;
73+ data . Value = System . Net . WebUtility . HtmlEncode ( encodedHtml ) ;
74+
75+ header = header . Replace ( "<p class=\" header\" >" , "" ) . Replace ( "</p>" , "" ) ;
76+ footer = footer . Replace ( "<p class=\" footer\" >" , "" ) . Replace ( "</p>" , "" ) ;
77+
78+ DocxFormatProvider docxProvider = new ( ) ;
79+ var exportFileStreamRaw = EditorExport . Export ( data ) ;
80+ byte [ ] exportFileStreamProcessed ;
81+ using ( Stream output = exportFileStreamRaw . FileStream )
82+ {
83+ var document = docxProvider . Import ( output ) ;
84+ Header defaultHeader = document . Sections . First ( ) . Headers . Add ( ) ;
85+ Paragraph defaultHeaderParagraph = defaultHeader . Blocks . AddParagraph ( ) ;
86+ defaultHeaderParagraph . TextAlignment = Alignment . Center ;
87+ defaultHeaderParagraph . Inlines . AddRun ( header ) ;
88+
89+ Footer defaultFooter = document . Sections . First ( ) . Footers . Add ( ) ;
90+ Paragraph defaultFooterParagraph = defaultFooter . Blocks . AddParagraph ( ) ;
91+ defaultFooterParagraph . TextAlignment = Alignment . Center ;
92+ defaultFooterParagraph . Inlines . AddRun ( footer ) ;
93+ exportFileStreamProcessed = docxProvider . Export ( document ) ;
94+ }
95+ try
96+ {
97+ return File ( exportFileStreamProcessed , "application/octet-stream" , data . FileName + ".docx" ) ;
98+ }
99+ catch
100+ {
101+ return RedirectToAction ( "Editor_Header_And_Footer_Simulation" , "Editor_Header_And_Footer_Simulation" ) ;
102+ }
103+ }
104+ }
105+ }
0 commit comments