We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

HowTo: Insert 1-N paragraphs at a BookMark

Folks, I'm pretty new to this product and was wondering what is the best approach for inserting 1-N (i.e., 1 or more) paragraphs at a BookMark. Is a BookMark even the right approach? I have a specific place within the document where I will need to insert 1 or more paragraphs.

Thanks in advance,
Anthony Yott


4 Replies

DY DUPAnthony Yott December 1, 2008 10:44 PM UTC



>Folks, I'm pretty new to this product and was wondering what is the best approach for inserting 1-N (i.e., 1 or more) paragraphs at a BookMark. Is a BookMark even the right approach? I have a specific place within the document where I will need to insert 1 or more paragraphs.

Thanks in advance,
Anthony Yott



I found a solution but not sure if it is the correct solution. However, it appears the first paragraph does not format the paragraph the way I intended. What am I doing wrong?

private void InsertParagraphsAtBookMark(IWordDocument doc,
string bookMarkName,
string[] paragraphs)
{
BookmarksNavigator bk = new BookmarksNavigator(doc);
bk.MoveToBookmark(bookMarkName);

IWParagraphStyle paraStyle = doc.AddParagraphStyle("ValidationReportBodyTextStyle");
paraStyle.CharacterFormat.FontName = "Times New Roman";
paraStyle.CharacterFormat.FontSize = 12f;
paraStyle.ParagraphFormat.AfterSpacing = 12f;

TextBodyPart textBodyPart = new TextBodyPart(doc as WordDocument);

foreach (var paragraph in paragraphs)
{
IWParagraph newPara = new WParagraph(doc);
//newPara.ApplyStyle(paraStyle.Name);
newPara.ApplyStyle(doc.Styles[doc.Styles.Count - 1].Name);
newPara.Text = paragraph;

textBodyPart.BodyItems.Add(newPara);
}

bk.ReplaceBookmarkContent(textBodyPart);
}



BP Bhuvaneswari P Syncfusion Team December 2, 2008 12:35 PM UTC

Hi Anthony,

I am able to reproduce the problem. This is not an issue. This is the behavior of DocIO while replacing the content, it replace with the existing content format. We can achieve this by deleting the content and insert the paragraph item one by one by using the below code snippet:


bk.DeleteBookmarkContent(false ); //Remove the content and its existing format
foreach (string paragraph in paragraphs)
{
IWParagraph newPara = new WParagraph(doc);
newPara.ApplyStyle("ValidationReportBodyTextStyle");
//newPara.ApplyStyle(doc.Styles[doc.Styles.Count - 1].Name);
newPara.Text = paragraph;
IParagraphItem item = bk.InsertParagraphItem(ParagraphItemType.TextRange);
item = newPara.Items[0];
}

But it’s not adding the items into the bookmarks we suspect this issue to be a defect and we have forwarded this to our development team for more analysis. We will update you in two business days with more details.

Thank you for your interest in Syncfusion Essential Studio.

Best Regards,
Bhuvana




DY DUPAnthony Yott December 2, 2008 07:04 PM UTC

Bhuvana, thanks for the response but adding the DeleteBookmarkContent is not helping with my current code. See below:

private void InsertParagraphsAtBookMark(IWordDocument doc,
string bookMarkName,
string[] paragraphs,
string styleName)
{
var bk = new BookmarksNavigator(doc);
bk.MoveToBookmark(bookMarkName);
bk.DeleteBookmarkContent(false);

var textBodyPart = new TextBodyPart(doc as WordDocument);

foreach (var paragraph in paragraphs)
{
IWParagraph newPara = new WParagraph(doc);
newPara.ApplyStyle(styleName);
newPara.Text = paragraph;
textBodyPart.BodyItems.Add(newPara);
}

bk.ReplaceBookmarkContent(textBodyPart);
}

>Hi Anthony,

I am able to reproduce the problem. This is not an issue. This is the behavior of DocIO while replacing the content, it replace with the existing content format. We can achieve this by deleting the content and insert the paragraph item one by one by using the below code snippet:


bk.DeleteBookmarkContent(false ); //Remove the content and its existing format
foreach (string paragraph in paragraphs)
{
IWParagraph newPara = new WParagraph(doc);
newPara.ApplyStyle("ValidationReportBodyTextStyle");
//newPara.ApplyStyle(doc.Styles[doc.Styles.Count - 1].Name);
newPara.Text = paragraph;
IParagraphItem item = bk.InsertParagraphItem(ParagraphItemType.TextRange);
item = newPara.Items[0];
}

But it’s not adding the items into the bookmarks we suspect this issue to be a defect and we have forwarded this to our development team for more analysis. We will update you in two business days with more details.

Thank you for your interest in Syncfusion Essential Studio.

Best Regards,
Bhuvana






BP Bhuvaneswari P Syncfusion Team December 3, 2008 12:55 PM UTC

Hi Anthony,

Thanks for the details.

As I said in the last update, it will not work with your code. Please refer the below code, it will insert each paragraph one by one instead of adding into the TextBody and replace the bookmark content.

bk.DeleteBookmarkContent(false ); //Remove the content and its existing format
foreach (string paragraph in paragraphs)
{
IWParagraph newPara = new WParagraph(doc);
newPara.ApplyStyle("ValidationReportBodyTextStyle");
//newPara.ApplyStyle(doc.Styles[doc.Styles.Count - 1].Name);
newPara.Text = paragraph;
IParagraphItem item = bk.InsertParagraphItem(ParagraphItemType.TextRange);
item = newPara.Items[0];
}

This will not work now, since there is an issue with inserting the paragraph item will update you regarding this by tomorrow.

Best Regards,
Bhuvana



Loader.
Live Chat Icon For mobile
Up arrow icon