|
| 1 | +using System.IO; |
| 2 | +using Syncfusion.DocIO; |
| 3 | +using Syncfusion.DocIO.DLS; |
| 4 | +using Syncfusion.OfficeChart; |
| 5 | + |
| 6 | +namespace Insert_caption_to_chart |
| 7 | +{ |
| 8 | + class Program |
| 9 | + { |
| 10 | + static void Main(string[] args) |
| 11 | + { |
| 12 | + //Create a new Word document. |
| 13 | + using (WordDocument document = new WordDocument()) |
| 14 | + { |
| 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)) |
| 73 | + { |
| 74 | + //Save the Word document to the file stream. |
| 75 | + document.Save(outputFileStream, FormatType.Docx); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + /// <summary> |
| 80 | + /// Add caption to the chart. |
| 81 | + /// </summary> |
| 82 | + public static void AddCaptionToChart(WChart chart, string captionName, CaptionNumberingFormat format, CaptionPosition captionPosition) |
| 83 | + { |
| 84 | + IWParagraph ownerParagraph = chart.OwnerParagraph; |
| 85 | + WTextBody body = (ownerParagraph.Owner as WTextBody); |
| 86 | + WParagraph paragraph = null; |
| 87 | + if (body != null) |
| 88 | + { |
| 89 | + //Get the index of the owner paragraph. |
| 90 | + int index = GetIndexInOwnerCollection(ownerParagraph); |
| 91 | + paragraph = new WParagraph(chart.Document); |
| 92 | + paragraph.AppendText(captionName + " "); |
| 93 | + captionName = captionName.Replace(" ", "_"); |
| 94 | + paragraph.ApplyStyle(BuiltinStyle.Caption); |
| 95 | + WSeqField field = (WSeqField)paragraph.AppendField(captionName, FieldType.FieldSequence); |
| 96 | + field.NumberFormat = format; |
| 97 | + int chartIndex = ownerParagraph.Items.IndexOf(chart); |
| 98 | + |
| 99 | + // Set needed formatting and paragraph location dependently on captionPosition value |
| 100 | + if (captionPosition == CaptionPosition.AboveImage) |
| 101 | + { |
| 102 | + paragraph.ParagraphFormat.KeepFollow = true; |
| 103 | + int captionIndex = (chartIndex == 0) ? index : index + 1; |
| 104 | + |
| 105 | + body.ChildEntities.Insert(captionIndex, paragraph); |
| 106 | + |
| 107 | + if (chartIndex > 0) |
| 108 | + { |
| 109 | + ownerParagraph.Items.RemoveAt(chartIndex); |
| 110 | + WParagraph newParagraph = new WParagraph(chart.Document); |
| 111 | + newParagraph.Items.Insert(0, chart); |
| 112 | + body.ChildEntities.Insert(captionIndex + 1, newParagraph); |
| 113 | + } |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + ownerParagraph.ParagraphFormat.KeepFollow = true; |
| 118 | + body.ChildEntities.Insert(index + 1, paragraph); |
| 119 | + } |
| 120 | + ApplyFormattingForCaption(paragraph); |
| 121 | + } |
| 122 | + } |
| 123 | + /// <summary> |
| 124 | + /// This methode is used to get the index of the paragraph. |
| 125 | + /// </summary> |
| 126 | + public static int GetIndexInOwnerCollection(IWParagraph ownerParagraph) |
| 127 | + { |
| 128 | + |
| 129 | + ICompositeEntity composite = ownerParagraph.Owner as ICompositeEntity; |
| 130 | + |
| 131 | + if (composite != null) |
| 132 | + { |
| 133 | + return composite.ChildEntities.IndexOf(ownerParagraph); |
| 134 | + } |
| 135 | + //If item is inside inline content control. |
| 136 | + else if (ownerParagraph is InlineContentControl) |
| 137 | + return (ownerParagraph as InlineContentControl).ParagraphItems.IndexOf(ownerParagraph); |
| 138 | + |
| 139 | + return -1; |
| 140 | + } |
| 141 | + /// <summary> |
| 142 | + /// Apply formattings for image caption paragraph |
| 143 | + /// </summary> |
| 144 | + public static void ApplyFormattingForCaption(WParagraph paragraph) |
| 145 | + { |
| 146 | + //Align the caption |
| 147 | + paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; |
| 148 | + //Sets after spacing |
| 149 | + paragraph.ParagraphFormat.AfterSpacing = 1.5f; |
| 150 | + //Sets before spacing |
| 151 | + paragraph.ParagraphFormat.BeforeSpacing = 1.5f; |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments