Hello
We're evaluating DocIo in order to use it into our printing process.
We have a .doc model with labels (constant texts) and variables (bookmarks).
The .doc model may have also tables: the first row contains cell titles, the second one contains data cells; each data cells contains one (or maybe more) bookmarks.
The printing process uses the .doc model to create a new (final) document, just substituting bookmarks with variable values.
We tried BookmarksNavigator, and it is great :-)
We just find some difficulties with tables.
We'd need to populate the table, inserting many new lines; each line need to be cloned by the first one, in which we resolve bookmark values.We don't need to clone bookmarks, but we need to use the second table row as a model for all the tables row we add.
Attached to this post you'll find:
- a sample of .doc model (model.doc)
- a sample of the .doc resulting file we'd need to produce (final document.doc)
Do you have any suggestion to perform these operations?
We tried to copy the row with bookmarks into another WordDocument, set the bookmark values, and then add the row to the original table into the final document, but we got an exception (cannot add row from other document). Do you have any code sample we could follow?
Thank you very much!
Best regards
Chiara Marzano
PS: we know... MailMarge would be the better way :-)... but our .doc models may be created with OpenOffice tools, and fields are not managed in the same way :-(
Thank you in advance for any help :-)
Hi Chiara,
Sample:
https://www.syncfusion.com/downloads/Support/DirectTrac/98975/Sample924521150.zip
Ramkumar
Thank you very much, Ramkumar!
Your sample was very helpful! :-)
Now, we're "playing" always with tables, cloning rows and substituting cell's text (variables are simple text, instead of bookmarks).
Thank you very much
Best regards!
Hi Ricerca,
You
are welcome. Please let us know if you have any question
Regards
Ramkumar
BookmarksNavigator bk = new BookmarksNavigator(document);//Finds the text within the table.TextSelection textSelcetion = document.Find("TemplateRow", false, true);//Gets the TextRange from the selection.WTextRange textRange = textSelcetion.GetRanges()[0];
BookmarksNavigator bk = new BookmarksNavigator(document);// Move to the bookmark in word documentbk.MoveToBookmark("TemplateRow");//Finds the text within the table.TextSelection textSelcetion = bk.GetBookmarkContent(); (????)//Gets the TextRange from the selection.WTextRange textRange = textSelcetion.GetRanges()[0];
Hi Pedro,
Thank you for contacting Syncfusion support.
Yes, it is possible to access a table through bookmark and access the contents of the table. Kindly refer to the following code example to achieve this.
Code example:
// Move to the bookmark in word document
bk.MoveToBookmark("Bookmark1");
//Gets the content for the current bookmark
TextBodyPart part = bk.GetBookmarkContent();
//Iterates the text body part contents
foreach (Entity entity in part.BodyItems)
{
//Gets the table
if (entity is WTable)
{
//Process further on tables
WTable table = entity as WTable;
}
}
Please refer to the following UG documentation link to know more about Bookmarks and Bookmark navigator in DocIO:
https://help.syncfusion.com/windowsforms/docio/working-with-bookmarks
Please refer to the following UG documentation link to know more about tables in DocIO:
https://help.syncfusion.com/windowsforms/docio/working-with-tables
Regards,
Sathish
Hi Pedro,
Kindly use the below code to access the bookmark contents with respect to your test case.
Code example:
//Move to the bookmark in word document
bk.MoveToBookmark("Bookmark1");
//Gets the content for the current bookmark
TextBodyPart part = bk.GetBookmarkContent();
//Iterates the text body part contents
foreach (Entity entity in part.BodyItems)
{
//Gets the table
if (entity is WTable)
{
//Process further on tables
WTable table = entity as WTable;
}
else if (entity is WParagraph)
{
//Process further on the paragraph
WParagraph paragraph = entity as WParagraph;
}
}
Regards,
Sathish
Hi Pedro,
Solution 1:
Kindly use the below code to get the WTable entity from the WParagraph entity.
Code example:
//Gets the bookmark by it's name
Bookmark bookmark = document.Bookmarks.FindByName("Bookmark1");
//Gets the owner paragraph of Bookmart start
WParagraph para = bookmark.BookmarkStart.OwnerParagraph;
//Checks whether paragaph is defined within table cell
if (para.IsInCell)
{
//Gets the table
WTable table = GetTable(para);
}
private WTable GetTable(Entity entity)
{
//Checks until WTable entity
while (!(entity is WTable))
{
if (entity.Owner != null)
entity = entity.Owner;
else
break;
}
return entity as WTable;
}
Solution 2:
Alternatively you shall modify your template document as below and get the WTable instance using BookamrkNavigator as suggested earlier.
Code example for Bookmark navigator:
//Move to the bookmark in word document
bk.MoveToBookmark("Bookmark1");
//Gets the content for the current bookmark
TextBodyPart part = bk.GetBookmarkContent();
//Iterates the text body part contents
foreach (Entity entity in part.BodyItems)
{
//Gets the table
if (entity is WTable)
{
//Process further on tables
WTable table = entity as WTable;
}
else if (entity is WParagraph)
{
//Process further on the paragraph
WParagraph paragraph = entity as WParagraph;
}
}
Regards,
Sathish