Spire.Doc for .NET is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files from any .NET platform (C#, VB.NET, ASP.NET, .NET Core) with fast and high quality performance.

Wed Feb 23, 2011 9:37 am

hi all,
I have a Bookmark that contains some HTML code added by
Code: Select all
Paragraph p2 = text.GetAsOneRange().OwnerParagraph;
...         
//start bookmark
p2.AppendBookmarkStart(bookName);
p2.AppendHTML(sysvar.ValueString);
textRange.Text = string.Empty;
//end bookmark
p2.AppendBookmarkEnd(bookName);

Ok, now I need to change the content of this bookmark.
I tried some code like this...
Code: Select all
 
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(_doc);
bookmarkNavigator.MoveToBookmark(_bm.Name);
bookmarkNavigator.DeleteBookmarkContent(true, true);

Paragraph _p5= new Paragraph(_doc); // ?????
_p5.AppendHTML(sysvar.ValueString); // ??????
bookmarkNavigator.InsertParagraph(_p5); //??????

But this isn't the correct way to do this.
Can you give me any suggestion?
Thanks in advance.

michelap
 
Posts: 6
Joined: Mon Jan 17, 2011 7:44 am

Thu Feb 24, 2011 2:42 am

I have tested your code, and it replaces the former bookmark successfully. I think that maybe your "html" file contains many <p> tags.
e-iceblue support
User avatar

iceblue support
 
Posts: 240
Joined: Tue Dec 21, 2010 2:56 am

Thu Feb 24, 2011 8:49 am

Sorry but my code crash at line
Code: Select all
Paragraph _p5= new Paragraph(_doc); // ?????
_p5.AppendHTML("<span> some text</span>"); // CRASH

and give me a NullReferenceException

My document contains about 30 bookmarks...

some info: W7 x64, VS 2010, Office 2010
can you send me some running code?
Thanks for your help.

michelap
 
Posts: 6
Joined: Mon Jan 17, 2011 7:44 am

Fri Feb 25, 2011 3:52 am

If you want to append HTML into the paragraph, you should add the paragraph to the section. And we write the code for multiple paragraphs in HTML to replace. Full example:

Document document = new Document();
Section section = document.AddSection();
Paragraph p1 = section.AddParagraph();
p1.AppendBookmarkStart("bookmark1");
p1.AppendHTML("<p> Paragraph1</p>");
p1.AppendBookmarkEnd("bookmark1");

Paragraph p2 = section.AddParagraph();
p2.AppendBookmarkStart("bookmark2");
p2.AppendHTML("<p>Paragraph2</p>");
//end bookmark
p2.AppendBookmarkEnd("bookmark2");
Paragraph p3 = section.AddParagraph();
p3.AppendBookmarkStart("bookmark3");
p3.AppendHTML("<p> Paragraph3</p>");
p3.AppendBookmarkEnd("bookmark3");
Paragraph p4 = section.AddParagraph();
p4.AppendBookmarkStart("bookmark4");
p4.AppendHTML("<p> Paragraph4</p>");
p4.AppendBookmarkEnd("bookmark4");

BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
bookmarkNavigator.MoveToBookmark("bookmark2");

//create a temp section to container multiple paragraph.
Section tempSection = document.AddSection();
String html
= "<p>The <font color=\"#ff0000\">replaced</font> bookmark</p><p>The <font color=\"#ff0000\">replaced</font> paragraph5</p>";
tempSection.AddParagraph().AppendHTML(html);
ParagraphBase replacementFirstItem
= tempSection.Paragraphs[0].Items.FirstItem as ParagraphBase;
ParagraphBase replacementLastItem
= tempSection.Paragraphs[tempSection.Paragraphs.Count - 1].Items.LastItem as ParagraphBase;
TextBodySelection selection
= new TextBodySelection(replacementFirstItem, replacementLastItem);
TextBodyPart part = new TextBodyPart(selection);

//replace
bookmarkNavigator.ReplaceBookmarkContent(part);

//remove temp section
document.Sections.Remove(tempSection);

document.SaveToFile("Sample.doc");
System.Diagnostics.Process.Start("Sample.doc");
e-iceblue support
User avatar

iceblue support
 
Posts: 240
Joined: Tue Dec 21, 2010 2:56 am

Return to Spire.Doc