Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Fri Oct 15, 2021 7:37 am

Hello Team,

I am evaluating Spire.doc for one of the company that i consult for their internal product. The product is of tech-stack ASP.net MVC and deals with creating proposals internally. Currently they are using OpenXML library and have few challenges dealing with HTML. Hence i've proposed Spire.doc hopping that this helps...

So I'm exited with Spire.doc features and ease of usage.

I don't understand what is missing but this is not working as expected.

I have attached a detail document with screenshots explaining my approach and issue.

Kindly revert back with any more details required.

Awaiting your early response.

Thanks

bridgeparents
 
Posts: 2
Joined: Fri Oct 15, 2021 7:33 am

Fri Oct 15, 2021 9:38 am

Hello Wasim,

Thanks for evaluating our Spire.Doc.
For your requirement, please try the following to achieve it.
Code: Select all
 Document doc = new Document();
            doc.LoadFromFile(@"C:\testbookmark.docx");
            BookmarksNavigator navigator = new BookmarksNavigator(doc);
            navigator.MoveToBookmark("TestBookmark");
            TextBodyPart content = navigator.GetBookmarkContent();
            content.Clear();
            Section sec = doc.AddSection();
            Paragraph par = sec.AddParagraph();
            par.AppendHTML(strHTML);
            for (int i = 0; i < sec.Body.ChildObjects.Count;i++ )
            {
                content.BodyItems.Add(sec.Body.ChildObjects[i].Clone());
            }

            navigator.ReplaceBookmarkContent(content);
            doc.Sections.Remove(sec);
            doc.SaveToFile(@"C:\testbookmarkReplaced.docx", FileFormat.Docx);


Feel free to contact us if any questions.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Fri Oct 15, 2021 7:23 pm

Thank you so much..

This seems to be working but I'm still wondering why can't it just be simple and replace the text in one call?
May be there should something that encapsulate this complexity and have a method just like you have for simple text(like below).

navigator.ReplaceBookmarkContent("New Bookmark Content", true);


Also, there is another small issue with positioning of replacement, attached are the screenshots that you can refer to..

Appreciate all the help..

bridgeparents
 
Posts: 2
Joined: Fri Oct 15, 2021 7:33 am

Mon Oct 18, 2021 7:34 am

Dear Wasim,

You're welcome.
I have talked our developer about a method like navigator.ReplaceBookmarkContent("New html string", true) , sorry we can't add such method because it's not friendly, and some customers need to adjust the generated format later. So the current way to replace bookmark content with html string has good control.

For the issue of positioning of replacement, please refer to the following code to keep the original indent of paragraph.
Code: Select all
 static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile(input file);
            ReplaceBookmarkContent(doc, "TestBookmark", strHTML );
            doc.SaveToFile(@"C:\testbookmarkReplaced.docx", FileFormat.Docx);
        }
        static void ReplaceBookmarkContent(Document doc, string bookmarkName, string newHtmlString)
        {
            BookmarksNavigator navigator = new BookmarksNavigator(doc);
            navigator.MoveToBookmark(bookmarkName);
            TextBodyPart content = navigator.GetBookmarkContent();
            Paragraph paragraph;
            float leftIndent = 0;
            if (content.BodyItems.Count > 0)
            {
                if (content.BodyItems[0] is Paragraph)
                {
                    paragraph = (Paragraph)content.BodyItems[0];
                    leftIndent = paragraph.Format.LeftIndent;
                }
            }
            content.Clear();
            Section sec = doc.AddSection();
            Paragraph par = sec.AddParagraph();
            par.AppendHTML(newHtmlString);

            for (int i = 0; i < sec.Body.ChildObjects.Count; i++)
            {
                if (sec.Body.ChildObjects[i] is Paragraph)
                {
                    paragraph = (Paragraph)sec.Body.ChildObjects[i];
                    if (paragraph.ListFormat.CurrentListLevel != null)
                    {
                        paragraph.Format.LeftIndent = paragraph.ListFormat.CurrentListLevel.TextPosition + leftIndent;
                    }
                    else
                    {
                        paragraph.Format.LeftIndent += leftIndent;
                    }
                }

                content.BodyItems.Add(sec.Body.ChildObjects[i].Clone());
            }

            navigator.ReplaceBookmarkContent(content);
            doc.Sections.Remove(sec);
        }


If the code can't work, please provide your source file.Thanks.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Fri Oct 22, 2021 8:17 am

Dear Wasim,

Creetings from E-iceblue!
Have your issues been solved?
Looking forward to your feedback.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.Doc