Table of Contents displays Images with TOC Entry

I am new to Aspose.Words and I am trying to create a document that will show a list of images with Descriptions and Add the description to the Table of Contents. The problem I am encountering is the Image displays above the Table of Content entry for each entry. I’m not sure if it will help but here is the code I am using to display each image in the document.

            i = 0;
            foreach (Photo photo in photos)
            {                             
                photo.FileName = photo.FileName.Replace(model.UniqueID + "\\", string.Empty);
                Shape image = new Shape(builder.Document, ShapeType.Image);
                image = builder.InsertImage(Path.Combine(model.PhotoPath, photo.FileName));
                

                if (image.Height > image.Width)
                {
                    image.Height = ConvertUtil.InchToPoint(3.88);
                    builder.InsertBreak(BreakType.LineBreak);
                }
                else
                {
                    image.Width = ConvertUtil.InchToPoint(5.17);
                }
                builder.ParagraphFormat.ClearFormatting();
                i++;

                string caption = "Photograph " + i + ": " + photo.Caption;
         
                builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
                builder.Writeln(caption);
                doc.UpdateFields();

                if (i % 2 == 0)
                {
                    builder.InsertBreak(BreakType.PageBreak);            
                }
                builder.InsertBreak(BreakType.ParagraphBreak);
            }
            doc.UpdateFields();

@bryguy1955

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

@tahir.manzoor
Thank you for your reply. I am sorry I have not been able to respond earlier as I was off for the 3 day weekend.
I am attaching a zip file the contains the information you requested. This project does not require an input word document. There is an images folder in the bin\debug folder that contains some sample images that will be placed in the document. There is also an Output folder in the bin\debug folder that contains the expected output document as well as an example document produced by the console app.
Due to the attachment size limitation I have had to remove the Aspose.Words Nuget Package from the zip file so you will need to install the nuget package prior to running the application.
Please let me know if you have any questions or if you run into any problems executing the application.
Thank you,
Bryan

AsposeDocumentGenerator.zip (3.0 MB)

@bryguy1955

Unfortunately, due to some issue we are unable to download the ZIP file. Please ZIP your documents and code again and attach them. Thanks for your cooperation.

@tahir.manzoor
I rezipped the project folder and it will be attached here .
Please let me know if you still have problems.
Thanks,
BryanAsposeDocumentGenerator.zip (3.0 MB)

@bryguy1955

In your code, you are inserting the image in the paragraph that has ‘Heading 1’ style. Please clear the paragraph formatting as shown below to get the desired output. Moreover, you do not need to call the Document.UpdateFields method twice.

builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");

builder.InsertBreak(BreakType.PageBreak);
builder.ParagraphFormat.ClearFormatting();
builder.Font.ClearFormatting();


font.Bold = false;
font.Size = 12;

i = 0;
foreach (Photo photo in photos)
{
    Shape image = new Shape(builder.Document, ShapeType.Image);
    image = builder.InsertImage(Path.Combine(model.PhotoPath, photo.FileName));
    builder.Writeln(); 
    builder.ParagraphFormat.ClearFormatting();

    if (image.Height > image.Width)
    {
        image.Height = ConvertUtil.InchToPoint(3.88);
        builder.InsertBreak(BreakType.LineBreak);
    }
    else
    {
        image.Width = ConvertUtil.InchToPoint(5.17);
    }
                
    i++;

    string caption = "Photograph " + i + ": " + photo.Caption;

    builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
    builder.Writeln(caption);
    builder.ParagraphFormat.ClearFormatting();
    if (i % 2 == 0)
    {
        builder.InsertBreak(BreakType.PageBreak);
    }
    builder.InsertBreak(BreakType.ParagraphBreak);
}

// A table of contents is a field of a type that needs to be updated to show an up-to-date result.
doc.UpdateFields();

doc.Save(fileName);

@tahir.manzoor
I tried that and it produced the same results as shown in the attached image
Capture.PNG.jpg (131.2 KB)

            if (image.Height > image.Width)
            {
                image.Height = ConvertUtil.InchToPoint(3.88);
                builder.InsertBreak(BreakType.LineBreak);
            }
            else
            {
                image.Width = ConvertUtil.InchToPoint(5.17);
            }

            i++;

            string caption = "Photograph " + i + ": " + photo.Caption;

            builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
            builder.Writeln(caption);
            builder.ParagraphFormat.ClearFormatting();

            if (i % 2 == 0)
            {
                builder.InsertBreak(BreakType.PageBreak);            
            }
            builder.InsertBreak(BreakType.ParagraphBreak);
        }
        doc.UpdateFields();

@bryguy1955

We have attached the modified Visual Studio project with this post. The output document (21.2.doc) is generated by latest version of Aspose.Words for .NET 201.2.
AsposeDocumentGenerator.zip (2.4 MB)

@tahir.manzoor
That has fixed the problem.
You have been very helpful. Thank you so much for your help.
Bryan