Skip to content

Commit e72f017

Browse files
Merge pull request #340 from SyncfusionExamples/Add-readme-file
Add README file in Getting started Create Word document sample
2 parents 405a8eb + 1f5898e commit e72f017

File tree

5 files changed

+267
-5
lines changed

5 files changed

+267
-5
lines changed

Getting-Started/.NET/Create-Word-document/Create-Word-document.csproj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,22 @@
1212
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
1313
</ItemGroup>
1414

15+
<ItemGroup>
16+
<None Update="Data\AdventureCycle.jpg">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Data\Mountain-200.jpg">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
<None Update="Data\Mountain-300.jpg">
23+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
24+
</None>
25+
<None Update="Data\Road-550-W.jpg">
26+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
27+
</None>
28+
<None Update="Output\.gitkeep">
29+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
30+
</None>
31+
</ItemGroup>
32+
1533
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

136 KB
Binary file not shown.

Getting-Started/.NET/Create-Word-document/Program.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
IWParagraph paragraph = section.HeadersFooters.Header.AddParagraph();
3434
// Get the image stream.
35-
FileStream imageStream = new FileStream(@"../../../Data/AdventureCycle.jpg", FileMode.Open, FileAccess.Read);
35+
FileStream imageStream = new FileStream(@Path.GetFullPath("Data/AdventureCycle.jpg"), FileMode.Open, FileAccess.Read);
3636
IWPicture picture = paragraph.AppendPicture(imageStream);
3737
picture.TextWrappingStyle = TextWrappingStyle.InFrontOfText;
3838
picture.VerticalOrigin = VerticalOrigin.Margin;
@@ -88,7 +88,7 @@
8888
paragraph.ParagraphFormat.AfterSpacing = 0;
8989
paragraph.BreakCharacterFormat.FontSize = 12f;
9090
//Append picture to the paragraph.
91-
FileStream image1 = new FileStream(@"../../../Data/Mountain-200.jpg", FileMode.Open, FileAccess.Read);
91+
FileStream image1 = new FileStream(Path.GetFullPath(@"Data/Mountain-200.jpg"), FileMode.Open, FileAccess.Read);
9292
picture = paragraph.AppendPicture(image1);
9393
picture.TextWrappingStyle = TextWrappingStyle.TopAndBottom;
9494
picture.VerticalOrigin = VerticalOrigin.Paragraph;
@@ -163,7 +163,7 @@
163163
paragraph.ApplyStyle("Heading 1");
164164
paragraph.ParagraphFormat.LineSpacing = 12f;
165165
//Append picture to the paragraph.
166-
FileStream image2 = new FileStream(@"../../../Data/Mountain-300.jpg", FileMode.Open, FileAccess.Read);
166+
FileStream image2 = new FileStream(Path.GetFullPath(@"Data/Mountain-300.jpg"), FileMode.Open, FileAccess.Read);
167167
picture = paragraph.AppendPicture(image2);
168168
picture.TextWrappingStyle = TextWrappingStyle.TopAndBottom;
169169
picture.VerticalOrigin = VerticalOrigin.Paragraph;
@@ -178,7 +178,7 @@
178178
paragraph.ApplyStyle("Heading 1");
179179
paragraph.ParagraphFormat.LineSpacing = 12f;
180180
//Append picture to the paragraph.
181-
FileStream image3 = new FileStream(@"../../../Data/Road-550-W.jpg", FileMode.Open, FileAccess.Read);
181+
FileStream image3 = new FileStream(Path.GetFullPath(@"Data/Road-550-W.jpg"), FileMode.Open, FileAccess.Read);
182182
picture = paragraph.AppendPicture(image3);
183183
picture.TextWrappingStyle = TextWrappingStyle.TopAndBottom;
184184
picture.VerticalOrigin = VerticalOrigin.Paragraph;
@@ -216,7 +216,7 @@
216216
section.AddParagraph();
217217

218218
//Save the Word document to FileStream
219-
using (FileStream stream = new FileStream(@"../../../Sample.docx", FileMode.OpenOrCreate))
219+
using (FileStream stream = new FileStream(Path.GetFullPath(@"Output/Sample.docx"), FileMode.OpenOrCreate))
220220
{
221221
document.Save(stream, FormatType.Docx);
222222
}
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
# Create Word document using C#
2+
3+
The Syncfusion&reg; [.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+
using (WordDocument document = new WordDocument())
23+
{
24+
//Adding a new section to the document.
25+
WSection section = document.AddSection() as WSection;
26+
//Set Margin of the section
27+
section.PageSetup.Margins.All = 72;
28+
//Set page size of the section
29+
section.PageSetup.PageSize = new Syncfusion.Drawing.SizeF(612, 792);
30+
31+
//Create Paragraph styles
32+
WParagraphStyle style = document.AddParagraphStyle("Normal") as WParagraphStyle;
33+
style.CharacterFormat.FontName = "Calibri";
34+
style.CharacterFormat.FontSize = 11f;
35+
style.ParagraphFormat.BeforeSpacing = 0;
36+
style.ParagraphFormat.AfterSpacing = 8;
37+
style.ParagraphFormat.LineSpacing = 13.8f;
38+
39+
style = document.AddParagraphStyle("Heading 1") as WParagraphStyle;
40+
style.ApplyBaseStyle("Normal");
41+
style.CharacterFormat.FontName = "Calibri Light";
42+
style.CharacterFormat.FontSize = 16f;
43+
style.CharacterFormat.TextColor = Syncfusion.Drawing.Color.FromArgb(46, 116, 181);
44+
style.ParagraphFormat.BeforeSpacing = 12;
45+
style.ParagraphFormat.AfterSpacing = 0;
46+
style.ParagraphFormat.Keep = true;
47+
style.ParagraphFormat.KeepFollow = true;
48+
style.ParagraphFormat.OutlineLevel = OutlineLevel.Level1;
49+
50+
IWParagraph paragraph = section.HeadersFooters.Header.AddParagraph();
51+
// Get the image stream.
52+
FileStream imageStream = new FileStream(@Path.GetFullPath("Data/AdventureCycle.jpg"), FileMode.Open, FileAccess.Read);
53+
IWPicture picture = paragraph.AppendPicture(imageStream);
54+
picture.TextWrappingStyle = TextWrappingStyle.InFrontOfText;
55+
picture.VerticalOrigin = VerticalOrigin.Margin;
56+
picture.VerticalPosition = -45;
57+
picture.HorizontalOrigin = HorizontalOrigin.Column;
58+
picture.HorizontalPosition = 263.5f;
59+
picture.WidthScale = 20;
60+
picture.HeightScale = 15;
61+
62+
paragraph.ApplyStyle("Normal");
63+
paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Left;
64+
WTextRange textRange = paragraph.AppendText("Adventure Works Cycles") as WTextRange;
65+
textRange.CharacterFormat.FontSize = 12f;
66+
textRange.CharacterFormat.FontName = "Calibri";
67+
textRange.CharacterFormat.TextColor = Syncfusion.Drawing.Color.Red;
68+
69+
//Append paragraph.
70+
paragraph = section.AddParagraph();
71+
paragraph.ApplyStyle("Heading 1");
72+
paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;
73+
textRange = paragraph.AppendText("Adventure Works Cycles") as WTextRange;
74+
textRange.CharacterFormat.FontSize = 18f;
75+
textRange.CharacterFormat.FontName = "Calibri";
76+
77+
//Append paragraph.
78+
paragraph = section.AddParagraph();
79+
paragraph.ParagraphFormat.FirstLineIndent = 36;
80+
paragraph.BreakCharacterFormat.FontSize = 12f;
81+
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;
82+
textRange.CharacterFormat.FontSize = 12f;
83+
84+
//Append paragraph.
85+
paragraph = section.AddParagraph();
86+
paragraph.ParagraphFormat.FirstLineIndent = 36;
87+
paragraph.BreakCharacterFormat.FontSize = 12f;
88+
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;
89+
textRange.CharacterFormat.FontSize = 12f;
90+
91+
paragraph = section.AddParagraph();
92+
paragraph.ApplyStyle("Heading 1");
93+
paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Left;
94+
textRange = paragraph.AppendText("Product Overview") as WTextRange;
95+
textRange.CharacterFormat.FontSize = 16f;
96+
textRange.CharacterFormat.FontName = "Calibri";
97+
98+
//Append table.
99+
IWTable table = section.AddTable();
100+
table.ResetCells(3, 2);
101+
table.TableFormat.Borders.BorderType = BorderStyle.None;
102+
table.TableFormat.IsAutoResized = true;
103+
//Append paragraph.
104+
paragraph = table[0, 0].AddParagraph();
105+
paragraph.ParagraphFormat.AfterSpacing = 0;
106+
paragraph.BreakCharacterFormat.FontSize = 12f;
107+
//Append picture to the paragraph.
108+
FileStream image1 = new FileStream(Path.GetFullPath(@"Data/Mountain-200.jpg"), FileMode.Open, FileAccess.Read);
109+
picture = paragraph.AppendPicture(image1);
110+
picture.TextWrappingStyle = TextWrappingStyle.TopAndBottom;
111+
picture.VerticalOrigin = VerticalOrigin.Paragraph;
112+
picture.VerticalPosition = 4.5f;
113+
picture.HorizontalOrigin = HorizontalOrigin.Column;
114+
picture.HorizontalPosition = -2.15f;
115+
picture.WidthScale = 79;
116+
picture.HeightScale = 79;
117+
118+
//Append paragraph.
119+
paragraph = table[0, 1].AddParagraph();
120+
paragraph.ApplyStyle("Heading 1");
121+
paragraph.ParagraphFormat.AfterSpacing = 0;
122+
paragraph.ParagraphFormat.LineSpacing = 12f;
123+
paragraph.AppendText("Mountain-200");
124+
//Append paragraph.
125+
paragraph = table[0, 1].AddParagraph();
126+
paragraph.ParagraphFormat.AfterSpacing = 0;
127+
paragraph.ParagraphFormat.LineSpacing = 12f;
128+
paragraph.BreakCharacterFormat.FontSize = 12f;
129+
paragraph.BreakCharacterFormat.FontName = "Times New Roman";
130+
textRange = paragraph.AppendText("Product No: BK-M68B-38\r") as WTextRange;
131+
textRange.CharacterFormat.FontSize = 12f;
132+
textRange.CharacterFormat.FontName = "Times New Roman";
133+
textRange = paragraph.AppendText("Size: 38\r") as WTextRange;
134+
textRange.CharacterFormat.FontSize = 12f;
135+
textRange.CharacterFormat.FontName = "Times New Roman";
136+
textRange = paragraph.AppendText("Weight: 25\r") as WTextRange;
137+
textRange.CharacterFormat.FontSize = 12f;
138+
textRange.CharacterFormat.FontName = "Times New Roman";
139+
textRange = paragraph.AppendText("Price: $2,294.99\r") as WTextRange;
140+
textRange.CharacterFormat.FontSize = 12f;
141+
textRange.CharacterFormat.FontName = "Times New Roman";
142+
//Append paragraph.
143+
paragraph = table[0, 1].AddParagraph();
144+
paragraph.ParagraphFormat.AfterSpacing = 0;
145+
paragraph.ParagraphFormat.LineSpacing = 12f;
146+
paragraph.BreakCharacterFormat.FontSize = 12f;
147+
148+
//Append paragraph.
149+
paragraph = table[1, 0].AddParagraph();
150+
paragraph.ApplyStyle("Heading 1");
151+
paragraph.ParagraphFormat.AfterSpacing = 0;
152+
paragraph.ParagraphFormat.LineSpacing = 12f;
153+
paragraph.AppendText("Mountain-300 ");
154+
//Append paragraph.
155+
paragraph = table[1, 0].AddParagraph();
156+
paragraph.ParagraphFormat.AfterSpacing = 0;
157+
paragraph.ParagraphFormat.LineSpacing = 12f;
158+
paragraph.BreakCharacterFormat.FontSize = 12f;
159+
paragraph.BreakCharacterFormat.FontName = "Times New Roman";
160+
textRange = paragraph.AppendText("Product No: BK-M47B-38\r") as WTextRange;
161+
textRange.CharacterFormat.FontSize = 12f;
162+
textRange.CharacterFormat.FontName = "Times New Roman";
163+
textRange = paragraph.AppendText("Size: 35\r") as WTextRange;
164+
textRange.CharacterFormat.FontSize = 12f;
165+
textRange.CharacterFormat.FontName = "Times New Roman";
166+
textRange = paragraph.AppendText("Weight: 22\r") as WTextRange;
167+
textRange.CharacterFormat.FontSize = 12f;
168+
textRange.CharacterFormat.FontName = "Times New Roman";
169+
textRange = paragraph.AppendText("Price: $1,079.99\r") as WTextRange;
170+
textRange.CharacterFormat.FontSize = 12f;
171+
textRange.CharacterFormat.FontName = "Times New Roman";
172+
//Append paragraph.
173+
paragraph = table[1, 0].AddParagraph();
174+
paragraph.ParagraphFormat.AfterSpacing = 0;
175+
paragraph.ParagraphFormat.LineSpacing = 12f;
176+
paragraph.BreakCharacterFormat.FontSize = 12f;
177+
178+
//Append paragraph.
179+
paragraph = table[1, 1].AddParagraph();
180+
paragraph.ApplyStyle("Heading 1");
181+
paragraph.ParagraphFormat.LineSpacing = 12f;
182+
//Append picture to the paragraph.
183+
FileStream image2 = new FileStream(Path.GetFullPath(@"Data/Mountain-300.jpg"), FileMode.Open, FileAccess.Read);
184+
picture = paragraph.AppendPicture(image2);
185+
picture.TextWrappingStyle = TextWrappingStyle.TopAndBottom;
186+
picture.VerticalOrigin = VerticalOrigin.Paragraph;
187+
picture.VerticalPosition = 8.2f;
188+
picture.HorizontalOrigin = HorizontalOrigin.Column;
189+
picture.HorizontalPosition = -14.95f;
190+
picture.WidthScale = 75;
191+
picture.HeightScale = 75;
192+
193+
//Append paragraph.
194+
paragraph = table[2, 0].AddParagraph();
195+
paragraph.ApplyStyle("Heading 1");
196+
paragraph.ParagraphFormat.LineSpacing = 12f;
197+
//Append picture to the paragraph.
198+
FileStream image3 = new FileStream(Path.GetFullPath(@"Data/Road-550-W.jpg"), FileMode.Open, FileAccess.Read);
199+
picture = paragraph.AppendPicture(image3);
200+
picture.TextWrappingStyle = TextWrappingStyle.TopAndBottom;
201+
picture.VerticalOrigin = VerticalOrigin.Paragraph;
202+
picture.VerticalPosition = 3.75f;
203+
picture.HorizontalOrigin = HorizontalOrigin.Column;
204+
picture.HorizontalPosition = -5f;
205+
picture.WidthScale = 92;
206+
picture.HeightScale = 92;
207+
208+
//Append paragraph.
209+
paragraph = table[2, 1].AddParagraph();
210+
paragraph.ApplyStyle("Heading 1");
211+
paragraph.ParagraphFormat.AfterSpacing = 0;
212+
paragraph.ParagraphFormat.LineSpacing = 12f;
213+
paragraph.AppendText("Road-150 ");
214+
//Append paragraph.
215+
paragraph = table[2, 1].AddParagraph();
216+
paragraph.ParagraphFormat.AfterSpacing = 0;
217+
paragraph.ParagraphFormat.LineSpacing = 12f;
218+
paragraph.BreakCharacterFormat.FontSize = 12f;
219+
paragraph.BreakCharacterFormat.FontName = "Times New Roman";
220+
textRange = paragraph.AppendText("Product No: BK-R93R-44\r") as WTextRange;
221+
textRange.CharacterFormat.FontSize = 12f;
222+
textRange.CharacterFormat.FontName = "Times New Roman";
223+
textRange = paragraph.AppendText("Size: 44\r") as WTextRange;
224+
textRange.CharacterFormat.FontSize = 12f;
225+
textRange.CharacterFormat.FontName = "Times New Roman";
226+
textRange = paragraph.AppendText("Weight: 14\r") as WTextRange;
227+
textRange.CharacterFormat.FontSize = 12f;
228+
textRange.CharacterFormat.FontName = "Times New Roman";
229+
textRange = paragraph.AppendText("Price: $3,578.27\r") as WTextRange;
230+
textRange.CharacterFormat.FontSize = 12f;
231+
textRange.CharacterFormat.FontName = "Times New Roman";
232+
//Append paragraph.
233+
section.AddParagraph();
234+
235+
//Save the Word document to FileStream
236+
using (FileStream stream = new FileStream(Path.GetFullPath(@"Output/Sample.docx"), FileMode.OpenOrCreate))
237+
{
238+
document.Save(stream, FormatType.Docx);
239+
}
240+
}
241+
```
242+
243+
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

Comments
 (0)