Hi.
I have an existing word file called “ExamplePlaceHolder.docx” that contains a placeholder called <<chart1>>
(file attached).
I need to replace this placeholder with a Aspose Chart.
Can I have a code example that does this operation?
Thanks in advance.
Best regards
Maurilio Martini
ExamplePlaceHolder.docx (11.6 KB)
@FAIVUser Placeholder in your document is a merge field, so you can use IFieldMergingCallback to insert a chart upon executing mail merge:
Document doc = new Document(@"C:\Temp\in.docx");
doc.MailMerge.FieldMergingCallback = new InsertChartCallback();
doc.MailMerge.Execute(new string[] { "chart1" }, new object[] { "some data" });
doc.Save(@"C:\Temp\out.docx");
private class InsertChartCallback : IFieldMergingCallback
{
public void FieldMerging(FieldMergingArgs args)
{
if (args.FieldName.ToLower() == "chart1")
{
DocumentBuilder builder = new DocumentBuilder(args.Document);
// Move to mergefield.
builder.MoveToField(args.Field, true);
// Insert a chrt
builder.InsertChart(Aspose.Words.Drawing.Charts.ChartType.Bar, 300, 200);
// Set field value to empty string.
args.FieldValue = "";
}
}
public void ImageFieldMerging(ImageFieldMergingArgs args)
{
// Do nothing
}
}
I would also suggest you to consider using LINQ Reporting Engine to create charts:
https://docs.aspose.com/words/net/linq-working-with-charts/