|
| 1 | +# Create Word document using C# |
| 2 | + |
| 3 | +The Syncfusion® [.NET Word Library](https://www.syncfusion.com/document-processing/word-framework/net/word-library) (DocIO) empowers you to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can **create Word document** using C#. |
| 4 | + |
| 5 | +## Steps to create Word document programmatically |
| 6 | + |
| 7 | +Step 1: Create a new .NET Core console application project. |
| 8 | + |
| 9 | +Step 2: Install the [Syncfusion.DocIO.Net.Core](https://www.nuget.org/packages/Syncfusion.DocIO.Net.Core) NuGet package as a reference to your project from [NuGet.org](https://www.nuget.org/). |
| 10 | + |
| 11 | +Step 3: Include the following namespaces in the Program.cs file. |
| 12 | + |
| 13 | +```csharp |
| 14 | +using Syncfusion.DocIO.DLS; |
| 15 | +using Syncfusion.DocIO; |
| 16 | +``` |
| 17 | + |
| 18 | +Step 4: Add the following code snippet in Program.cs file to split Word document by bookmarks. |
| 19 | + |
| 20 | +```csharp |
| 21 | +// Creating a new document. |
| 22 | +WordDocument document = new WordDocument(); |
| 23 | +//Adding a new section to the document. |
| 24 | +WSection section = document.AddSection() as WSection; |
| 25 | +//Set Margin of the section |
| 26 | +section.PageSetup.Margins.All = 72; |
| 27 | +//Set page size of the section |
| 28 | +section.PageSetup.PageSize = new Syncfusion.Drawing.SizeF(612, 792); |
| 29 | + |
| 30 | +//Create Paragraph styles |
| 31 | +WParagraphStyle style = document.AddParagraphStyle("Normal") as WParagraphStyle; |
| 32 | +style.CharacterFormat.FontName = "Calibri"; |
| 33 | +style.CharacterFormat.FontSize = 11f; |
| 34 | +style.ParagraphFormat.BeforeSpacing = 0; |
| 35 | +style.ParagraphFormat.AfterSpacing = 8; |
| 36 | +style.ParagraphFormat.LineSpacing = 13.8f; |
| 37 | + |
| 38 | +style = document.AddParagraphStyle("Heading 1") as WParagraphStyle; |
| 39 | +style.ApplyBaseStyle("Normal"); |
| 40 | +style.CharacterFormat.FontName = "Calibri Light"; |
| 41 | +style.CharacterFormat.FontSize = 16f; |
| 42 | +style.CharacterFormat.TextColor = Syncfusion.Drawing.Color.FromArgb(46, 116, 181); |
| 43 | +style.ParagraphFormat.BeforeSpacing = 12; |
| 44 | +style.ParagraphFormat.AfterSpacing = 0; |
| 45 | +style.ParagraphFormat.Keep = true; |
| 46 | +style.ParagraphFormat.KeepFollow = true; |
| 47 | +style.ParagraphFormat.OutlineLevel = OutlineLevel.Level1; |
| 48 | + |
| 49 | +IWParagraph paragraph = section.HeadersFooters.Header.AddParagraph(); |
| 50 | +// Get the image stream. |
| 51 | +FileStream imageStream = new FileStream(@Path.GetFullPath("Data/AdventureCycle.jpg"), FileMode.Open, FileAccess.Read); |
| 52 | +IWPicture picture = paragraph.AppendPicture(imageStream); |
| 53 | +picture.TextWrappingStyle = TextWrappingStyle.InFrontOfText; |
| 54 | +picture.VerticalOrigin = VerticalOrigin.Margin; |
| 55 | +picture.VerticalPosition = -45; |
| 56 | +picture.HorizontalOrigin = HorizontalOrigin.Column; |
| 57 | +picture.HorizontalPosition = 263.5f; |
| 58 | +picture.WidthScale = 20; |
| 59 | +picture.HeightScale = 15; |
| 60 | + |
| 61 | +paragraph.ApplyStyle("Normal"); |
| 62 | +paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Left; |
| 63 | +WTextRange textRange = paragraph.AppendText("Adventure Works Cycles") as WTextRange; |
| 64 | +textRange.CharacterFormat.FontSize = 12f; |
| 65 | +textRange.CharacterFormat.FontName = "Calibri"; |
| 66 | +textRange.CharacterFormat.TextColor = Syncfusion.Drawing.Color.Red; |
| 67 | + |
| 68 | +//Append paragraph. |
| 69 | +paragraph = section.AddParagraph(); |
| 70 | +paragraph.ApplyStyle("Heading 1"); |
| 71 | +paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center; |
| 72 | +textRange = paragraph.AppendText("Adventure Works Cycles") as WTextRange; |
| 73 | +textRange.CharacterFormat.FontSize = 18f; |
| 74 | +textRange.CharacterFormat.FontName = "Calibri"; |
| 75 | + |
| 76 | +//Append paragraph. |
| 77 | +paragraph = section.AddParagraph(); |
| 78 | +paragraph.ParagraphFormat.FirstLineIndent = 36; |
| 79 | +paragraph.BreakCharacterFormat.FontSize = 12f; |
| 80 | +textRange = paragraph.AppendText("Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European and Asian commercial markets. While its base operation is in Bothell, Washington with 290 employees, several regional sales teams are located throughout their market base.") as WTextRange; |
| 81 | +textRange.CharacterFormat.FontSize = 12f; |
| 82 | + |
| 83 | +//Append paragraph. |
| 84 | +paragraph = section.AddParagraph(); |
| 85 | +paragraph.ParagraphFormat.FirstLineIndent = 36; |
| 86 | +paragraph.BreakCharacterFormat.FontSize = 12f; |
| 87 | +textRange = paragraph.AppendText("In 2000, AdventureWorks Cycles bought a small manufacturing plant, Importadores Neptuno, located in Mexico. Importadores Neptuno manufactures several critical subcomponents for the AdventureWorks Cycles product line. These subcomponents are shipped to the Bothell location for final product assembly. In 2001, Importadores Neptuno, became the sole manufacturer and distributor of the touring bicycle product group.") as WTextRange; |
| 88 | +textRange.CharacterFormat.FontSize = 12f; |
| 89 | + |
| 90 | +paragraph = section.AddParagraph(); |
| 91 | +paragraph.ApplyStyle("Heading 1"); |
| 92 | +paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Left; |
| 93 | +textRange = paragraph.AppendText("Product Overview") as WTextRange; |
| 94 | +textRange.CharacterFormat.FontSize = 16f; |
| 95 | +textRange.CharacterFormat.FontName = "Calibri"; |
| 96 | + |
| 97 | +//Append table. |
| 98 | +IWTable table = section.AddTable(); |
| 99 | +table.ResetCells(3, 2); |
| 100 | +table.TableFormat.Borders.BorderType = BorderStyle.None; |
| 101 | +table.TableFormat.IsAutoResized = true; |
| 102 | +//Append paragraph. |
| 103 | +paragraph = table[0, 0].AddParagraph(); |
| 104 | +paragraph.ParagraphFormat.AfterSpacing = 0; |
| 105 | +paragraph.BreakCharacterFormat.FontSize = 12f; |
| 106 | +//Append picture to the paragraph. |
| 107 | +FileStream image1 = new FileStream(Path.GetFullPath(@"Data/Mountain-200.jpg"), FileMode.Open, FileAccess.Read); |
| 108 | +picture = paragraph.AppendPicture(image1); |
| 109 | +picture.TextWrappingStyle = TextWrappingStyle.TopAndBottom; |
| 110 | +picture.VerticalOrigin = VerticalOrigin.Paragraph; |
| 111 | +picture.VerticalPosition = 4.5f; |
| 112 | +picture.HorizontalOrigin = HorizontalOrigin.Column; |
| 113 | +picture.HorizontalPosition = -2.15f; |
| 114 | +picture.WidthScale = 79; |
| 115 | +picture.HeightScale = 79; |
| 116 | + |
| 117 | +//Append paragraph. |
| 118 | +paragraph = table[0, 1].AddParagraph(); |
| 119 | +paragraph.ApplyStyle("Heading 1"); |
| 120 | +paragraph.ParagraphFormat.AfterSpacing = 0; |
| 121 | +paragraph.ParagraphFormat.LineSpacing = 12f; |
| 122 | +paragraph.AppendText("Mountain-200"); |
| 123 | +//Append paragraph. |
| 124 | +paragraph = table[0, 1].AddParagraph(); |
| 125 | +paragraph.ParagraphFormat.AfterSpacing = 0; |
| 126 | +paragraph.ParagraphFormat.LineSpacing = 12f; |
| 127 | +paragraph.BreakCharacterFormat.FontSize = 12f; |
| 128 | +paragraph.BreakCharacterFormat.FontName = "Times New Roman"; |
| 129 | +textRange = paragraph.AppendText("Product No: BK-M68B-38\r") as WTextRange; |
| 130 | +textRange.CharacterFormat.FontSize = 12f; |
| 131 | +textRange.CharacterFormat.FontName = "Times New Roman"; |
| 132 | +textRange = paragraph.AppendText("Size: 38\r") as WTextRange; |
| 133 | +textRange.CharacterFormat.FontSize = 12f; |
| 134 | +textRange.CharacterFormat.FontName = "Times New Roman"; |
| 135 | +textRange = paragraph.AppendText("Weight: 25\r") as WTextRange; |
| 136 | +textRange.CharacterFormat.FontSize = 12f; |
| 137 | +textRange.CharacterFormat.FontName = "Times New Roman"; |
| 138 | +textRange = paragraph.AppendText("Price: $2,294.99\r") as WTextRange; |
| 139 | +textRange.CharacterFormat.FontSize = 12f; |
| 140 | +textRange.CharacterFormat.FontName = "Times New Roman"; |
| 141 | +//Append paragraph. |
| 142 | +paragraph = table[0, 1].AddParagraph(); |
| 143 | +paragraph.ParagraphFormat.AfterSpacing = 0; |
| 144 | +paragraph.ParagraphFormat.LineSpacing = 12f; |
| 145 | +paragraph.BreakCharacterFormat.FontSize = 12f; |
| 146 | + |
| 147 | +//Append paragraph. |
| 148 | +paragraph = table[1, 0].AddParagraph(); |
| 149 | +paragraph.ApplyStyle("Heading 1"); |
| 150 | +paragraph.ParagraphFormat.AfterSpacing = 0; |
| 151 | +paragraph.ParagraphFormat.LineSpacing = 12f; |
| 152 | +paragraph.AppendText("Mountain-300 "); |
| 153 | +//Append paragraph. |
| 154 | +paragraph = table[1, 0].AddParagraph(); |
| 155 | +paragraph.ParagraphFormat.AfterSpacing = 0; |
| 156 | +paragraph.ParagraphFormat.LineSpacing = 12f; |
| 157 | +paragraph.BreakCharacterFormat.FontSize = 12f; |
| 158 | +paragraph.BreakCharacterFormat.FontName = "Times New Roman"; |
| 159 | +textRange = paragraph.AppendText("Product No: BK-M47B-38\r") as WTextRange; |
| 160 | +textRange.CharacterFormat.FontSize = 12f; |
| 161 | +textRange.CharacterFormat.FontName = "Times New Roman"; |
| 162 | +textRange = paragraph.AppendText("Size: 35\r") as WTextRange; |
| 163 | +textRange.CharacterFormat.FontSize = 12f; |
| 164 | +textRange.CharacterFormat.FontName = "Times New Roman"; |
| 165 | +textRange = paragraph.AppendText("Weight: 22\r") as WTextRange; |
| 166 | +textRange.CharacterFormat.FontSize = 12f; |
| 167 | +textRange.CharacterFormat.FontName = "Times New Roman"; |
| 168 | +textRange = paragraph.AppendText("Price: $1,079.99\r") as WTextRange; |
| 169 | +textRange.CharacterFormat.FontSize = 12f; |
| 170 | +textRange.CharacterFormat.FontName = "Times New Roman"; |
| 171 | +//Append paragraph. |
| 172 | +paragraph = table[1, 0].AddParagraph(); |
| 173 | +paragraph.ParagraphFormat.AfterSpacing = 0; |
| 174 | +paragraph.ParagraphFormat.LineSpacing = 12f; |
| 175 | +paragraph.BreakCharacterFormat.FontSize = 12f; |
| 176 | + |
| 177 | +//Append paragraph. |
| 178 | +paragraph = table[1, 1].AddParagraph(); |
| 179 | +paragraph.ApplyStyle("Heading 1"); |
| 180 | +paragraph.ParagraphFormat.LineSpacing = 12f; |
| 181 | +//Append picture to the paragraph. |
| 182 | +FileStream image2 = new FileStream(Path.GetFullPath(@"Data/Mountain-300.jpg"), FileMode.Open, FileAccess.Read); |
| 183 | +picture = paragraph.AppendPicture(image2); |
| 184 | +picture.TextWrappingStyle = TextWrappingStyle.TopAndBottom; |
| 185 | +picture.VerticalOrigin = VerticalOrigin.Paragraph; |
| 186 | +picture.VerticalPosition = 8.2f; |
| 187 | +picture.HorizontalOrigin = HorizontalOrigin.Column; |
| 188 | +picture.HorizontalPosition = -14.95f; |
| 189 | +picture.WidthScale = 75; |
| 190 | +picture.HeightScale = 75; |
| 191 | + |
| 192 | +//Append paragraph. |
| 193 | +paragraph = table[2, 0].AddParagraph(); |
| 194 | +paragraph.ApplyStyle("Heading 1"); |
| 195 | +paragraph.ParagraphFormat.LineSpacing = 12f; |
| 196 | +//Append picture to the paragraph. |
| 197 | +FileStream image3 = new FileStream(Path.GetFullPath(@"Data/Road-550-W.jpg"), FileMode.Open, FileAccess.Read); |
| 198 | +picture = paragraph.AppendPicture(image3); |
| 199 | +picture.TextWrappingStyle = TextWrappingStyle.TopAndBottom; |
| 200 | +picture.VerticalOrigin = VerticalOrigin.Paragraph; |
| 201 | +picture.VerticalPosition = 3.75f; |
| 202 | +picture.HorizontalOrigin = HorizontalOrigin.Column; |
| 203 | +picture.HorizontalPosition = -5f; |
| 204 | +picture.WidthScale = 92; |
| 205 | +picture.HeightScale = 92; |
| 206 | + |
| 207 | +//Append paragraph. |
| 208 | +paragraph = table[2, 1].AddParagraph(); |
| 209 | +paragraph.ApplyStyle("Heading 1"); |
| 210 | +paragraph.ParagraphFormat.AfterSpacing = 0; |
| 211 | +paragraph.ParagraphFormat.LineSpacing = 12f; |
| 212 | +paragraph.AppendText("Road-150 "); |
| 213 | +//Append paragraph. |
| 214 | +paragraph = table[2, 1].AddParagraph(); |
| 215 | +paragraph.ParagraphFormat.AfterSpacing = 0; |
| 216 | +paragraph.ParagraphFormat.LineSpacing = 12f; |
| 217 | +paragraph.BreakCharacterFormat.FontSize = 12f; |
| 218 | +paragraph.BreakCharacterFormat.FontName = "Times New Roman"; |
| 219 | +textRange = paragraph.AppendText("Product No: BK-R93R-44\r") as WTextRange; |
| 220 | +textRange.CharacterFormat.FontSize = 12f; |
| 221 | +textRange.CharacterFormat.FontName = "Times New Roman"; |
| 222 | +textRange = paragraph.AppendText("Size: 44\r") as WTextRange; |
| 223 | +textRange.CharacterFormat.FontSize = 12f; |
| 224 | +textRange.CharacterFormat.FontName = "Times New Roman"; |
| 225 | +textRange = paragraph.AppendText("Weight: 14\r") as WTextRange; |
| 226 | +textRange.CharacterFormat.FontSize = 12f; |
| 227 | +textRange.CharacterFormat.FontName = "Times New Roman"; |
| 228 | +textRange = paragraph.AppendText("Price: $3,578.27\r") as WTextRange; |
| 229 | +textRange.CharacterFormat.FontSize = 12f; |
| 230 | +textRange.CharacterFormat.FontName = "Times New Roman"; |
| 231 | +//Append paragraph. |
| 232 | +section.AddParagraph(); |
| 233 | + |
| 234 | +//Save the Word document to FileStream |
| 235 | +using FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create); |
| 236 | +document.Save(outputStream, FormatType.Docx); |
| 237 | +``` |
| 238 | + |
| 239 | +More information about Create a Word document can be refer in this [documentation](https://help.syncfusion.com/document-processing/word/word-library/net/working-with-word-document) section. |
0 commit comments