Articles in this section
Category / Section

How to find and replace a text with chart in Word document?

4 mins read

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 a text with chart in Word document using C#.

Steps to find and replace a text with chart in Word document

  1. Create a new .NET Core console application project.

Project template in Visual Studio

2. Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your application from NuGet.org.

Install DocIO NuGet package in your application

Note: Starting with v16.2.0.x, if you reference Syncfusion® assemblies from 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 trail message.

  1. Include the following Namespaces in the Program.cs file.
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.OfficeChart;
  1. Add the following code snippet in Program.cs file to find a text and replace with chart in 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 textbody part.
            TextBodyPart bodyPart = new TextBodyPart(document);
            //Create a chart in paragraph.
            WParagraph paragraph= CreateBarChart(document);  
            //Add the paragraph to textbody 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 file stream.
                 document.Save(outputFileStream, FormatType.Docx);
            }
      }
}
       
  1. Add the below helper methods to create a bar chart for Word document.

// Create a bar chart inside a new paragraph.
private static WParagraph CreateBarChart(WordDocument document)
{
   //Create new paragraph.
   WParagraph paragraph = new WParagraph(document);
   //Append new chart.
   WChart barchart = paragraph.AppendChart(400, 300);
   //Set type as 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 bar chart in it.
   return paragraph;
}
       
// Add the data for 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 a text with chart in Word document using C# can be downloaded from GitHub.

By executing the program, you will get the Word document as follows.

Output of find and replace text with chart in C#

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 documents, find and replace text in the Word document, and most importantly PDF conversion with code examples.

See Also

  1. How to replace the particular text with hyperlink in Word document
  2. How to replace text in a Word document with HTML
  3. How to find and replace text inside table in Word document
  4. How to find and replace text in headers and footers of Word document
  5. How to find and replace placeholder with page break in Word document
  6. How to find and replace line break in Word document as paragraph mark
  7. How to find and replace in Word document with text from database

Conclusion
I hope you enjoyed learning about how to resolve font problems when converting Word document to PDF or image conversion.

You can refer to our ASP.NET Core DocIO feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for 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!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied