Charts are not replacing at correct tag position

Hello Team ,

We are replacing charts and table to the required tag position in template , tables are replacing properly at desired tag position, but same is not happen for Chart , I have attach console application code and required feed and generated doc for your reference , kindly check and help to resolve this .

Thanks,
Avinash
POCAndFeed.zip (1.1 MB)

@avinashchilke1

It seems that you are experiencing issues with replacing charts at the correct tag positions in your template, while tables are being replaced correctly. Here are some steps and considerations that might help you resolve this issue:

  1. Check Chart Insertion Method: Ensure that you are using the correct method to insert charts into your document. The InsertChart method from the DocumentBuilder class is typically used for this purpose. Make sure you specify the correct chart type and dimensions.

  2. Positioning Charts: When inserting a chart, you can control its position by adjusting the Shape object that is returned when you insert the chart. You can set the position of the shape using properties like Top and Left to ensure it aligns with your desired tag position.

  3. Clearing Default Series: If you are adding new data to the chart, remember to clear any default series that may be automatically generated. This can be done using the Clear() method on the Series collection of the chart.

  4. Example Code: Here’s a simple example of how to insert a chart and set its position:

    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);
    
    // Insert a chart at a specific position
    Shape shape = builder.InsertChart(ChartType.COLUMN, 432, 252);
    shape.Top = 100; // Set the top position
    shape.Left = 100; // Set the left position
    
    Chart chart = shape.Chart;
    chart.Series.Clear(); // Clear default series
    chart.Series.Add("Series 1", new string[] { "Category 1", "Category 2" }, new double[] { 1, 2 });
    
    doc.Save("output.docx");
    
  5. Debugging: If the charts are still not appearing in the correct positions, consider logging the positions of the charts after insertion to verify their coordinates. This can help identify if they are being placed correctly or if there is an issue with the positioning logic.

  6. Documentation Reference: For more detailed information on working with charts in Aspose.Words, you can refer to the official documentation which provides comprehensive examples and guidelines.

If you continue to face issues, please provide more details about your implementation, and I would be happy to assist further!

@avinashchilke1 In your code the document builder position is not moved to the matched text when image is inserted. So the images are inserted at the beginning of the document. You can use code like this to replace placeholder in the document with image:

string placeholder = "{{Rpt.ExpensesByProgram}}";

Document doc = new Document(@"C:\Temp\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

// Replace placeholder with itself to make it to be represented as a single Run.
FindReplaceOptions opt = new FindReplaceOptions();
opt.UseSubstitutions = true;
doc.Range.Replace(placeholder, "$0", opt);

// Replace the placeholder with an image.
foreach (Run r in doc.GetChildNodes(NodeType.Run, true))
{
    if (r.Text == placeholder)
    {
        r.Text = "";
        builder.MoveTo(r);
        builder.InsertImage(@"C:\Temp\test.png");
    }
}

doc.Save(@"C:\Temp\out.docx");