About caption insertion to image & table column hide in Aspose.word template

Could you please help me on add caption to image & how hide column in Aspose template word report.

Could you please share tutorials and code example related to Aspose word table column hide and caption insertion in template document.

@rose.thomas01

Summary
You can add a caption to an image and hide a table column in a Word .docx template by using Aspose.Words for .NET. The caption is created with DocumentBuilder.InsertCaption, and a column can be made invisible by setting its width to 0 pt and marking each cell in that column as hidden (CellFormat.Hidden = true).

Why it happens

  • Caption – Word stores a caption as a SEQ field that follows the picture. InsertCaption generates this field automatically, so you do not need to build the field text yourself.
  • Column hide – Word has no explicit “hide column” flag. Making the column width 0 removes the visual space, and setting the cells to hidden prevents them from being selected or printed. Aspose.Words exposes both the column width (TableColumn.Width) and the cell‑format hidden flag (Cell.CellFormat.Hidden).

What to do next

  1. Add the NuGet package
dotnet add package Aspose.Words
  1. Load the template
using Aspose.Words;
using Aspose.Words.Tables;
using Aspose.Words.Drawing;

Document doc = new Document("Template.docx"); // .docx template
DocumentBuilder builder = new DocumentBuilder(doc);
  1. Insert an image with a caption
// Move to the bookmark (or any location) where the picture should appear
builder.MoveToBookmark("ImagePlaceholder");

// Insert the picture
Shape picture = builder.InsertImage(@"C:\Images\MyPhoto.png");
picture.Width = 300; // points
picture.Height = 200;

// Insert a caption right after the picture
// CaptionType.Image creates a SEQ field of type "Figure"
builder.InsertCaption(
CaptionType.Image, // picture caption
"My picture caption", // custom text after the number
true); // add a paragraph break after the caption

If the picture already exists in the template, locate the Shape (e.g., by its Name or AlternativeText) and call builder.MoveTo(shape); builder.InsertCaption(...);.

  1. Hide a specific table column
// Get the first table in the document (adjust if you need a different one)
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);

int columnIndex = 2; // 0‑based index of the column to hide
TableColumn column = table.Columns[columnIndex];

// 1) Set column width to 0 points
column.Width = 0;

// 2) Mark every cell in that column as hidden
foreach (Row row in table.Rows)
{
Cell cell = row.Cells[columnIndex];
cell.CellFormat.Hidden = true;
}
  1. Save the modified document
doc.Save("ReportWithCaptionAndHiddenColumn.docx");
  1. Full minimal example
using Aspose.Words;
using Aspose.Words.Tables;
using Aspose.Words.Drawing;

class Program
{
static void Main()
{
Document doc = new Document("Template.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

// ---- Insert image + caption ----
builder.MoveToBookmark("ImagePlaceholder");
Shape pic = builder.InsertImage(@"C:\Images\Sample.png");
pic.Width = 300;
pic.Height = 200;
builder.InsertCaption(CaptionType.Image, "Sample picture", true);

// ---- Hide column (3rd column) ----
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);
int hideIdx = 2; // zero‑based
TableColumn col = table.Columns[hideIdx];
col.Width = 0;
foreach (Row r in table.Rows)
r.Cells[hideIdx].CellFormat.Hidden = true;

// ---- Save ----
doc.Save("Result.docx");
}
}

Additional tips

Situation Recommended approach
Multiple images Loop through your data source and call InsertImage + InsertCaption inside the loop.
Dynamic column index Find the column by header text, e.g., table.Rows[0].Cells.First(c => c.GetText().Trim() == "Header").ParentRow.Index, then hide it.
Preserve formatting After hiding a column, you may want to clear any shading: cell.CellFormat.Shading.BackgroundPatternColor = Color.White.
Printing Cells marked as hidden are not printed, but verify printer settings if you notice unexpected blank spaces.

These snippets are compatible with recent versions of Aspose.Words for .NET. Adjust file paths, bookmark names, and column indexes to match your actual template.

:warning: I could not fully verify this explanation against the retrieved sources. Please double-check the behaviour in your environment and confirm the exact SDK/version.

2 Likes

@rose.thomas01

Captions in MS Word documents are represented with simple text followed by SEQ field. So you can insert caption using code like this:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Insert table caption
builder.Write("Table ");
builder.InsertField("SEQ Table");

builder.Writeln();

// Insert image caption.
builder.Write("Image ");
builder.InsertField("SEQ Image");

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

In MS Word documents’ tables there is no “column” concept. Each row in the table is independent and can contain any number of cells. Please see our documentation for more information:
https://docs.aspose.com/words/python-net/working-with-columns-and-rows/

2 Likes

Thanks for the response, Is there any If expression can we use in template for hide columns?If yes could you pls share the sample code for the same?

@rose.thomas01 Do you mean LINQ Reporting Syntax? If so, please see our documentation to learn how to achieve this:
https://docs.aspose.com/words/net/working-with-table-column-conditional-blocks/

1 Like