/// <summary> /// Creates word document with built - in styles /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, System.EventArgs e) { try { WordDocument document = new WordDocument(); WSection section = document.AddSection() as WSection; WParagraph para = section.AddParagraph() as WParagraph; section.AddColumn(100, 100); section.AddColumn(100, 100); section.MakeColumnsEqual(); #region Built-in styles # region List Style //List para.AppendText("This para is written with style List").CharacterFormat.UnderlineStyle = UnderlineStyle.Double; para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.List); para.AppendText("Google Chrome\n"); para.AppendText("Mozilla Firefox\n"); para.AppendText("Internet Explorer"); //List5 style para = section.AddParagraph() as WParagraph; para.AppendText("\nThis para is written with style List5").CharacterFormat.UnderlineStyle = UnderlineStyle.Double; para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.List5); para.AppendText("Google Chrome\n"); para.AppendText("Mozilla Firefox\n"); para.AppendText("Internet Explorer"); # endregion # region ListNumber Style
private static void AddHeading(WSection section, string styleName, string headingText, string paragraghText) { WParagraph newPara = section.AddParagraph() as WParagraph; WTextRange text = newPara.AppendText(headingText) as WTextRange; newPara.ApplyStyle(styleName); newPara = section.AddParagraph() as WParagraph; newPara.AppendText(paragraghText); section.AddParagraph(); }
static void Main(string[] args) { using (WordDocument document = new WordDocument()) { document.EnsureMinimal(); document.LastSection.PageSetup.Margins.All = 72; WParagraph para = document.LastParagraph; para.AppendText("Essential DocIO - Table of Contents"); para.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center; para.ApplyStyle(BuiltinStyle.Heading4); para = document.LastSection.AddParagraph() as WParagraph; //Creates the new custom styles. WParagraphStyle pStyle1 = (WParagraphStyle)document.AddParagraphStyle("MyStyle1"); pStyle1.CharacterFormat.FontSize = 18f; WParagraphStyle pStyle2 = (WParagraphStyle)document.AddParagraphStyle("MyStyle2"); pStyle2.CharacterFormat.FontSize = 16f; WParagraphStyle pStyle3 = (WParagraphStyle)document.AddParagraphStyle("MyStyle3"); pStyle3.CharacterFormat.FontSize = 14f; para = document.LastSection.AddParagraph() as WParagraph; //Insert TOC field in the Word document. TableOfContent toc = para.AppendTOC(1, 3); //Sets the Heading Styles to false, to define custom levels for TOC. toc.UseHeadingStyles = false; //Sets the TOC level style which determines; based on which the TOC should be created. toc.SetTOCLevelStyle(1, "MyStyle1"); toc.SetTOCLevelStyle(2, "MyStyle2"); toc.SetTOCLevelStyle(3, "MyStyle3"); //Adds content to the Word document with custom styles. WSection section = document.LastSection; WParagraph newPara = section.AddParagraph() as WParagraph; newPara.AppendBreak(BreakType.PageBreak); AddHeading(section, "MyStyle1", "Document with custom styles", "This is the 1st custom style. This sample demonstrates the TOC insertion in a word document. Note that DocIO can insert TOC field in a word document. It can refresh or update TOC field by using UpdateTableOfContents method. MS Word refreshes the TOC field after insertion. Please update the field or press F9 key to refresh the TOC."); AddHeading(section, "MyStyle2", "Section 1", "This is the 2nd custom style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); AddHeading(section, "MyStyle3", "Paragraph 1", "This is the 3rd custom style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives a meaning for the text."); AddHeading(section, "MyStyle3", "Paragraph 2", "This is the 3rd custom style. This demonstrates the paragraphs at the same level and style as that of the previous one. A paragraph can have any number formatting. This can be attained by formatting each text range in the paragraph."); AddHeading(section, "Normal", "Paragraph with normal", "This is the paragraph with normal style. This demonstrates the paragraph with outline level 4 and normal style. This can be attained by formatting outline level of the paragraph."); //Adds a new section to the Word document. section = document.AddSection() as WSection; section.PageSetup.Margins.All = 72; section.BreakCode = SectionBreakCode.NewPage; AddHeading(section, "MyStyle2", "Section 2", "This is the 2nd custom style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); AddHeading(section, "MyStyle3", "Paragraph 1", "This is the 3rd custom style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives a meaning for the text."); AddHeading(section, "MyStyle3", "Paragraph 2", "This is the 3rd custom style. This demonstrates the paragraphs at the same level and style as that of the previous one. A paragraph can have any number formatting. This can be attained by formatting each text range in the paragraph."); //Updates the table of contents. document.UpdateTableOfContents(); //Saves the file in the given path Stream docStream = File.Create(Path.GetFullPath(@"../../../TOC-custom-style.docx")); document.Save(docStream, FormatType.Docx); docStream.Dispose(); } }
static void Main(string[] args) { using (WordDocument document = new WordDocument()) { document.EnsureMinimal(); document.LastSection.PageSetup.Margins.All = 72; WParagraph para = document.LastParagraph; para.AppendText("Essential DocIO - Table of Contents"); para.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center; para.ApplyStyle(BuiltinStyle.Heading4); para = document.LastSection.AddParagraph() as WParagraph; para = document.LastSection.AddParagraph() as WParagraph; //Insert TOC field in the Word document. TableOfContent toc = para.AppendTOC(1, 3); //Sets the heading levels 1 to 3, to include in TOC. toc.LowerHeadingLevel = 1; toc.UpperHeadingLevel = 3; //Adds content to the Word document with built-in heading styles. WSection section = document.LastSection; WParagraph newPara = section.AddParagraph() as WParagraph; newPara.AppendBreak(BreakType.PageBreak); AddHeading(section, BuiltinStyle.Heading1, "Document with built-in heading styles", "This is the built-in heading 1 style. This sample demonstrates the TOC insertion in a word document. Note that DocIO can insert TOC field in a word document. It can refresh or update TOC field by using UpdateTableOfContents method. MS Word refreshes the TOC field after insertion. Please update the field or press F9 key to refresh the TOC."); AddHeading(section, BuiltinStyle.Heading2, "Section 1", "This is the built-in heading 2 style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); AddHeading(section, BuiltinStyle.Heading3, "Paragraph 1", "This is the built-in heading 3 style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives a meaning for the text."); AddHeading(section, BuiltinStyle.Heading3, "Paragraph 2", "This is the built-in heading 3 style. This demonstrates the paragraphs at the same level and style as that of the previous one. A paragraph can have any number formatting. This can be attained by formatting each text range in the paragraph."); //Adds a new section to the Word document. section = document.AddSection() as WSection; section.PageSetup.Margins.All = 72; section.BreakCode = SectionBreakCode.NewPage; AddHeading(section, BuiltinStyle.Heading2, "Section 2", "This is the built-in heading 2 style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); AddHeading(section, BuiltinStyle.Heading3, "Paragraph 1", "This is the built-in heading 3 style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives a meaning for the text."); AddHeading(section, BuiltinStyle.Heading3, "Paragraph 2", "This is the built-in heading 3 style. This demonstrates the paragraphs at the same level and style as that of the previous one. A paragraph can have any number formatting. This can be attained by formatting each text range in the paragraph."); //Updates the table of contents. document.UpdateTableOfContents(); //Saves the file in the given path Stream docStream = File.Create(Path.GetFullPath(@"../../../TOC-creation.docx")); document.Save(docStream, FormatType.Docx); docStream.Dispose(); } }
protected void Button1_Click(object sender, EventArgs e) { WordDocument doc = new WordDocument(); doc.EnsureMinimal(); WParagraph para = doc.LastParagraph; para.AppendText("Essential DocIO - Table of Contents"); para.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; para.ApplyStyle(BuiltinStyle.Heading4); para = doc.LastSection.AddParagraph() as WParagraph; para.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; para.ApplyStyle(BuiltinStyle.Heading4); if (!this.CheckBox7.Checked) { para.AppendText("Select TOC and press F9 to update the Table of Contents").CharacterFormat.HighlightColor = Color.Yellow; } para = doc.LastSection.AddParagraph() as WParagraph; string title = this.TextBox1.Text + "\n"; para.AppendText(title); para.ApplyStyle(BuiltinStyle.Heading4); //Insert TOC TableOfContent toc = para.AppendTOC(1, 3); para.ApplyStyle(BuiltinStyle.Heading4); //Apply built-in paragraph formatting WSection section = doc.LastSection; if (this.RadioButton1.Checked) { #region Default Styles WParagraph newPara = section.AddParagraph() as WParagraph; newPara = section.AddParagraph() as WParagraph; newPara.AppendBreak(BreakType.PageBreak); WTextRange text = newPara.AppendText("Document with Default styles") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading1); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading1 of built in style. This sample demonstrates the TOC insertion in a word document. Note that DocIO can only insert TOC field in a word document. It can not refresh or create TOC field. MS Word refreshes the TOC field after insertion. Please update the field or press F9 key to refresh the TOC."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Section1") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading2); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading2 of built in style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph1") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives a meaning for the text."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph2") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. This demonstrates the paragraphs at the same level and style as that of the previous one. A paragraph can have any number formatting. This can be attained by formatting each text range in the paragraph."); section.AddParagraph(); section = doc.AddSection() as WSection; section.BreakCode = SectionBreakCode.NewPage; newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Section2") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading2); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading2 of built in style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph1") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives a meaning for the text."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph2") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. This demonstrates the paragraphs at the same level and style as that of the previous one. A paragraph can have any number formatting. This can be attained by formatting each text range in the paragraph."); #endregion } else { #region Custom styles //Custom styles. WParagraphStyle pStyle1 = (WParagraphStyle)doc.AddParagraphStyle("MyStyle1"); WParagraphStyle pStyle2 = (WParagraphStyle)doc.AddParagraphStyle("MyStyle2"); WParagraphStyle pStyle3 = (WParagraphStyle)doc.AddParagraphStyle("MyStyle3"); //Set the Heading Styles to false in order to define custom levels to TOC. toc.UseHeadingStyles = false; //Set the TOC level style which determines; based on which the TOC should be created. toc.SetTOCLevelStyle(1, "MyStyle1"); toc.SetTOCLevelStyle(2, "MyStyle2"); toc.SetTOCLevelStyle(3, "MyStyle3"); section = doc.AddSection() as WSection; pStyle1.CharacterFormat.FontName = "Cambria"; pStyle1.CharacterFormat.FontSize = 30f; para = section.AddParagraph() as WParagraph; WTextRange text = para.AppendText("Document with Custom Styles") as WTextRange; para.ApplyStyle("MyStyle1"); para = doc.LastSection.AddParagraph() as WParagraph; para.AppendText("This is the heading1 of built in style. This sample demonstrates the TOC insertion in a word document. Note that DocIO can only insert TOC field in a word document. It can not refresh or create TOC field. MS Word refreshes the TOC field after insertion. Please update the field or press F9 key to refresh the TOC."); pStyle2.CharacterFormat.FontName = "Cambria"; pStyle2.CharacterFormat.FontSize = 20f; doc.LastSection.AddParagraph(); para = doc.LastSection.AddParagraph() as WParagraph; text = para.AppendText("Section1") as WTextRange; para.ApplyStyle("MyStyle2"); para = doc.LastSection.AddParagraph() as WParagraph; para.AppendText("This is the heading2 of built in style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); pStyle3.CharacterFormat.FontName = "Cambria"; pStyle3.CharacterFormat.FontSize = 14f; doc.LastSection.AddParagraph(); para = doc.LastSection.AddParagraph() as WParagraph; text = para.AppendText("Section2") as WTextRange; para.ApplyStyle("MyStyle3"); para = doc.LastSection.AddParagraph() as WParagraph; para.AppendText("This is the heading2 of built in style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); #endregion } if (Convert.ToInt32(this.DropDownList2.Text) < Convert.ToInt32(this.DropDownList1.Text)) { Response.Write("Not a valid heading level range. UpperHeadingLevel must be greater than LowerHeadingLevel"); } else { toc.IncludePageNumbers = this.CheckBox2.Checked; toc.RightAlignPageNumbers = this.CheckBox3.Checked; toc.UseHyperlinks = this.CheckBox4.Checked; toc.LowerHeadingLevel = Convert.ToInt32(this.DropDownList1.Text); toc.UpperHeadingLevel = Convert.ToInt32(this.DropDownList2.Text); //Used to set levels for a word or paragraph through OutLine Levels //Right click text. Select Paragraph option. Set OutlineLevel. Update TOC toc see the text added in TOC. toc.UseOutlineLevels = this.CheckBox5.Checked; //Used to set levels for a word or paragraph through Table Entry Fields //Select the text that should be marked as Table of contents. //Press ALT+SHIFT+O. A dialog box will appear with options to enter the text, select the table identifier and level. //Choose the table identifier and level for the test and click �Mark�. Update TOC toc see the text added in TOC. //Sets the Table Identifier if necessary. //toc.TableID = "B"; toc.UseTableEntryFields = this.CheckBox6.Checked; //Updates the table of contents. if (this.CheckBox7.Checked) { doc.UpdateTableOfContents(); } if (rdButtonDoc.Checked) { //Save as .doc format doc.Save("Sample.doc", FormatType.Doc, Response, HttpContentDisposition.Attachment); } //Save as .docx format else if (rdButtonDocx.Checked) { try { doc.Save("Sample.docx", FormatType.Docx, Response, HttpContentDisposition.Attachment); } catch (Win32Exception ex) { Response.Write("Microsoft Word Viewer or Microsoft Word is not installed in this system"); Console.WriteLine(ex.ToString()); } } //Save as WordML(.xml) format if (rdButtonWordML.Checked) { try { doc.Save("Sample.xml", FormatType.WordML, Response, HttpContentDisposition.Attachment); } catch (Win32Exception ex) { Response.Write("Microsoft Word Viewer or Microsoft Word is not installed in this system"); Console.WriteLine(ex.ToString()); } } //Save as .pdf format else if (rdButtonPdf.Checked) { try { DocToPDFConverter converter = new DocToPDFConverter(); PdfDocument pdfDoc = converter.ConvertToPDF(doc); pdfDoc.Save("Sample.pdf", Response, HttpReadType.Save); } catch (Win32Exception ex) { Response.Write("PDF Viewer is not installed in this system"); Console.WriteLine(ex.ToString()); } } } }
/// <summary> /// Generate update table of contents functionality of Essential DocIO /// </summary> /// <returns>Return the created Word document as stream</returns> public MemoryStream TableofContent(string documentType, string button, bool UpdateTOC) { WordDocument doc = new WordDocument(); doc.EnsureMinimal(); WParagraph para = doc.LastParagraph; para.AppendText("Essential DocIO - Table of Contents"); para.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; para.ApplyStyle(BuiltinStyle.Heading4); para = doc.LastSection.AddParagraph() as WParagraph; para.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; para.ApplyStyle(BuiltinStyle.Heading4); if (!UpdateTOC) { para.AppendText("Select TOC and press F9 to update the Table of Contents").CharacterFormat.HighlightColor = Syncfusion.Drawing.Color.Yellow; } para = doc.LastSection.AddParagraph() as WParagraph; //Insert TOC TableOfContent toc = para.AppendTOC(1, 3); para.ApplyStyle(BuiltinStyle.Heading4); //Apply built-in paragraph formatting WSection section = doc.LastSection; #region Default Styles WParagraph newPara = section.AddParagraph() as WParagraph; newPara = section.AddParagraph() as WParagraph; newPara.AppendBreak(BreakType.PageBreak); WTextRange text = newPara.AppendText("Document with Default styles") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading1); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading1 of built in style. This sample demonstrates the TOC insertion in a word document. Note that DocIO can only insert TOC field in a word document. It can not refresh or create TOC field. MS Word refreshes the TOC field after insertion. Please update the field or press F9 key to refresh the TOC."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Section1") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading2); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading2 of built in style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph1") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives a meaning for the text."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph2") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. This demonstrates the paragraphs at the same level and style as that of the previous one. A paragraph can have any number formatting. This can be attained by formatting each text range in the paragraph."); section.AddParagraph(); section = doc.AddSection() as WSection; section.BreakCode = SectionBreakCode.NewPage; newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Section2") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading2); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading2 of built in style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph1") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives a meaning for the text."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph2") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. This demonstrates the paragraphs at the same level and style as that of the previous one. A paragraph can have any number formatting. This can be attained by formatting each text range in the paragraph."); #endregion toc.IncludePageNumbers = true; toc.RightAlignPageNumbers = true; toc.UseHyperlinks = true; toc.LowerHeadingLevel = 1; toc.UpperHeadingLevel = 3; toc.UseOutlineLevels = true; //Updates the table of contents. if (UpdateTOC) { doc.UpdateTableOfContents(); } FormatType formatType = FormatType.Docx; //Save as .doc format if (documentType == "WordDoc") { formatType = FormatType.Doc; } //Save as .xml format else if (documentType == "WordML") { formatType = FormatType.WordML; } //Save the document as a stream and retrun the stream using (MemoryStream stream = new MemoryStream()) { //Save the created Word document to MemoryStream doc.Save(stream, formatType); doc.Close(); stream.Position = 0; return(stream); } }
void OnButtonClicked(object sender, EventArgs e) { WordDocument doc = new WordDocument(); doc.EnsureMinimal(); WParagraph para = doc.LastParagraph; para.AppendText("Essential DocIO - Table of Contents"); para.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; para.ApplyStyle(BuiltinStyle.Heading4); para = doc.LastSection.AddParagraph() as WParagraph; para.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; para.ApplyStyle(BuiltinStyle.Heading4); para = doc.LastSection.AddParagraph() as WParagraph; //Insert TOC TableOfContent toc = para.AppendTOC(1, 3); para.ApplyStyle(BuiltinStyle.Heading4); //Apply built-in paragraph formatting WSection section = doc.LastSection; #region Default Styles WParagraph newPara = section.AddParagraph() as WParagraph; newPara = section.AddParagraph() as WParagraph; newPara.AppendBreak(BreakType.PageBreak); WTextRange text = newPara.AppendText("Document with Default styles") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading1); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading1 of built in style. This sample demonstrates the TOC insertion in a word document. Note that DocIO can only insert TOC field in a word document. It can not refresh or create TOC field. MS Word refreshes the TOC field after insertion. Please update the field or press F9 key to refresh the TOC."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Section1") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading2); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading2 of built in style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph1") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives a meaning for the text."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph2") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. This demonstrates the paragraphs at the same level and style as that of the previous one. A paragraph can have any number formatting. This can be attained by formatting each text range in the paragraph."); section.AddParagraph(); section = doc.AddSection() as WSection; section.BreakCode = SectionBreakCode.NewPage; newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Section2") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading2); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading2 of built in style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph1") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives a meaning for the text."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph2") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. This demonstrates the paragraphs at the same level and style as that of the previous one. A paragraph can have any number formatting. This can be attained by formatting each text range in the paragraph."); #endregion toc.IncludePageNumbers = true; toc.RightAlignPageNumbers = true; toc.UseHyperlinks = true; toc.LowerHeadingLevel = 1; toc.UpperHeadingLevel = 3; toc.UseOutlineLevels = true; //Updates the table of contents. doc.UpdateTableOfContents(); string filename = ""; string contenttype = ""; MemoryStream outputStream = new MemoryStream(); if (pdfButton != null && (bool)pdfButton.IsChecked) { filename = "Table Of Contents.pdf"; contenttype = "application/pdf"; DocIORenderer renderer = new DocIORenderer(); PdfDocument pdfDoc = renderer.ConvertToPDF(doc); pdfDoc.Save(outputStream); pdfDoc.Close(); } else { filename = "Table Of Contents.docx"; contenttype = "application/msword"; doc.Save(outputStream, FormatType.Docx); } doc.Close(); if (Device.RuntimePlatform == Device.UWP) { DependencyService.Get <ISaveWindowsPhone>() .Save(filename, contenttype, outputStream); } else { DependencyService.Get <ISave>().Save(filename, contenttype, outputStream); } }
public async Task <IActionResult> GenerateDocument(int Id, string tokenId) { var LabTest = _context.TblLabTests .Include(x => x.MethodNavigation) .Include(x => x.BiodataNavigation) .Include(x => x.BiodataNavigation.GenderNavigation) .Include(x => x.TblLabTestsIndicatorsValues) .Include(x => x.TblLabTestsSpecimen) .FirstOrDefault(x => x.Id == Id); if (LabTest == null) { return(NotFound()); } //Loads or opens an existing word document through Open method of WordDocument class Stream fs = new FileStream("Templates/result.docx", FileMode.Open, FileAccess.Read, FileShare.Read); WordDocument document = new WordDocument(fs, FormatType.Docx); if (document.Sections.Count < 1) { throw new Exception("Result template empty. Please contact your Application support."); } IWSection section = document.Sections[0]; IWTable table = section.Tables[0]; WParagraph p = (WParagraph)table.Rows[0].Cells[1].ChildEntities[0]; p.AppendText(LabTest.BiodataNavigation.Fullname); p = (WParagraph)table.Rows[1].Cells[1].ChildEntities[0]; string gardian = LabTest.BiodataNavigation.LegalGardianName; gardian = string.IsNullOrEmpty(gardian) ? "-" : gardian; p.AppendText(gardian); p = (WParagraph)table.Rows[2].Cells[1].ChildEntities[0]; p.AppendText(LabTest.BiodataNavigation.Dateofbirth.ToString("dd/MMM/yyyy")); //skip 3 //Repos.GenderRepos genders = new Repos.GenderRepos(); int g_id = 1; foreach (var g in _context.TlkpGenders) { string tmp = g.Gender.Substring(0, 2).Trim().Trim('='); try { p = (WParagraph)table.Rows[3].Cells[g_id].ChildEntities[0]; WCheckBox checkbox = p.AppendCheckBox(); if (LabTest.BiodataNavigation.Gender == g_id) { checkbox.Checked = true; } p.AppendText(" " + tmp); g_id++; } catch { } } p = (WParagraph)table.Rows[4].Cells[1].ChildEntities[0]; p.AppendText(LabTest.BiodataNavigation.EpidNo); //skip 5 for local phone number p = (WParagraph)table.Rows[5].Cells[1].ChildEntities[0]; p.AppendText(LabTest.BiodataNavigation.LocalPhone ?? "-"); p = (WParagraph)table.Rows[6].Cells[1].ChildEntities[0]; p.AppendText(LabTest.BiodataNavigation.HomePhone ?? "-"); p = (WParagraph)table.Rows[7].Cells[1].ChildEntities[0]; p.AppendText(LabTest.BiodataNavigation.ResidentialAddress); //next table table = section.Tables[1]; p = (WParagraph)table.Rows[0].Cells[1].ChildEntities[0]; p.AppendText(LabTest.MethodNavigation.Methodname); int k = 1; foreach (var i in LabTest.TblLabTestsIndicatorsValues) { var iname = _context.TlkpTestIndicators.FirstOrDefault(x => x.Id == i.Indicator).IndicatorName; p = (WParagraph)table.Rows[k].Cells[0].ChildEntities[0]; p.AppendText(iname); p = (WParagraph)table.Rows[k].Cells[1].ChildEntities[0]; p.AppendText(i.IndicatorValue.Value.ToString()); k++; } /*Dictionary<int, string> _dict = new Dictionary<int, string> { * { 1, "POSITIVE"}, * { 2, "NEGATIVE"}, * { 97, "NO RESULT"} * };*/ var p1 = (WParagraph)table.Rows[k].Cells[0].ChildEntities[0]; p = (WParagraph)table.Rows[k].Cells[1].ChildEntities[0]; WCheckBox checkbox1 = p.AppendCheckBox(); if (LabTest.Interpretation == 2) { checkbox1.Checked = true; p1.AppendText("INTERPRETATION: NEGATIVE RESULT"); } else if (LabTest.Interpretation > 2) { p1.AppendText("INTERPRETATION: UNKNOWN"); } p.AppendText(" NEGATIVE"); p = (WParagraph)table.Rows[k + 1].Cells[1].ChildEntities[0]; checkbox1 = p.AppendCheckBox(); if (LabTest.Interpretation == 1) { checkbox1.Checked = true; p1.AppendText("INTERPRETATION: POSITIVE RESULT"); } p.AppendText(" POSITIVE"); //next table table = section.Tables[2]; DateTime d1 = DateTime.Today; // any date will do p = (WParagraph)table.Rows[0].Cells[1].ChildEntities[0]; p.AppendText(LabTest.TestingDate.Value.ToString("dd/MMM/yyyy")); p = (WParagraph)table.Rows[1].Cells[1].ChildEntities[0]; TimeSpan t = LabTest.TestingTime.Value; string chg = (d1 + t).ToString("hh:mm tt"); p.AppendText(chg); p = (WParagraph)table.Rows[2].Cells[1].ChildEntities[0]; p.AppendText(LabTest.ReportingDate.Value.ToString("dd/MMM/yyyy")); p = (WParagraph)table.Rows[3].Cells[1].ChildEntities[0]; t = LabTest.ReportingTime.Value; chg = (d1 + t).ToString("hh:mm tt"); p.AppendText(chg); //next table table = section.Tables[3]; //Repos.SpecimenRepos _specimen = new Repos.SpecimenRepos(); k = 1; foreach (var s in LabTest.TblLabTestsSpecimen) { bool other = s.Specimen == 99; var sname = _context.TlkpSpecimen.FirstOrDefault(x => x.Id == s.Specimen).Type; p = (WParagraph)table.Rows[k].Cells[0].ChildEntities[0]; IWTextRange textRange = p.AppendText(sname + (other ? " (Specify)" : "")); textRange.CharacterFormat.FontSize = 8; p.ApplyStyle(BuiltinStyle.BlockText); p = (WParagraph)table.Rows[k].Cells[1].ChildEntities[0]; if (other && !string.IsNullOrEmpty(s.SpecimenOther)) { textRange = p.AppendText(s.SpecimenOther); textRange.CharacterFormat.FontSize = 10; } p = (WParagraph)table.Rows[k].Cells[2].ChildEntities[0]; checkbox1 = p.AppendCheckBox(); if (s.Checked) { checkbox1.Checked = true; } k++; } // //table = section.Tables[1]; // p = (WParagraph)table.Rows[6].Cells[1].ChildEntities[0]; //p.AppendText(LabTest.BioData.ResidentialAddress); DocIORenderer render = new DocIORenderer(); //Converts Word document to PDF. PdfDocument pdfDocument = render.ConvertToPDF(document); //Release the resources used by the Word document and DocIO Renderer objects. render.Dispose(); render.Dispose(); document.Dispose(); //add barcode PdfQRBarcode barcode = new PdfQRBarcode(); barcode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.High; //Set XDimension barcode.XDimension = 2.5f; //https://localhost:44353/LabTests/Details/ string endpoint = configuration.GetConnectionString("ServerEndpoint"); barcode.Text = string.Format("{0}LabTests/Details/{1}", endpoint, Id); //Creating new PDF Document //PdfDocument doc = new PdfDocument(); //Adding new page to PDF document PdfPage page = pdfDocument.Pages[0]; //Printing barcode on to the Pdf. //barcode.Draw(page, new PointF(25, 70)); barcode.Draw(page, new PointF(250, 650)); if (!string.IsNullOrEmpty(tokenId)) { Response.Cookies.Append("fileDownloadToken", tokenId); } MemoryStream stream = new MemoryStream(); //document.Save(stream, FormatType.Docx); pdfDocument.Save(stream); //return File(stream, "application/msword", "Sample.docx"); return(File(stream.ToArray(), "application/octet-stream", "Draft.pdf")); //return RedirectToAction(nameof(Index)); }
private void button1_Click(object sender, EventArgs e) { try { WordDocument doc = new WordDocument(); doc.EnsureMinimal(); WParagraph para = doc.LastParagraph; para.AppendText("Essential DocIO - Table of Contents"); para.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; para.ApplyStyle(BuiltinStyle.Heading4); para = doc.LastSection.AddParagraph() as WParagraph; para.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; para.ApplyStyle(BuiltinStyle.Heading4); if (!this.checkBox6.Checked) { para.AppendText("Select TOC and press F9 to update the Table of Contents").CharacterFormat.HighlightColor = Color.Yellow; } para = doc.LastSection.AddParagraph() as WParagraph; string title = this.textBox1.Text + "\n"; para.AppendText(title); para.ApplyStyle(BuiltinStyle.Heading4); //Insert TOC TableOfContent toc = para.AppendTOC(1, 3); para.ApplyStyle(BuiltinStyle.Heading4); //Apply built-in paragraph formatting WSection section = doc.LastSection; // Set Margin of the document section.PageSetup.Margins.All = 72; if (radioButton1.Checked) { #region Default Styles WParagraph newPara = section.AddParagraph() as WParagraph; newPara = section.AddParagraph() as WParagraph; newPara.AppendBreak(BreakType.PageBreak); WTextRange text = newPara.AppendText("Document with Default styles") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading1); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading1 of built in style. This sample demonstrates the TOC insertion in a word document. Note that DocIO can only insert TOC field in a word document. It can not refresh or create TOC field. MS Word refreshes the TOC field after insertion. Please update the field or press F9 key to refresh the TOC."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Section1") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading2); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading2 of built in style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph1") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives a meaning for the text."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph2") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. This demonstrates the paragraphs at the same level and style as that of the previous one. A paragraph can have any number formatting. This can be attained by formatting each text range in the paragraph."); section.AddParagraph(); section = doc.AddSection() as WSection; section.BreakCode = SectionBreakCode.NewPage; newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Section2") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading2); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading2 of built in style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph1") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives a meaning for the text."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph2") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. This demonstrates the paragraphs at the same level and style as that of the previous one. A paragraph can have any number formatting. This can be attained by formatting each text range in the paragraph."); #endregion } else { #region Custom styles //Custom styles. WParagraphStyle pStyle1 = (WParagraphStyle)doc.AddParagraphStyle("MyStyle1"); WParagraphStyle pStyle2 = (WParagraphStyle)doc.AddParagraphStyle("MyStyle2"); WParagraphStyle pStyle3 = (WParagraphStyle)doc.AddParagraphStyle("MyStyle3"); //Set the Heading Styles to false in order to define custom levels to TOC. toc.UseHeadingStyles = false; //Set the TOC level style which determines; based on which the TOC should be created. toc.SetTOCLevelStyle(1, "MyStyle1"); toc.SetTOCLevelStyle(2, "MyStyle2"); toc.SetTOCLevelStyle(3, "MyStyle3"); section = doc.AddSection() as WSection; pStyle1.CharacterFormat.FontName = "Cambria"; pStyle1.CharacterFormat.FontSize = 30f; para = section.AddParagraph() as WParagraph; WTextRange text = para.AppendText("Document with Custom Styles") as WTextRange; para.ApplyStyle("MyStyle1"); para = doc.LastSection.AddParagraph() as WParagraph; para.AppendText("This is the heading1 of built in style. This sample demonstrates the TOC insertion in a word document. Note that DocIO can only insert TOC field in a word document. It can not refresh or create TOC field. MS Word refreshes the TOC field after insertion. Please update the field or press F9 key to refresh the TOC."); pStyle2.CharacterFormat.FontName = "Cambria"; pStyle2.CharacterFormat.FontSize = 20f; doc.LastSection.AddParagraph(); para = doc.LastSection.AddParagraph() as WParagraph; text = para.AppendText("Section1") as WTextRange; para.ApplyStyle("MyStyle2"); para = doc.LastSection.AddParagraph() as WParagraph; para.AppendText("This is the heading2 of built in style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); pStyle3.CharacterFormat.FontName = "Cambria"; pStyle3.CharacterFormat.FontSize = 14f; doc.LastSection.AddParagraph(); para = doc.LastSection.AddParagraph() as WParagraph; text = para.AppendText("Section2") as WTextRange; para.ApplyStyle("MyStyle3"); para = doc.LastSection.AddParagraph() as WParagraph; para.AppendText("This is the heading2 of built in style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); #endregion } if (this.numericUpDown2.Value < this.numericUpDown1.Value) { MessageBoxAdv.Show("Not a valid heading level range. UpperHeadingLevel must be greater than LowerHeadingLevel"); } else { toc.IncludePageNumbers = checkBox1.Checked; toc.RightAlignPageNumbers = checkBox2.Checked; toc.UseHyperlinks = checkBox3.Checked; toc.LowerHeadingLevel = Convert.ToInt32(this.numericUpDown1.Value); toc.UpperHeadingLevel = Convert.ToInt32(this.numericUpDown2.Value); //Right click text. Select Paragraph option. Set OutlineLevel. Update TOC toc see the text added in TOC. toc.UseOutlineLevels = this.checkBox4.Checked; //Select the text that should be marked as Table of contents. //Press ALT+SHIFT+O. A dialog box will appear with options to enter the text, select the table identifier and level. //Choose the table identifier and level for the test and click �Mark�. Update TOC toc see the text added in TOC. //Sets the Table Identifier if necessary. //toc.TableID = "B"; toc.UseTableEntryFields = this.checkBox5.Checked; //Updates the table of contents. if (this.checkBox6.Checked) { doc.UpdateTableOfContents(); } //Save as doc format if (wordDocRadioBtn.Checked) { //Saving the document to disk. doc.Save("Sample.doc"); //Message box confirmation to view the created document. if (MessageBoxAdv.Show("Do you want to view the generated Word document?", "Document has been created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { //Launching the MS Word file using the default Application.[MS Word Or Free WordViewer] #if NETCORE System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo = new System.Diagnostics.ProcessStartInfo("Sample.doc") { UseShellExecute = true }; process.Start(); #else System.Diagnostics.Process.Start("Sample.doc"); #endif //Exit this.Close(); } } //Save as docx format else if (wordDocxRadioBtn.Checked) { //Saving the document as .docx doc.Save("Sample.docx", FormatType.Docx); //Message box confirmation to view the created document. if (MessageBoxAdv.Show("Do you want to view the generated Word document?", "Document has been created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { try { //Launching the MS Word file using the default Application.[MS Word Or Free WordViewer] #if NETCORE System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo = new System.Diagnostics.ProcessStartInfo("Sample.docx") { UseShellExecute = true }; process.Start(); #else System.Diagnostics.Process.Start("Sample.docx"); #endif //Exit this.Close(); } catch (Win32Exception ex) { MessageBoxAdv.Show("Microsoft Word Viewer or Microsoft Word is not installed in this system"); Console.WriteLine(ex.ToString()); } } } //Save as pdf format else if (pdfRadioBtn.Checked) { DocToPDFConverter converter = new DocToPDFConverter(); //Convert word document into PDF document PdfDocument pdfDoc = converter.ConvertToPDF(doc); //Save the pdf file pdfDoc.Save("Sample.pdf"); //Message box confirmation to view the created document. if (MessageBoxAdv.Show("Do you want to view the generated PDF?", " Document has been created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { try { #if NETCORE System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo = new System.Diagnostics.ProcessStartInfo("Sample.pdf") { UseShellExecute = true }; process.Start(); #else System.Diagnostics.Process.Start("Sample.pdf"); #endif //Exit this.Close(); } catch (Exception ex) { MessageBoxAdv.Show("PDF Viewer is not installed in this system"); Console.WriteLine(ex.ToString()); } } } else { // Exit this.Close(); } } } catch (Exception Ex) { MessageBox.Show(Ex.Message); } }
public ActionResult TableofContent(string Group1, string UpdateTOC) { if (Group1 == null) { return(View()); } WordDocument doc = new WordDocument(); doc.EnsureMinimal(); WParagraph para = doc.LastParagraph; para.AppendText("Essential DocIO - Table of Contents"); para.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; para.ApplyStyle(BuiltinStyle.Heading4); para = doc.LastSection.AddParagraph() as WParagraph; para.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; para.ApplyStyle(BuiltinStyle.Heading4); if (UpdateTOC != "Update") { para.AppendText("Select TOC and press F9 to update the Table of Contents").CharacterFormat.HighlightColor = Color.Yellow; } para = doc.LastSection.AddParagraph() as WParagraph; //Insert TOC TableOfContent toc = para.AppendTOC(1, 3); para.ApplyStyle(BuiltinStyle.Heading4); //Apply built-in paragraph formatting WSection section = doc.LastSection; #region Default Styles WParagraph newPara = section.AddParagraph() as WParagraph; newPara = section.AddParagraph() as WParagraph; newPara.AppendBreak(BreakType.PageBreak); WTextRange text = newPara.AppendText("Document with Default styles") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading1); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading1 of built in style. This sample demonstrates the TOC insertion in a word document. Note that DocIO can only insert TOC field in a word document. It can not refresh or create TOC field. MS Word refreshes the TOC field after insertion. Please update the field or press F9 key to refresh the TOC."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Section1") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading2); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading2 of built in style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph1") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives a meaning for the text."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph2") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. This demonstrates the paragraphs at the same level and style as that of the previous one. A paragraph can have any number formatting. This can be attained by formatting each text range in the paragraph."); section.AddParagraph(); section = doc.AddSection() as WSection; section.BreakCode = SectionBreakCode.NewPage; newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Section2") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading2); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading2 of built in style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph1") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives a meaning for the text."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph2") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. This demonstrates the paragraphs at the same level and style as that of the previous one. A paragraph can have any number formatting. This can be attained by formatting each text range in the paragraph."); #endregion toc.IncludePageNumbers = true; toc.RightAlignPageNumbers = true; toc.UseHyperlinks = true; toc.LowerHeadingLevel = 1; toc.UpperHeadingLevel = 3; toc.UseOutlineLevels = true; //Updates the table of contents. if (UpdateTOC == "Update") { doc.UpdateTableOfContents(); } //Save as .doc format if (Group1 == "WordDoc") { return(doc.ExportAsActionResult("Sample.doc", FormatType.Doc, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment)); } //Save as .docx format else if (Group1 == "WordDocx") { return(doc.ExportAsActionResult("Sample.docx", FormatType.Docx, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment)); } // Save as WordML(.xml) format else if (Group1 == "WordML") { return(doc.ExportAsActionResult("Sample.xml", FormatType.WordML, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment)); } //Save as .pdf format else if (Group1 == "Pdf") { DocToPDFConverter converter = new DocToPDFConverter(); PdfDocument pdfDoc = converter.ConvertToPDF(doc); return(pdfDoc.ExportAsActionResult("sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save)); } return(View()); }
public ActionResult Styles(string Group1, string Group2) { if (Group1 == null) { return(View()); } WordDocument document = null; #region Built-in Style if (Group1 == "Built-in") { document = new WordDocument(); WSection section = document.AddSection() as WSection; WParagraph para = section.AddParagraph() as WParagraph; section.AddColumn(100, 100); section.AddColumn(100, 100); section.MakeColumnsEqual(); #region List Style //List //para = section.AddParagraph() as WParagraph; para.AppendText("This para is written with style List").CharacterFormat.UnderlineStyle = UnderlineStyle.Double; para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.List); para.AppendText("Google Chrome\n"); para.AppendText("Mozilla Firefox\n"); para.AppendText("Internet Explorer"); //List5 style para = section.AddParagraph() as WParagraph; para.AppendText("\nThis para is written with style List5").CharacterFormat.UnderlineStyle = UnderlineStyle.Double; para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.List5); para.AppendText("Google Chrome\n"); para.AppendText("Mozilla Firefox\n"); para.AppendText("Internet Explorer"); #endregion #region ListNumber Style //List Number style para = section.AddParagraph() as WParagraph; para.AppendText("\nThis para is written with style ListNumber").CharacterFormat.UnderlineStyle = UnderlineStyle.Double; para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.ListNumber); para.AppendText("Google Chrome\n"); para.AppendText("Mozilla Firefox\n"); para.AppendText("Internet Explorer"); //List Number5 style para = section.AddParagraph() as WParagraph; para.AppendText("\nThis para is written with style ListNumber5").CharacterFormat.UnderlineStyle = UnderlineStyle.Double; para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.ListNumber5); para.AppendText("Google Chrome\n"); para.AppendText("Mozilla Firefox\n"); para.AppendText("Internet Explorer"); #endregion #region TOA Heading Style //TOA Heading para = section.AddParagraph() as WParagraph; para.ParagraphFormat.AfterSpacing = 10; para.AppendText("\nThis para is written with style TOA Heading").CharacterFormat.UnderlineStyle = UnderlineStyle.Double; para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.ToaHeading); para.AppendText("Google Chrome\n"); para.AppendText("Mozilla Firefox\n"); para.AppendText("Internet Explorer"); #endregion section.BreakCode = SectionBreakCode.NewColumn; #region ListBullet Style //ListBullet para = section.AddParagraph() as WParagraph; para.AppendText("\nThis para is written with style ListBullet").CharacterFormat.UnderlineStyle = UnderlineStyle.Double; para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.ListBullet); para.AppendText("Google Chrome\n"); para.AppendText("Mozilla Firefox\n"); para.AppendText("Internet Explorer"); //ListBullet5 para = section.AddParagraph() as WParagraph; para.AppendText("\nThis para is written with style ListBullet5").CharacterFormat.UnderlineStyle = UnderlineStyle.Double; para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.ListBullet5); para.AppendText("Google Chrome\n"); para.AppendText("Mozilla Firefox\n"); para.AppendText("Internet Explorer"); #endregion #region List Continue Style //ListContinue para = section.AddParagraph() as WParagraph; para.AppendText("\nThis para is written with style ListContinue").CharacterFormat.UnderlineStyle = UnderlineStyle.Double; para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.ListContinue); para.AppendText("Google Chrome\n"); para.AppendText("Mozilla Firefox\n"); para.AppendText("Internet Explorer"); //ListContinue5 para = section.AddParagraph() as WParagraph; para.AppendText("\nThis para is written with style ListContinue5").CharacterFormat.UnderlineStyle = UnderlineStyle.Double; para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.ListContinue5); para.AppendText("Google Chrome\n"); para.AppendText("Mozilla Firefox\n"); para.AppendText("Internet Explorer"); #endregion #region HTMLSample Style //HtmlSample para = section.AddParagraph() as WParagraph; para.AppendText("\nThis para is written with style HtmlSample").CharacterFormat.UnderlineStyle = UnderlineStyle.Double; para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.HtmlSample); para.AppendText("Google Chrome\n"); para.AppendText("Mozilla Firefox\n"); para.AppendText("Internet Explorer"); #endregion section = document.AddSection() as WSection; section.BreakCode = SectionBreakCode.NoBreak; #region Document Map Style //Docuemnt Map para = section.AddParagraph() as WParagraph; para.AppendText("This para is written with style DocumentMap\n").CharacterFormat.UnderlineStyle = UnderlineStyle.Double; para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.DocumentMap); para.AppendText("Google Chrome\n").CharacterFormat.TextColor = Syncfusion.Drawing.Color.White; para.AppendText("Mozilla Firefox\n").CharacterFormat.TextColor = Syncfusion.Drawing.Color.White; para.AppendText("Internet Explorer").CharacterFormat.TextColor = Syncfusion.Drawing.Color.White; #endregion #region Heading Styles //Heading Styles para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.Heading1); para.AppendText("Hello World. This para is written with style " + para.StyleName.ToString()); para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.Heading2); para.AppendText("Hello World. This para is written with style " + para.StyleName.ToString()); para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.Heading3); para.AppendText("Hello World. This para is written with style " + para.StyleName.ToString()); para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.Heading4); para.AppendText("Hello World. This para is written with style " + para.StyleName.ToString()); para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.Heading5); para.AppendText("Hello World. This para is written with style " + para.StyleName.ToString()); para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.Heading6); para.AppendText("Hello World. This para is written with style " + para.StyleName.ToString()); para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.Heading7); para.AppendText("Hello World. This para is written with style " + para.StyleName.ToString()); para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.Heading8); para.AppendText("Hello World. This para is written with style " + para.StyleName.ToString()); para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.Heading9); para.AppendText("Hello World. This para is written with style " + para.StyleName.ToString()); #endregion #region MessageHeaderStyle //MessageHeader para = section.AddParagraph() as WParagraph; para = section.AddParagraph() as WParagraph; para.AppendText("This para is written with style MessageHeader\n").CharacterFormat.UnderlineStyle = UnderlineStyle.Double; para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.MessageHeader); para.AppendText("Google Chrome\n"); para.AppendText("Mozilla Firefox\n"); para.AppendText("Internet Explorer"); #endregion } #endregion Built-in Style #region Custom Style else { document = new WordDocument(); IWParagraphStyle style = null; // Adding a new section to the document. WSection section = document.AddSection() as WSection; //Set Margin of the section section.PageSetup.Margins.All = 72; IWParagraph par = document.LastSection.AddParagraph(); WTextRange range = par.AppendText("Using CustomStyles") as WTextRange; range.CharacterFormat.TextBackgroundColor = Syncfusion.Drawing.Color.Red; range.CharacterFormat.TextColor = Syncfusion.Drawing.Color.White; range.CharacterFormat.FontSize = 18f; document.LastParagraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; // Create Paragraph styles style = document.AddParagraphStyle("MyStyle_Normal"); style.CharacterFormat.FontName = "Bitstream Vera Serif"; style.CharacterFormat.FontSize = 10f; style.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Justify; style.CharacterFormat.TextColor = Syncfusion.Drawing.Color.FromArgb(0, 21, 84); style = document.AddParagraphStyle("MyStyle_Low"); style.CharacterFormat.FontName = "Times New Roman"; style.CharacterFormat.FontSize = 16f; style.CharacterFormat.Bold = true; style = document.AddParagraphStyle("MyStyle_Medium"); style.CharacterFormat.FontName = "Monotype Corsiva"; style.CharacterFormat.FontSize = 18f; style.CharacterFormat.Bold = true; style.CharacterFormat.TextColor = Syncfusion.Drawing.Color.FromArgb(51, 66, 125); style = document.AddParagraphStyle("Mystyle_High"); style.CharacterFormat.FontName = "Bitstream Vera Serif"; style.CharacterFormat.FontSize = 20f; style.CharacterFormat.Bold = true; style.CharacterFormat.TextColor = Syncfusion.Drawing.Color.FromArgb(242, 151, 50); IWParagraph paragraph = null; for (int i = 0; i < document.Styles.Count; i++) { //Skip to apply the document default styles and also paragraph style. if (document.Styles[i].Name == "Normal" || document.Styles[i].Name == "Default Paragraph Font" || document.Styles[i].StyleType != StyleType.ParagraphStyle) { continue; } // Getting styles from Document. style = (IWParagraphStyle)document.Styles[i]; // Adding a new paragraph section.AddParagraph(); paragraph = section.AddParagraph(); // Applying styles to the current paragraph. paragraph.ApplyStyle(style.Name); // Writing Text with the current style and formatting. paragraph.AppendText("Northwind Database with [" + style.Name + "] Style"); // Adding a new paragraph section.AddParagraph(); paragraph = section.AddParagraph(); // Applying another style to the current paragraph. paragraph.ApplyStyle("MyStyle_Normal"); // Writing text with current style. paragraph.AppendText("The Northwind sample database (Northwind.mdb) is included with all versions of Access. It provides data you can experiment with and database objects that demonstrate features you might want to implement in your own databases. Using Northwind, you can become familiar with how a relational database is structured and how the database objects work together to help you enter, store, manipulate, and print your data."); } } #endregion Custom Style FormatType type = FormatType.Docx; string filename = "Sample.docx"; string contenttype = "application/vnd.ms-word.document.12"; #region Document SaveOption //Save as .doc format if (Group2 == "WordDoc") { type = FormatType.Doc; filename = "Sample.doc"; contenttype = "application/msword"; } //Save as .xml format else if (Group2 == "WordML") { type = FormatType.WordML; filename = "Sample.xml"; contenttype = "application/msword"; } #endregion Document SaveOption MemoryStream ms = new MemoryStream(); document.Save(ms, type); document.Close(); ms.Position = 0; return(File(ms, contenttype, filename)); }
/// <summary> /// Inserts and updates the Table of Contents (TOC) in a Word document. /// </summary> private void Button_Click(object sender, Microsoft.UI.Xaml.RoutedEventArgs e) { //Creates a new Word document. using WordDocument document = new(); //Adds one section and paragraph. document.EnsureMinimal(); document.LastSection.PageSetup.Margins.All = 72; WParagraph para = document.LastParagraph; IWTextRange textRange = para.AppendText("Essential DocIO - Table of Contents"); textRange.CharacterFormat.Bold = true; textRange.CharacterFormat.FontSize = 14; para.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; para.ParagraphFormat.BeforeSpacing = 12f; para.ParagraphFormat.AfterSpacing = 3f; para = document.LastSection.AddParagraph() as WParagraph; para.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; para.ParagraphFormat.BeforeSpacing = 12f; para.ParagraphFormat.AfterSpacing = 3f; if (!checkBox.IsChecked.Value) { textRange = para.AppendText("Select TOC and press F9 to update the Table of Contents"); textRange.CharacterFormat.HighlightColor = Syncfusion.Drawing.Color.Yellow; textRange.CharacterFormat.Bold = true; textRange.CharacterFormat.FontSize = 14; } para = document.LastSection.AddParagraph() as WParagraph; //Inserts TOC. Syncfusion.DocIO.DLS.TableOfContent toc = para.AppendTOC(1, 3); para.ParagraphFormat.BeforeSpacing = 12f; para.ParagraphFormat.AfterSpacing = 3f; //Applies built-in paragraph formatting. WSection section = document.LastSection; #region Default Styles WParagraph newPara = section.AddParagraph() as WParagraph; newPara = section.AddParagraph() as WParagraph; newPara.AppendBreak(BreakType.PageBreak); WTextRange text = newPara.AppendText("Document with Default styles") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading1); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading1 of built in style. This sample demonstrates the TOC insertion in a word document. Note that DocIO can only insert TOC field in a word document. It can not refresh or create TOC field. MS Word refreshes the TOC field after insertion. Please update the field or press F9 key to refresh the TOC."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Section1") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading2); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading2 of built in style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph1") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives a meaning for the text."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph2") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. This demonstrates the paragraphs at the same level and style as that of the previous one. A paragraph can have any number formatting. This can be attained by formatting each text range in the paragraph."); section.AddParagraph(); section = document.AddSection() as WSection; section.PageSetup.Margins.All = 72; section.BreakCode = SectionBreakCode.NewPage; newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Section2") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading2); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading2 of built in style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph1") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives a meaning for the text."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph2") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. This demonstrates the paragraphs at the same level and style as that of the previous one. A paragraph can have any number formatting. This can be attained by formatting each text range in the paragraph."); #endregion //Enables a flag to show page numbers in table of contents. toc.IncludePageNumbers = true; //Enables a flag to show page numbers as right aligned. toc.RightAlignPageNumbers = true; //Enables a flag to use hyperlinks for the levels. toc.UseHyperlinks = true; //Sets the starting heading level of the table of contents. toc.LowerHeadingLevel = 1; //Sets the ending heading level of the table of contents. toc.UpperHeadingLevel = 3; //Enables a flag to use outline levels. toc.UseOutlineLevels = true; //Updates the table of contents. if (checkBox.IsChecked.Value) { document.UpdateTableOfContents(); } #region Document SaveOption using MemoryStream ms = new(); string filename = string.Empty; //Saves as .docx format. if (worddocx.IsChecked == true) { filename = "Table of Contents.docx"; //Saves the Word document to the memory stream. document.Save(ms, FormatType.Docx); } //Saves as .doc format. else if (worddoc.IsChecked == true) { filename = "Table of Contents.doc"; //Saves the Word document to the memory stream. document.Save(ms, FormatType.Doc); } //Saves as .pdf format. else if (pdf.IsChecked == true) { filename = "Table of Contents.pdf"; //Creates a new DocIORenderer instance. using DocIORenderer renderer = new(); //Converts Word document into PDF. using PdfDocument pdfDoc = renderer.ConvertToPDF(document); //Saves the PDF document to the memory stream. pdfDoc.Save(ms); } ms.Position = 0; //Saves the memory stream as file. SaveHelper.SaveAndLaunch(filename, ms); #endregion Document SaveOption }
public ActionResult TableofContents(string Group1, string UpdateTOC) { if (Group1 == null) { return(View()); } WordDocument doc = new WordDocument(); doc.EnsureMinimal(); WParagraph para = doc.LastParagraph; para.AppendText("Essential DocIO - Table of Contents"); para.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; para.ApplyStyle(BuiltinStyle.Heading4); para = doc.LastSection.AddParagraph() as WParagraph; para.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; para.ApplyStyle(BuiltinStyle.Heading4); if (UpdateTOC != "Update") { para.AppendText("Select TOC and press F9 to update the Table of Contents").CharacterFormat.HighlightColor = Syncfusion.Drawing.Color.Yellow; } para = doc.LastSection.AddParagraph() as WParagraph; //Insert TOC TableOfContent toc = para.AppendTOC(1, 3); para.ApplyStyle(BuiltinStyle.Heading4); //Apply built-in paragraph formatting WSection section = doc.LastSection; #region Default Styles WParagraph newPara = section.AddParagraph() as WParagraph; newPara = section.AddParagraph() as WParagraph; newPara.AppendBreak(BreakType.PageBreak); WTextRange text = newPara.AppendText("Document with Default styles") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading1); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading1 of built in style. This sample demonstrates the TOC insertion in a word document. Note that DocIO can only insert TOC field in a word document. It can not refresh or create TOC field. MS Word refreshes the TOC field after insertion. Please update the field or press F9 key to refresh the TOC."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Section1") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading2); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading2 of built in style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph1") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives a meaning for the text."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph2") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. This demonstrates the paragraphs at the same level and style as that of the previous one. A paragraph can have any number formatting. This can be attained by formatting each text range in the paragraph."); section.AddParagraph(); section = doc.AddSection() as WSection; section.BreakCode = SectionBreakCode.NewPage; newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Section2") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading2); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading2 of built in style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph1") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives a meaning for the text."); section.AddParagraph(); newPara = section.AddParagraph() as WParagraph; text = newPara.AppendText("Paragraph2") as WTextRange; newPara.ApplyStyle(BuiltinStyle.Heading3); newPara = section.AddParagraph() as WParagraph; newPara.AppendText("This is the heading3 of built in style. This demonstrates the paragraphs at the same level and style as that of the previous one. A paragraph can have any number formatting. This can be attained by formatting each text range in the paragraph."); #endregion toc.IncludePageNumbers = true; toc.RightAlignPageNumbers = true; toc.UseHyperlinks = true; toc.LowerHeadingLevel = 1; toc.UpperHeadingLevel = 3; toc.UseOutlineLevels = true; //Updates the table of contents. if (UpdateTOC == "Update") { doc.UpdateTableOfContents(); } #region Document SaveOption string filename = ""; string contenttype = ""; MemoryStream ms = new MemoryStream(); //Save as .docx format if (Group1 == "WordDocx") { filename = "Table of Contents.docx"; contenttype = "application/vnd.ms-word.document.12"; doc.Save(ms, FormatType.Docx); } // Save as .doc format else if (Group1 == "WordDoc") { filename = "Table of Contents.doc"; contenttype = "application/msword"; doc.Save(ms, FormatType.Doc); } //Save as .xml format else if (Group1 == "WordML") { filename = "Table of Contents.xml"; contenttype = "application/msword"; doc.Save(ms, FormatType.WordML); } //Save as .pdf format else if (Group1 == "Pdf") { filename = "Table of Contents.pdf"; contenttype = "application/pdf"; Syncfusion.DocIORenderer.DocIORenderer renderer = new Syncfusion.DocIORenderer.DocIORenderer(); Syncfusion.Pdf.PdfDocument pdfDoc = renderer.ConvertToPDF(doc); pdfDoc.Save(ms); pdfDoc.Close(); } #endregion Document SaveOption doc.Close(); ms.Position = 0; return(File(ms, contenttype, filename)); }
private void ApplyBuiltInStyle() { // Creating a new document. using (WordDocument document = new WordDocument()) { #region Create a Word document //Add new section to the Word document WSection section = document.AddSection() as WSection; //Set Margin of the section section.PageSetup.Margins.All = 72; //Add new paragraph WParagraph para = section.AddParagraph() as WParagraph; //Add multi-column to the section section.AddColumn(100, 100); section.AddColumn(100, 100); //Make the section columns are qual section.MakeColumnsEqual(); #region Built-in styles #region List Style //Apply List Style para.AppendText("This para is written with style List").CharacterFormat.UnderlineStyle = Syncfusion.Drawing.UnderlineStyle.Double; para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.List); para.AppendText("Google Chrome\n"); para.AppendText("Mozilla Firefox\n"); para.AppendText("Internet Explorer"); //Apply List5 style para = section.AddParagraph() as WParagraph; para.AppendText("\nThis para is written with style List5").CharacterFormat.UnderlineStyle = Syncfusion.Drawing.UnderlineStyle.Double; para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.List5); para.AppendText("Google Chrome\n"); para.AppendText("Mozilla Firefox\n"); para.AppendText("Internet Explorer"); #endregion #region ListNumber Style //List Number style para = section.AddParagraph() as WParagraph; para.AppendText("\nThis para is written with style ListNumber").CharacterFormat.UnderlineStyle = Syncfusion.Drawing.UnderlineStyle.Double; para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.ListNumber); para.AppendText("Google Chrome\n"); para.AppendText("Mozilla Firefox\n"); para.AppendText("Internet Explorer"); //List Number5 style para = section.AddParagraph() as WParagraph; para.AppendText("\nThis para is written with style ListNumber5").CharacterFormat.UnderlineStyle = Syncfusion.Drawing.UnderlineStyle.Double; para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.ListNumber5); para.AppendText("Google Chrome\n"); para.AppendText("Mozilla Firefox\n"); para.AppendText("Internet Explorer"); #endregion #region TOA Heading Style //Apply TOA Heading style para = section.AddParagraph() as WParagraph; para.AppendText("\nThis para is written with style TOA Heading").CharacterFormat.UnderlineStyle = Syncfusion.Drawing.UnderlineStyle.Double; para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.ToaHeading); para.AppendText("Google Chrome\n"); para.AppendText("Mozilla Firefox\n"); para.AppendText("Internet Explorer"); #endregion //Add new paragraph para = section.AddParagraph() as WParagraph; //Set Section break section.BreakCode = SectionBreakCode.NewColumn; #region ListBullet Style //ListBullet para = section.AddParagraph() as WParagraph; para.AppendText("\nThis para is written with style ListBullet").CharacterFormat.UnderlineStyle = Syncfusion.Drawing.UnderlineStyle.Double; para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.ListBullet); para.AppendText("Google Chrome\n"); para.AppendText("Mozilla Firefox\n"); para.AppendText("Internet Explorer"); //ListBullet5 para = section.AddParagraph() as WParagraph; para.AppendText("\nThis para is written with style ListBullet5").CharacterFormat.UnderlineStyle = Syncfusion.Drawing.UnderlineStyle.Double; para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.ListBullet5); para.AppendText("Google Chrome\n"); para.AppendText("Mozilla Firefox\n"); para.AppendText("Internet Explorer"); #endregion #region List Continue Style //Apply ListContinue Style para = section.AddParagraph() as WParagraph; para.AppendText("\nThis para is written with style ListContinue").CharacterFormat.UnderlineStyle = Syncfusion.Drawing.UnderlineStyle.Double; para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.ListContinue); para.AppendText("Google Chrome\n"); para.AppendText("Mozilla Firefox\n"); para.AppendText("Internet Explorer"); //Apply ListContinue5 style para = section.AddParagraph() as WParagraph; para.AppendText("\nThis para is written with style ListContinue5").CharacterFormat.UnderlineStyle = Syncfusion.Drawing.UnderlineStyle.Double; para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.ListContinue5); para.AppendText("Google Chrome\n"); para.AppendText("Mozilla Firefox\n"); para.AppendText("Internet Explorer"); #endregion #region HTMLSample Style //Apply HtmlSample style para = section.AddParagraph() as WParagraph; para.AppendText("\nThis para is written with style HtmlSample").CharacterFormat.UnderlineStyle = Syncfusion.Drawing.UnderlineStyle.Double; para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.HtmlSample); para.AppendText("Google Chrome\n"); para.AppendText("Mozilla Firefox\n"); para.AppendText("Internet Explorer"); #endregion //Add new section to the document section = document.AddSection() as WSection; //Set Section break section.BreakCode = SectionBreakCode.NoBreak; #region Document Map Style //Apply Docuemnt Map Style para = section.AddParagraph() as WParagraph; para.AppendText("This para is written with style DocumentMap\n").CharacterFormat.UnderlineStyle = Syncfusion.Drawing.UnderlineStyle.Double; para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.DocumentMap); IWTextRange textrange = para.AppendText("Google Chrome\n"); textrange.CharacterFormat.TextBackgroundColor = Syncfusion.Drawing.Color.Red; textrange = para.AppendText("Mozilla Firefox\n"); textrange.CharacterFormat.TextBackgroundColor = Syncfusion.Drawing.Color.Red; textrange = para.AppendText("Internet Explorer"); textrange.CharacterFormat.TextBackgroundColor = Syncfusion.Drawing.Color.Red; #endregion #region Heading Styles //Apply Heading Styles para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.Heading1); para.AppendText("Hello World. This para is written with style " + para.StyleName.ToString()); para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.Heading2); para.AppendText("Hello World. This para is written with style " + para.StyleName.ToString()); para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.Heading3); para.AppendText("Hello World. This para is written with style " + para.StyleName.ToString()); para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.Heading4); para.AppendText("Hello World. This para is written with style " + para.StyleName.ToString()); para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.Heading5); para.AppendText("Hello World. This para is written with style " + para.StyleName.ToString()); para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.Heading6); para.AppendText("Hello World. This para is written with style " + para.StyleName.ToString()); para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.Heading7); para.AppendText("Hello World. This para is written with style " + para.StyleName.ToString()); para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.Heading8); para.AppendText("Hello World. This para is written with style " + para.StyleName.ToString()); para = section.AddParagraph() as WParagraph; para.ApplyStyle(BuiltinStyle.Heading9); para.AppendText("Hello World. This para is written with style " + para.StyleName.ToString()); #endregion #endregion Built-in styles #endregion #region Saving Document //Save the word document to the stream. MemoryStream stream = new MemoryStream(); document.Save(stream, FormatType.Docx); //Save file in the disk based on specfic OS if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows) { Xamarin.Forms.DependencyService.Get <ISaveWindowsPhone>().Save("WordDocument_BuiltInStyles.docx", "application/msword", stream); } else { Xamarin.Forms.DependencyService.Get <ISave>().Save("WordDocument_BuiltInStyles.docx", "application/msword", stream); } #endregion } }