We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Create Table and Add to Bookmark?

Is it possible to create a table and then attach the table to an existing bookmark? This is something we currently do with the Word Object Model but the performance is lacking.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Yes, I think that you can achieve this using DocumentBuilder. For example see the following code:

Document doc = new Document(@"Test173\in.doc");

DocumentBuilder builder = new DocumentBuilder(doc);

//Move cursor to bookmark start

builder.MoveToBookmark("myBookmark", true, true);

//Insert empty line

builder.Writeln();

//Build table

for (int rowIndex = 0; rowIndex < 5; rowIndex++)

{

for (int cellIndex = 0; cellIndex < 5; cellIndex++)

{

builder.InsertCell();

builder.Write("Table inside bookamrk");

}

builder.EndRow();

}

builder.EndTable();

//Save output document

doc.Save(@"Test173\out.doc");

Hope this helps.

Best regards.

Thanks you for your reply. That works.

Another question: Is it possible to set the cells width to autofit(to the length of the text)? Or the table to autofit? I 'm creating the table dynamically and do not know what the widths of each cell should be?

Thanks

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. By default cell width auto fits to its content. You can also use CellFormat anf RowFormat to configure your table:

http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/aspose.words.tables.cellformatmembers.html

http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/aspose.words.tables.rowformatmembers.html

Best regards.

Alexey,

Thanks for your help.

I'm not having any luck with the autofit. I'm trying to write a table into a table of a templete document. The first inserted cell is taking the width of the cell it is being inserted into.
Here is my code and the templete file:

builder.MoveToBookmark("new", true, true);
builder.StartTable();
builder.RowFormat.AllowAutoFit = true;
builder.InsertCell();
builder.Write("ABCDEFG");
builder.InsertCell();
builder.Write("B");
builder.InsertCell();
builder.Write("C");
builder.InsertCell();
builder.Write("D");
builder.EndRow();
builder.EndTable();

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. I think that in this case you should set width of each cell manually. Please try using the following code:

Table tab = builder.StartTable();

builder.InsertCell();

cell.CellFormat.Width = 50;

builder.Write("ABCDEFG");

builder.InsertCell();

builder.Write("B");

builder.InsertCell();

builder.Write("C");

builder.InsertCell();

builder.Write("D");

builder.EndRow();

builder.EndTable();

Hope this helps.

Best regards.

Alexey,

This is just sample data to write. I will not know the lenth of the text to write so setting the width manually is not an option. Is there no way to have it autofit?

Thanks

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Maybe you can try using the following code as a workaround.

Document doc = new Document(@"Test175\in.doc");

//Create document builder

DocumentBuilder builder = new DocumentBuilder(doc);

//Create another DocumentBuilder

DocumentBuilder secondBuilder = new DocumentBuilder();

//Generate table

Table tab = secondBuilder.StartTable();

secondBuilder.InsertCell();

secondBuilder.Write("ABCDEFG");

secondBuilder.InsertCell();

secondBuilder.Write("B");

secondBuilder.InsertCell();

secondBuilder.Write("C");

secondBuilder.InsertCell();

secondBuilder.Write("D");

secondBuilder.EndRow();

secondBuilder.EndTable();

//Move cursor to the bookamark

builder.MoveToBookmark("new", true, true);

//Import generated table

Node importedTable = doc.ImportNode(tab, true, ImportFormatMode.KeepSourceFormatting);

//Insert table

builder.CurrentParagraph.ParentNode.InsertBefore(importedTable, builder.CurrentParagraph);

//Save result

doc.Save(@"Test175\out.doc");

Hope this helps.

Best regards.

Hello Alexey,

This still isn't auto sizing the cells. It is making all the cells the same width. I tried setting the rows to allowautofit and that doesn't work either. Is there any thing else to try?

Thanks

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. You can try calculating width of cell. For example you can try using the following logic.

Document doc = new Document(@"Test044\in.doc");

//Create document builder

DocumentBuilder builder = new DocumentBuilder(doc);

//Move cursor to the bookamark

builder.MoveToBookmark("new", true, true);

//Generate table

Table tab = builder.StartTable();

builder.InsertCell();

builder.Write("ABCDEFG");

builder.InsertCell();

builder.Write("B");

builder.InsertCell();

builder.Write("C");

builder.InsertCell();

builder.Write("D");

builder.EndRow();

builder.EndTable();

//Update table layout

//Loop through rows in table

foreach (Row myRow in tab.Rows)

{

//Loop trouhg cells

foreach (Cell myCell in myRow.Cells)

{

double widht = 0;

//Loop throuhg Runs inside Cell

foreach (Run myRun in myCell.GetChildNodes(NodeType.Run, true))

{

foreach (Char c in myRun.Text.ToCharArray())

{

if (Char.IsUpper(c))

//If current char is upper case then its width = font size

widht += myRun.Font.Size;

else

//If current char is lower case then its width = font size / 2

widht += myRun.Font.Size / 2;

}

}

//Set width

myCell.CellFormat.Width = widht;

}

}

//Save result

doc.Save(@"Test044\out.doc");

Maybe this helps.

Best regards.