1+ using Form . Models ;
2+ using Microsoft . AspNetCore . Mvc ;
3+ using System . Diagnostics ;
4+ using Syncfusion . DocIO ;
5+ using Syncfusion . DocIO . DLS ;
6+ using Syncfusion . Drawing ;
7+
8+ namespace Form . Controllers
9+ {
10+ public class HomeController : Controller
11+ {
12+ private readonly ILogger < HomeController > _logger ;
13+
14+ public HomeController ( ILogger < HomeController > logger )
15+ {
16+ _logger = logger ;
17+ }
18+
19+ public IActionResult Index ( )
20+ {
21+ return View ( ) ;
22+ }
23+
24+ public IActionResult CreateForm ( )
25+ {
26+ WordDocument document = new WordDocument ( ) ;
27+ IWSection section = document . AddSection ( ) ;
28+ section . PageSetup . Margins . All = 50 ;
29+ WTextBody textBody = section . Body ;
30+
31+ WTable table = textBody . AddTable ( ) as WTable ;
32+ table . ResetCells ( 1 , 2 ) ;
33+ table . TableFormat . Borders . BorderType = BorderStyle . None ;
34+ table . TableFormat . Borders . Bottom . LineWidth = 3 ;
35+ table . TableFormat . Borders . Bottom . Color = Color . Blue ;
36+
37+ WParagraph paragraph = table . Rows [ 0 ] . Cells [ 0 ] . AddParagraph ( ) as WParagraph ;
38+ WTextRange text = paragraph . AppendText ( "Project Status Report" ) as WTextRange ;
39+ ApplyCharacterFormat ( text , 18 , true , Color . Blue ) ;
40+
41+ paragraph = table . Rows [ 0 ] . Cells [ 1 ] . AddParagraph ( ) as WParagraph ;
42+ paragraph . ParagraphFormat . BeforeSpacing = 8 ;
43+ text = paragraph . AppendText ( "Overall Status " ) as WTextRange ;
44+ ApplyCharacterFormat ( text , 12 , false , Color . Blue ) ;
45+
46+ AddDropDown ( paragraph , "Status" ,
47+ new string [ ] { "In-Progress" , "Testing" , "Review" , "Completed" } ) ;
48+
49+ paragraph = textBody . AddParagraph ( ) as WParagraph ;
50+ paragraph . ParagraphFormat . AfterSpacing = 24 ;
51+ paragraph . ParagraphFormat . BeforeSpacing = 12 ;
52+ text = paragraph . AppendText ( " Date: " ) as WTextRange ;
53+ ApplyCharacterFormat ( text , 12 , false , Color . Black ) ;
54+
55+ AddDate ( paragraph , "Date" ) ;
56+
57+ table = textBody . AddTable ( ) as WTable ;
58+ table . ResetCells ( 8 , 2 ) ;
59+ table . TableFormat . Borders . BorderType = BorderStyle . None ;
60+
61+ paragraph = table . Rows [ 0 ] . Cells [ 0 ] . AddParagraph ( ) as WParagraph ;
62+ paragraph . ParagraphFormat . AfterSpacing = 24 ;
63+ text = paragraph . AppendText ( "Project Name: " ) as WTextRange ;
64+ ApplyCharacterFormat ( text , 12 , true , Color . Black ) ;
65+
66+ paragraph = table . Rows [ 0 ] . Cells [ 1 ] . AddParagraph ( ) as WParagraph ;
67+ AddText ( paragraph , "ProjectName" ) ;
68+
69+ paragraph = table . Rows [ 1 ] . Cells [ 0 ] . AddParagraph ( ) as WParagraph ;
70+ paragraph . ParagraphFormat . AfterSpacing = 24 ;
71+ text = paragraph . AppendText ( "Team Size: " ) as WTextRange ;
72+ ApplyCharacterFormat ( text , 12 , true , Color . Black ) ;
73+
74+ paragraph = table . Rows [ 1 ] . Cells [ 1 ] . AddParagraph ( ) as WParagraph ;
75+ AddText ( paragraph , "TeamSize" ) ;
76+
77+ paragraph = table . Rows [ 2 ] . Cells [ 0 ] . AddParagraph ( ) as WParagraph ;
78+ paragraph . ParagraphFormat . AfterSpacing = 24 ;
79+ text = paragraph . AppendText ( "Language: " ) as WTextRange ;
80+ ApplyCharacterFormat ( text , 12 , true , Color . Black ) ;
81+
82+ paragraph = table . Rows [ 2 ] . Cells [ 1 ] . AddParagraph ( ) as WParagraph ;
83+ AddCheckbox ( paragraph , "C#" ) ;
84+ AddCheckbox ( paragraph , "VB" ) ;
85+
86+ paragraph = table . Rows [ 3 ] . Cells [ 0 ] . AddParagraph ( ) as WParagraph ;
87+ paragraph . ParagraphFormat . AfterSpacing = 24 ;
88+ text = paragraph . AppendText ( "Start Date: " ) as WTextRange ;
89+ ApplyCharacterFormat ( text , 12 , true , Color . Black ) ;
90+
91+ paragraph = table . Rows [ 3 ] . Cells [ 1 ] . AddParagraph ( ) as WParagraph ;
92+ AddDate ( paragraph , "StartDate" ) ;
93+
94+ paragraph = table . Rows [ 4 ] . Cells [ 0 ] . AddParagraph ( ) as WParagraph ;
95+ paragraph . ParagraphFormat . AfterSpacing = 24 ;
96+ text = paragraph . AppendText ( "Project Manager: " ) as WTextRange ;
97+ ApplyCharacterFormat ( text , 12 , true , Color . Black ) ;
98+
99+ paragraph = table . Rows [ 4 ] . Cells [ 1 ] . AddParagraph ( ) as WParagraph ;
100+ AddText ( paragraph , "ProjectManager" ) ;
101+
102+ paragraph = table . Rows [ 5 ] . Cells [ 0 ] . AddParagraph ( ) as WParagraph ;
103+ paragraph . ParagraphFormat . AfterSpacing = 24 ;
104+ text = paragraph . AppendText ( "Team Name: " ) as WTextRange ;
105+ ApplyCharacterFormat ( text , 12 , true , Color . Black ) ;
106+
107+ paragraph = table . Rows [ 5 ] . Cells [ 1 ] . AddParagraph ( ) as WParagraph ;
108+ AddText ( paragraph , "TeamName" ) ;
109+
110+ paragraph = table . Rows [ 6 ] . Cells [ 0 ] . AddParagraph ( ) as WParagraph ;
111+ paragraph . ParagraphFormat . AfterSpacing = 24 ;
112+ text = paragraph . AppendText ( "Platform: " ) as WTextRange ;
113+ ApplyCharacterFormat ( text , 12 , true , Color . Black ) ;
114+
115+ paragraph = table . Rows [ 6 ] . Cells [ 1 ] . AddParagraph ( ) as WParagraph ;
116+ AddDropDown ( paragraph , "Platform" ,
117+ new string [ ] { "ASP.NET" , "ASP.NET MVC" , "ASP.NET Core" , "Blazor" } ) ;
118+
119+ paragraph = table . Rows [ 7 ] . Cells [ 0 ] . AddParagraph ( ) as WParagraph ;
120+ paragraph . ParagraphFormat . AfterSpacing = 24 ;
121+ text = paragraph . AppendText ( "End Date: " ) as WTextRange ;
122+ ApplyCharacterFormat ( text , 12 , true , Color . Black ) ;
123+
124+ paragraph = table . Rows [ 7 ] . Cells [ 1 ] . AddParagraph ( ) as WParagraph ;
125+ AddDate ( paragraph , "EndDate" ) ;
126+
127+ section . HeadersFooters . Footer . AddParagraph ( ) ;
128+ BlockContentControl contact = section . HeadersFooters . Footer . AddBlockContentControl ( ContentControlType . RichText ) as BlockContentControl ;
129+ contact . ContentControlProperties . Title = "ContactInformation" ;
130+ paragraph = contact . TextBody . AddParagraph ( ) as WParagraph ;
131+ table = contact . TextBody . AddTable ( ) as WTable ;
132+ table . ResetCells ( 2 , 2 ) ;
133+ table . TableFormat . Borders . BorderType = BorderStyle . None ;
134+
135+ paragraph = table . Rows [ 0 ] . Cells [ 0 ] . AddParagraph ( ) as WParagraph ;
136+ paragraph . ParagraphFormat . AfterSpacing = 8 ;
137+ text = paragraph . AppendText ( "Contact Information: " ) as WTextRange ;
138+ ApplyCharacterFormat ( text , 12 , true , Color . Black ) ;
139+
140+ paragraph = table . Rows [ 1 ] . Cells [ 0 ] . AddParagraph ( ) as WParagraph ;
141+ text = paragraph . AppendText ( "Client Project Manager " ) as WTextRange ;
142+ ApplyCharacterFormat ( text , 12 , true , Color . Black ) ;
143+
144+ paragraph = table . Rows [ 1 ] . Cells [ 0 ] . AddParagraph ( ) as WParagraph ;
145+ text = paragraph . AppendText ( "Mobile: (206) 555-9857-x5467" ) as WTextRange ;
146+ ApplyCharacterFormat ( text , 12 , false , Color . Black ) ;
147+
148+ paragraph = table . Rows [ 1 ] . Cells [ 0 ] . AddParagraph ( ) as WParagraph ;
149+ text = paragraph . AppendText ( "Mail id: [email protected] " ) as WTextRange ; 150+ ApplyCharacterFormat ( text , 12 , false , Color . Black ) ;
151+
152+ MemoryStream stream = new MemoryStream ( ) ;
153+ document . Save ( stream , FormatType . Docx ) ;
154+ document . Close ( ) ;
155+ return File ( stream , "application/docx" , "Form.docx" ) ;
156+ }
157+
158+ public IActionResult FillForm ( )
159+ {
160+ FileStream fileStream = new FileStream ( Path . GetFullPath ( "Data/Form.docx" ) , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) ;
161+ WordDocument document = new WordDocument ( fileStream , FormatType . Docx ) ;
162+ fileStream . Dispose ( ) ;
163+
164+ InlineContentControl status = document . FindItemByProperty ( EntityType . InlineContentControl ,
165+ "ContentControlProperties.Title" , "Status" ) as InlineContentControl ;
166+ WTextRange textRange = status . ParagraphItems [ 0 ] as WTextRange ;
167+ textRange . Text = "In-Progress" ;
168+ ApplyCharacterFormat ( textRange ) ;
169+
170+ InlineContentControl date = document . FindItemByProperty ( EntityType . InlineContentControl ,
171+ "ContentControlProperties.Title" , "Date" ) as InlineContentControl ;
172+ textRange = date . ParagraphItems [ 0 ] as WTextRange ;
173+ textRange . Text = DateTime . Now . ToShortDateString ( ) ;
174+ ApplyCharacterFormat ( textRange ) ;
175+
176+ date = document . FindItemByProperty ( EntityType . InlineContentControl ,
177+ "ContentControlProperties.Title" , "StartDate" ) as InlineContentControl ;
178+ textRange = date . ParagraphItems [ 0 ] as WTextRange ;
179+ textRange . Text = DateTime . Now . AddDays ( - 6 ) . ToShortDateString ( ) ;
180+ ApplyCharacterFormat ( textRange ) ;
181+
182+ date = document . FindItemByProperty ( EntityType . InlineContentControl ,
183+ "ContentControlProperties.Title" , "EndDate" ) as InlineContentControl ;
184+ textRange = date . ParagraphItems [ 0 ] as WTextRange ;
185+ textRange . Text = DateTime . Now . AddDays ( - 1 ) . ToShortDateString ( ) ;
186+ ApplyCharacterFormat ( textRange ) ;
187+
188+ InlineContentControl inline = document . FindItemByProperty ( EntityType . InlineContentControl ,
189+ "ContentControlProperties.Title" , "ProjectName" ) as InlineContentControl ;
190+ inline . ContentControlProperties . LockContents = true ;
191+
192+ textRange = inline . ParagraphItems [ 0 ] as WTextRange ;
193+ textRange . Text = "Website for Adventure works cycle" ;
194+ ApplyCharacterFormat ( textRange ) ;
195+
196+ inline = document . FindItemByProperty ( EntityType . InlineContentControl ,
197+ "ContentControlProperties.Title" , "TeamName" ) as InlineContentControl ;
198+ textRange = inline . ParagraphItems [ 0 ] as WTextRange ;
199+ textRange . Text = "Adventure works cycle" ;
200+ ApplyCharacterFormat ( textRange ) ;
201+
202+ inline = document . FindItemByProperty ( EntityType . InlineContentControl ,
203+ "ContentControlProperties.Title" , "TeamSize" ) as InlineContentControl ;
204+ textRange = inline . ParagraphItems [ 0 ] as WTextRange ;
205+ textRange . Text = "10" ;
206+ ApplyCharacterFormat ( textRange ) ;
207+
208+ inline = document . FindItemByProperty ( EntityType . InlineContentControl ,
209+ "ContentControlProperties.Title" , "ProjectManager" ) as InlineContentControl ;
210+ textRange = inline . ParagraphItems [ 0 ] as WTextRange ;
211+ textRange . Text = "Nancy Davolio" ;
212+ ApplyCharacterFormat ( textRange ) ;
213+
214+ inline = document . FindItemByProperty ( EntityType . InlineContentControl ,
215+ "ContentControlProperties.Title" , "C#" ) as InlineContentControl ;
216+ inline . ContentControlProperties . IsChecked = true ;
217+ inline . ContentControlProperties . LockContentControl = true ;
218+
219+ inline = document . FindItemByProperty ( EntityType . InlineContentControl ,
220+ "ContentControlProperties.Title" , "Platform" ) as InlineContentControl ;
221+ inline . ContentControlProperties . LockContentControl = true ;
222+ inline . ContentControlProperties . LockContents = true ;
223+
224+ textRange = inline . ParagraphItems [ 0 ] as WTextRange ;
225+ textRange . Text = "ASP.NET" ;
226+ ApplyCharacterFormat ( textRange ) ;
227+
228+ MemoryStream stream = new MemoryStream ( ) ;
229+ document . Save ( stream , FormatType . Docx ) ;
230+ document . Close ( ) ;
231+ return File ( stream , "application/docx" , "FilledForm.docx" ) ;
232+ }
233+
234+ private void ApplyCharacterFormat ( WTextRange textRange )
235+ {
236+ textRange . CharacterFormat . FontName = "Century Gothic" ;
237+ textRange . CharacterFormat . FontSize = 12 ;
238+ textRange . CharacterFormat . TextColor = Color . Black ;
239+ }
240+ private void AddCheckbox ( WParagraph paragraph , string title )
241+ {
242+ InlineContentControl checkbox = paragraph . AppendInlineContentControl ( ContentControlType . CheckBox ) as InlineContentControl ;
243+ checkbox . ContentControlProperties . Title = title ;
244+ checkbox . ContentControlProperties . Tag = title ;
245+ checkbox . ContentControlProperties . IsChecked = false ;
246+ WTextRange text = paragraph . AppendText ( " " + title + " " ) as WTextRange ;
247+ ApplyCharacterFormat ( text , 12 , false , Color . Black ) ;
248+ }
249+ private void AddText ( WParagraph paragraph , string title )
250+ {
251+ InlineContentControl text = paragraph . AppendInlineContentControl ( ContentControlType . RichText ) as InlineContentControl ;
252+ text . ContentControlProperties . Title = title ;
253+ text . ContentControlProperties . Tag = title ;
254+ WTextRange textRange = new WTextRange ( paragraph . Document ) ;
255+ textRange . Text = "Click or tap to enter text" ;
256+ ApplyCharacterFormat ( textRange , 12 , false , Color . Gray ) ;
257+ text . ParagraphItems . Add ( textRange ) ;
258+ }
259+ private void AddDate ( WParagraph paragraph , string title )
260+ {
261+ InlineContentControl date = paragraph . AppendInlineContentControl ( ContentControlType . Date ) as InlineContentControl ;
262+ date . ContentControlProperties . Title = title ;
263+ date . ContentControlProperties . Tag = title ;
264+ date . ContentControlProperties . DateDisplayFormat = "M/d/yyyy" ;
265+ WTextRange textRange = new WTextRange ( paragraph . Document ) ;
266+ textRange . Text = "Click or tap to enter date" ;
267+ ApplyCharacterFormat ( textRange , 12 , false , Color . Gray ) ;
268+ date . ParagraphItems . Add ( textRange ) ;
269+ }
270+
271+ private void AddDropDown ( WParagraph paragraph , string title , string [ ] items )
272+ {
273+ InlineContentControl dropdown = paragraph . AppendInlineContentControl ( ContentControlType . DropDownList ) as InlineContentControl ;
274+ dropdown . ContentControlProperties . Title = title ;
275+ WTextRange textRange = new WTextRange ( paragraph . Document ) ;
276+ textRange . Text = "Choose an item" ;
277+ ApplyCharacterFormat ( textRange , 12 , false , Color . Gray ) ;
278+ dropdown . ParagraphItems . Add ( textRange ) ;
279+ int i = 1 ;
280+ foreach ( string itemName in items )
281+ {
282+ ContentControlListItem item = new ContentControlListItem ( ) ;
283+ item . DisplayText = itemName ;
284+ item . Value = i . ToString ( ) ;
285+ i ++ ;
286+ dropdown . ContentControlProperties . ContentControlListItems . Add ( item ) ;
287+ }
288+ }
289+
290+ private void ApplyCharacterFormat ( WTextRange textRange , float size , bool isBold , Color color )
291+ {
292+ textRange . CharacterFormat . FontName = "Century Gothic" ;
293+ textRange . CharacterFormat . FontSize = size ;
294+ textRange . CharacterFormat . Bold = isBold ;
295+ textRange . CharacterFormat . TextColor = color ;
296+ }
297+ public IActionResult Privacy ( )
298+ {
299+ return View ( ) ;
300+ }
301+
302+ [ ResponseCache ( Duration = 0 , Location = ResponseCacheLocation . None , NoStore = true ) ]
303+ public IActionResult Error ( )
304+ {
305+ return View ( new ErrorViewModel { RequestId = Activity . Current ? . Id ?? HttpContext . TraceIdentifier } ) ;
306+ }
307+ }
308+ }
0 commit comments