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. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

HowTo: Insert 1-N paragraphs at a BookMark

Thread ID:

Created:

Updated:

Platform:

Replies:

78142 Dec 1,2008 09:09 PM UTC Dec 3,2008 12:55 PM UTC ASP.NET Web Forms (Classic) 4
loading
Tags: DocIO
DUPAnthony Yott
Asked On December 1, 2008 09:09 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


DUPAnthony Yott
Replied On 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);
}


Bhuvaneswari P [Syncfusion]
Replied On 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



DUPAnthony Yott
Replied On 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





Bhuvaneswari P [Syncfusion]
Replied On 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



CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;