|
| 1 | +using Syncfusion.DocIO.DLS; |
| 2 | +using Syncfusion.DocIO; |
| 3 | +using Syncfusion.DocIORenderer; |
| 4 | +using Syncfusion.OfficeChart; |
| 5 | + |
| 6 | + |
| 7 | +// Open the file as stream. |
| 8 | +using (FileStream docStream = new FileStream(Path.GetFullPath(@"../../../Data/Input.docx"), FileMode.Open)) |
| 9 | +{ |
| 10 | + // Load file stream into Word document. |
| 11 | + using (WordDocument document = new WordDocument(docStream, Syncfusion.DocIO.FormatType.Automatic)) |
| 12 | + { |
| 13 | + string[] propertyNames = { "ContentControlProperties.Title", "ContentControlProperties.Type" }; |
| 14 | + string[] propertyValues = { "Chart", "Picture" }; |
| 15 | + // Find BlockContentControl by given properties |
| 16 | + BlockContentControl contentControl = document.FindItemByProperties(EntityType.BlockContentControl, propertyNames, propertyValues) as BlockContentControl; |
| 17 | + |
| 18 | + if (contentControl != null) |
| 19 | + { |
| 20 | + int index = contentControl.OwnerTextBody.ChildEntities.IndexOf(contentControl); |
| 21 | + |
| 22 | + // Create a new paragraph to hold the chart image. |
| 23 | + WParagraph paragraph = new WParagraph(document); |
| 24 | + |
| 25 | + // Generate the chart and get it as an image stream. |
| 26 | + Stream chartImage = GenerateChartAndGetChartAsImage(); |
| 27 | + |
| 28 | + // Create a new image instance and load the chart image. |
| 29 | + WPicture picture = (WPicture)paragraph.AppendPicture(chartImage); |
| 30 | + |
| 31 | + // Set picture dimensions. |
| 32 | + picture.Height = 300; |
| 33 | + picture.Width = 300; |
| 34 | + |
| 35 | + // To replace Picture content Control insert Picture paragraph and remove the content control. |
| 36 | + contentControl.OwnerTextBody.ChildEntities.Insert(index, paragraph); |
| 37 | + contentControl.OwnerTextBody.ChildEntities.RemoveAt(index + 1); |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + // Create file stream for output document. |
| 42 | + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Data/Sample.docx"), FileMode.Create, FileAccess.ReadWrite)) |
| 43 | + { |
| 44 | + // Save the Word document to the file stream. |
| 45 | + document.Save(outputFileStream, FormatType.Docx); |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +/// <summary> |
| 52 | +/// Generates a chart and saves it as an image. |
| 53 | +/// </summary> |
| 54 | +/// <returns>A stream containing the chart image.</returns> |
| 55 | +static Stream GenerateChartAndGetChartAsImage() |
| 56 | +{ |
| 57 | + // Create a new Word document. |
| 58 | + using (WordDocument document = new WordDocument()) |
| 59 | + { |
| 60 | + // Add a section to the document. |
| 61 | + IWSection section = document.AddSection(); |
| 62 | + // Add a paragraph to the section. |
| 63 | + IWParagraph paragraph = section.AddParagraph(); |
| 64 | + // Create and append the chart to the paragraph. |
| 65 | + WChart chart = paragraph.AppendChart(446, 270); |
| 66 | + |
| 67 | + // Set chart data. |
| 68 | + chart.ChartData.SetValue(1, 1, "Month"); |
| 69 | + chart.ChartData.SetValue(2, 1, "Jan"); |
| 70 | + chart.ChartData.SetValue(3, 1, "Feb"); |
| 71 | + chart.ChartData.SetValue(4, 1, "Mar"); |
| 72 | + chart.ChartData.SetValue(5, 1, "Apr"); |
| 73 | + chart.ChartData.SetValue(6, 1, "May"); |
| 74 | + chart.ChartData.SetValue(7, 1, "Jun"); |
| 75 | + chart.ChartData.SetValue(8, 1, "Jul"); |
| 76 | + chart.ChartData.SetValue(9, 1, "Aug"); |
| 77 | + chart.ChartData.SetValue(10, 1, "Sep"); |
| 78 | + chart.ChartData.SetValue(11, 1, "Oct"); |
| 79 | + chart.ChartData.SetValue(12, 1, "Nov"); |
| 80 | + chart.ChartData.SetValue(13, 1, "Dec"); |
| 81 | + chart.ChartData.SetValue(1, 2, "Rainy Days"); |
| 82 | + chart.ChartData.SetValue(2, 2, 12); |
| 83 | + chart.ChartData.SetValue(3, 2, 11); |
| 84 | + chart.ChartData.SetValue(4, 2, 10); |
| 85 | + chart.ChartData.SetValue(5, 2, 9); |
| 86 | + chart.ChartData.SetValue(6, 2, 8); |
| 87 | + chart.ChartData.SetValue(7, 2, 6); |
| 88 | + chart.ChartData.SetValue(8, 2, 4); |
| 89 | + chart.ChartData.SetValue(9, 2, 6); |
| 90 | + chart.ChartData.SetValue(10, 2, 7); |
| 91 | + chart.ChartData.SetValue(11, 2, 8); |
| 92 | + chart.ChartData.SetValue(12, 2, 10); |
| 93 | + chart.ChartData.SetValue(13, 2, 11); |
| 94 | + chart.ChartData.SetValue(1, 3, "Profit"); |
| 95 | + chart.ChartData.SetValue(2, 3, 3574); |
| 96 | + chart.ChartData.SetValue(3, 3, 4708); |
| 97 | + chart.ChartData.SetValue(4, 3, 5332); |
| 98 | + chart.ChartData.SetValue(5, 3, 6693); |
| 99 | + chart.ChartData.SetValue(6, 3, 8843); |
| 100 | + chart.ChartData.SetValue(7, 3, 12347); |
| 101 | + chart.ChartData.SetValue(8, 3, 15180); |
| 102 | + chart.ChartData.SetValue(9, 3, 11198); |
| 103 | + chart.ChartData.SetValue(10, 3, 9739); |
| 104 | + chart.ChartData.SetValue(11, 3, 9846); |
| 105 | + chart.ChartData.SetValue(12, 3, 6620); |
| 106 | + chart.ChartData.SetValue(13, 3, 5085); |
| 107 | + |
| 108 | + // Set region of chart data. |
| 109 | + chart.DataRange = chart.ChartData[1, 1, 13, 3]; |
| 110 | + // Set chart series in the column for assigned data region. |
| 111 | + chart.IsSeriesInRows = false; |
| 112 | + // Set chart title. |
| 113 | + chart.ChartTitle = "Combination Chart"; |
| 114 | + |
| 115 | + // Set data labels. |
| 116 | + IOfficeChartSerie series1 = chart.Series[0]; |
| 117 | + IOfficeChartSerie series2 = chart.Series[1]; |
| 118 | + // Set series type. |
| 119 | + series1.SerieType = OfficeChartType.Column_Clustered; |
| 120 | + series2.SerieType = OfficeChartType.Line; |
| 121 | + series2.UsePrimaryAxis = false; |
| 122 | + |
| 123 | + // Set data labels. |
| 124 | + series1.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; |
| 125 | + |
| 126 | + // Set legend. |
| 127 | + chart.HasLegend = true; |
| 128 | + chart.Legend.Position = OfficeLegendPosition.Bottom; |
| 129 | + |
| 130 | + // Set chart type. |
| 131 | + chart.ChartType = OfficeChartType.Combination_Chart; |
| 132 | + |
| 133 | + // Set secondary axis on right side. |
| 134 | + chart.SecondaryValueAxis.TickLabelPosition = OfficeTickLabelPosition.TickLabelPosition_High; |
| 135 | + |
| 136 | + // Create an instance of DocIORenderer. |
| 137 | + using (DocIORenderer renderer = new DocIORenderer()) |
| 138 | + { |
| 139 | + // Convert chart to an image. |
| 140 | + Stream stream = chart.SaveAsImage(); |
| 141 | + return stream; |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
0 commit comments