@@ -9,70 +9,30 @@ class Program
99 {
1010 static void Main ( string [ ] args )
1111 {
12- //Create a new Word document.
13- using ( WordDocument document = new WordDocument ( ) )
12+ Syncfusion . Licensing . SyncfusionLicenseProvider . RegisterLicense ( "NxYtFisQPR08Cit/VkR+XU9FfV5AQmBIYVp/TGpJfl96cVxMZVVBJAtUQF1hTH9SdENiWHtZc3ZVRWFeWkd1" ) ;
13+ // Load existing Word document.
14+ using ( FileStream inputFileStream = new FileStream ( Path . GetFullPath ( @"../../../Data/Template.docx" ) , FileMode . Open , FileAccess . ReadWrite ) )
1415 {
15- //Add a section to the document.
16- IWSection section = document . AddSection ( ) ;
17- //Add a paragraph to the section.
18- IWParagraph paragraph = section . AddParagraph ( ) ;
19- //Create and append the chart to the paragraph.
20- WChart chart = paragraph . AppendChart ( 446 , 270 ) ;
21- //Set chart type.
22- chart . ChartType = OfficeChartType . Line ;
23- //Set chart data.
24- chart . ChartData . SetValue ( 1 , 1 , "Fruits" ) ;
25- chart . ChartData . SetValue ( 2 , 1 , "Apples" ) ;
26- chart . ChartData . SetValue ( 3 , 1 , "Grapes" ) ;
27- chart . ChartData . SetValue ( 4 , 1 , "Bananas" ) ;
28- chart . ChartData . SetValue ( 5 , 1 , "Oranges" ) ;
29- chart . ChartData . SetValue ( 6 , 1 , "Melons" ) ;
30- chart . ChartData . SetValue ( 1 , 2 , "Joey" ) ;
31- chart . ChartData . SetValue ( 2 , 2 , 5 ) ;
32- chart . ChartData . SetValue ( 3 , 2 , 4 ) ;
33- chart . ChartData . SetValue ( 4 , 2 , 4 ) ;
34- chart . ChartData . SetValue ( 5 , 2 , 2 ) ;
35- chart . ChartData . SetValue ( 6 , 2 , 2 ) ;
36- chart . ChartData . SetValue ( 1 , 3 , "Matthew" ) ;
37- chart . ChartData . SetValue ( 2 , 3 , 3 ) ;
38- chart . ChartData . SetValue ( 3 , 3 , 5 ) ;
39- chart . ChartData . SetValue ( 4 , 3 , 4 ) ;
40- chart . ChartData . SetValue ( 5 , 3 , 1 ) ;
41- chart . ChartData . SetValue ( 6 , 3 , 7 ) ;
42- chart . ChartData . SetValue ( 1 , 4 , "Peter" ) ;
43- chart . ChartData . SetValue ( 2 , 4 , 2 ) ;
44- chart . ChartData . SetValue ( 3 , 4 , 2 ) ;
45- chart . ChartData . SetValue ( 4 , 4 , 3 ) ;
46- chart . ChartData . SetValue ( 5 , 4 , 5 ) ;
47- chart . ChartData . SetValue ( 6 , 4 , 6 ) ;
48- //Set region of Chart data.
49- chart . DataRange = chart . ChartData [ 1 , 1 , 6 , 4 ] ;
50- //Set chart series in the column for assigned data region.
51- chart . IsSeriesInRows = false ;
52- //Set a Chart Title.
53- chart . ChartTitle = "Line Chart" ;
54- //Set Datalabels.
55- IOfficeChartSerie series1 = chart . Series [ 0 ] ;
56- IOfficeChartSerie series2 = chart . Series [ 1 ] ;
57- IOfficeChartSerie series3 = chart . Series [ 2 ] ;
58-
59- series1 . DataPoints . DefaultDataPoint . DataLabels . IsValue = true ;
60- series2 . DataPoints . DefaultDataPoint . DataLabels . IsValue = true ;
61- series3 . DataPoints . DefaultDataPoint . DataLabels . IsValue = true ;
62- //Set legend.
63- chart . HasLegend = true ;
64- chart . Legend . Position = OfficeLegendPosition . Bottom ;
65- //Mention caption text here.
66- string captionName = "Figure" ;
67- //Add caption to the chart.
68- AddCaptionToChart ( chart , captionName , CaptionNumberingFormat . Number , CaptionPosition . AfterImage ) ;
69- //Update fields in the Word document.
70- document . UpdateDocumentFields ( ) ;
71- //Create a file stream.
72- using ( FileStream outputFileStream = new FileStream ( Path . GetFullPath ( @"../../../Output/Result.docx" ) , FileMode . Create , FileAccess . ReadWrite ) )
16+ // Initialize the Word document with the input file stream.
17+ using ( WordDocument document = new WordDocument ( inputFileStream , FormatType . Automatic ) )
7318 {
74- //Save the Word document to the file stream.
75- document . Save ( outputFileStream , FormatType . Docx ) ;
19+ Entity entity = document . FindItemByProperty ( EntityType . Chart , null , null ) ;
20+ WChart chart = entity as WChart ;
21+ if ( chart != null )
22+ {
23+ //Mention caption text here.
24+ string captionName = "Chart" ;
25+ //Add caption to the chart.
26+ AddCaptionToChart ( chart , captionName , CaptionNumberingFormat . Number , CaptionPosition . AfterImage ) ;
27+ //Update fields in the Word document.
28+ document . UpdateDocumentFields ( ) ;
29+ }
30+ //Create a file stream.
31+ using ( FileStream outputFileStream = new FileStream ( Path . GetFullPath ( @"../../../Output/Result.docx" ) , FileMode . Create , FileAccess . ReadWrite ) )
32+ {
33+ //Save the Word document to the file stream.
34+ document . Save ( outputFileStream , FormatType . Docx ) ;
35+ }
7636 }
7737 }
7838 }
@@ -82,7 +42,7 @@ static void Main(string[] args)
8242 public static void AddCaptionToChart ( WChart chart , string captionName , CaptionNumberingFormat format , CaptionPosition captionPosition )
8343 {
8444 IWParagraph ownerParagraph = chart . OwnerParagraph ;
85- WTextBody body = ( ownerParagraph . Owner as WTextBody ) ;
45+ WTextBody body = ownerParagraph . Owner as WTextBody ;
8646 WParagraph paragraph = null ;
8747 if ( body != null )
8848 {
@@ -97,7 +57,12 @@ public static void AddCaptionToChart(WChart chart, string captionName, CaptionNu
9757 int chartIndex = ownerParagraph . Items . IndexOf ( chart ) ;
9858
9959 // Set needed formatting and paragraph location dependently on captionPosition value
100- if ( captionPosition == CaptionPosition . AboveImage )
60+ if ( captionPosition == CaptionPosition . AfterImage )
61+ {
62+ ownerParagraph . ParagraphFormat . KeepFollow = true ;
63+ body . ChildEntities . Insert ( index + 1 , paragraph ) ;
64+ }
65+ else
10166 {
10267 paragraph . ParagraphFormat . KeepFollow = true ;
10368 int captionIndex = ( chartIndex == 0 ) ? index : index + 1 ;
@@ -112,11 +77,6 @@ public static void AddCaptionToChart(WChart chart, string captionName, CaptionNu
11277 body . ChildEntities . Insert ( captionIndex + 1 , newParagraph ) ;
11378 }
11479 }
115- else
116- {
117- ownerParagraph . ParagraphFormat . KeepFollow = true ;
118- body . ChildEntities . Insert ( index + 1 , paragraph ) ;
119- }
12080 ApplyFormattingForCaption ( paragraph ) ;
12181 }
12282 }
0 commit comments