Hello, I have a Word Add-in project that uses a table variable of type Word.Table, and a par variable of type Word.Paragraph
I have this: using Word = Microsoft.Office.Interop.Word
I want to know how to migrate these two lines with GemBox.Document
1- table.Range.Next().Paragraphs[1];
2- par.Range.InRange(table.Range)
Hi Mirel,
Here is how you can retrieve the first Paragraph
element after the Table
element with GemBox.Document:
var document = DocumentModel.Load("input.docx");
var table = document.GetChildElements(true).OfType<Table>().First();
var parentCollection = table.ParentCollection;
int index = parentCollection.IndexOf(table);
Paragraph? paragraphAfterTable = null;
for (int i = index + 1; i < parentCollection.Count; i++)
if (parentCollection[i].ElementType == ElementType.Paragraph)
{
paragraphAfterTable = (Paragraph)parentCollection[i];
break;
}
if (paragraphAfterTable != null)
Console.WriteLine(paragraphAfterTable.Content.ToString().TrimEnd());
And here is how you can find out if the Paragraph
is inside the Table
:
var document = DocumentModel.Load("input.docx");
var table = document.GetChildElements(true).OfType<Table>().First();
var paragraph = document.GetChildElements(true).OfType<Paragraph>().ElementAt(3);
string paragraphContent = paragraph.Content.ToString().TrimEnd();
if (paragraph.GetParentElements(ElementType.Table).Any(t => t == table))
Console.WriteLine("Paragraph is inside targeted table: " + paragraphContent);
else
Console.WriteLine("Paragraph is not inside targeted table: " + paragraphContent);
I hope this helps.
Regards,
Mario
1 Like