How to find and replace a text with chart in Word document?
Syncfusion® DocIO is a .NET Core Word library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can find and replace text with a chart in a Word document using C#.
Steps to find and replace text with a chart in a Word document
- Create a new .NET Core console application project.
- Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your application from NuGet.org.
Note: Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion® license key in your application to use the components without a trial message.
- Include the following namespaces in the Program.cs file:
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.OfficeChart;
- Add the following code snippet in the Program.cs file to find text and replace it with a chart in a Word document.
using (FileStream fileStreamPath = new FileStream("Data/Input.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
// Open an existing Word document.
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
{
// Create text body part.
TextBodyPart bodyPart = new TextBodyPart(document);
// Create a chart in a paragraph.
WParagraph paragraph = CreateBarChart(document);
// Add the paragraph to the text body part.
bodyPart.BodyItems.Add(paragraph);
// Replace the placeholder text with a new chart.
document.Replace("[Purchase details]", bodyPart, true, true, true);
// Create file stream.
using (FileStream outputFileStream = new FileStream("Data/Result.docx", FileMode.Create, FileAccess.ReadWrite))
{
// Save the Word document to the file stream.
document.Save(outputFileStream, FormatType.Docx);
}
}
}
- Add the helper methods below to create a bar chart for the Word document:
// Create a bar chart inside a new paragraph.
private static WParagraph CreateBarChart(WordDocument document)
{
// Create a new paragraph.
WParagraph paragraph = new WParagraph(document);
// Append a new chart.
WChart barchart = paragraph.AppendChart(400, 300);
// Set the type as a bar chart.
barchart.ChartType = OfficeChartType.Bar_Clustered;
// Add data for the bar chart.
AddBarChartData(barchart);
// Set the other parts of the chart.
barchart.ChartTitle = "Purchase Details";
barchart.ChartTitleArea.FontName = "Calibri";
barchart.ChartTitleArea.Size = 14;
IOfficeChartSerie serie1 = barchart.Series.Add("Sum of Future Expenses");
serie1.Values = barchart.ChartData[2, 2, 6, 2];
IOfficeChartSerie serie2 = barchart.Series.Add("Sum of Purchases");
serie2.Values = barchart.ChartData[2, 3, 6, 3];
barchart.HasDataTable = true;
barchart.DataTable.HasBorders = true;
barchart.DataTable.HasHorzBorder = true;
barchart.DataTable.HasVertBorder = true;
barchart.DataTable.ShowSeriesKeys = true;
barchart.HasLegend = false;
barchart.PlotArea.Border.LinePattern = OfficeChartLinePattern.Solid;
barchart.ChartArea.Border.LinePattern = OfficeChartLinePattern.Solid;
barchart.PrimaryCategoryAxis.CategoryLabels = barchart.ChartData[2, 1, 6, 1];
barchart.PrimaryCategoryAxis.Font.Size = 12;
barchart.PrimaryCategoryAxis.MajorTickMark = OfficeTickMark.TickMark_None;
barchart.PrimaryValueAxis.MajorTickMark = OfficeTickMark.TickMark_None;
// Return the new paragraph that has the bar chart in it.
return paragraph;
}
// Add the data for the bar chart.
private static void AddBarChartData(WChart chart)
{
chart.ChartData.SetValue(1, 2, "Sum of Future Expenses");
chart.ChartData.SetValue(1, 3, "Sum of Purchases");
chart.ChartData.SetValue(2, 1, "Nancy Davalio");
chart.ChartData.SetValue(2, 2, 1300);
chart.ChartData.SetValue(2, 3, 600);
chart.ChartData.SetValue(3, 1, "Andrew Fuller");
chart.ChartData.SetValue(3, 2, 680);
chart.ChartData.SetValue(3, 3, 1000);
chart.ChartData.SetValue(4, 1, "Janet Leverling");
chart.ChartData.SetValue(4, 2, 1280);
chart.ChartData.SetValue(4, 3, 800);
chart.ChartData.SetValue(5, 1, "Margaret Peacock");
chart.ChartData.SetValue(5, 2, 2000);
chart.ChartData.SetValue(5, 3, 400);
chart.ChartData.SetValue(6, 1, "Steven Buchanan");
chart.ChartData.SetValue(6, 2, 2660);
chart.ChartData.SetValue(6, 3, 731);
}
A complete working sample to find and replace text with a chart in a Word document using C# can be downloaded from GitHub.
By executing the program, you will get the Word document as follows:
Take a moment to peruse the documentation where you can find basic Word document processing options along with features like mail merge, merge, split, and compare Word documents, find and replace text in the Word document, protect Word documents, and most importantly, the PDF and Image conversions with code examples.
See Also
- How to replace particular text with a hyperlink in a Word document
- How to replace text in a Word document with HTML
- How to find and replace text inside a table in a Word document
- How to find and replace text in headers and footers of a Word document
- How to find and replace a placeholder with a page break in a Word document
- How to find and replace a line break in a Word document as a paragraph mark
- How to find and replace text in a Word document with text from a database
Conclusion
I hope you enjoyed learning about how to resolve font problems when converting a Word document to PDF or image conversion.
You can refer to our ASP.NET Core DocIO feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our ASP.NET Core DocIO example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!