Replace text with html content

Technical support for Spire.Doc

Moderator: iceblue support

Replace text with html content

Postby shruti1009 » Mon Jul 10, 2017 6:44 am

I want to replace text in a word template with HTML. I know how to append HTML to last paragraph but this paragraph is in the middle of word document.
shruti1009
 
Posts: 4
Joined: Mon Jul 10, 2017 6:42 am

Re: Replace text with html content

Postby Betsy.jiang » Mon Jul 10, 2017 7:04 am

Dear shruti1009,

Thanks for your inquiry. Here is a guide to replace text with HTML.
https://www.e-iceblue.com/Tutorials/Spi ... -HTML.html
Hope this helps. If you still have the issue, please provide us with the input file and the HTML, then we will provide corresponding code for you.

Sincerely,
Betsy
E-iceblue support team
User avatar
Betsy.jiang
 
Posts: 1650
Joined: Tue Sep 06, 2016 8:30 am

Re: Replace text with html content

Postby shruti1009 » Mon Jul 10, 2017 7:53 am

No this is not helpful for me. I have one word doc file in which i have placed one word eg. {{{wordToReplace}}} and i want to replace this place holder to HTML content. I need this code in c#. I have a licensed version of spire doc. Please help me out.
shruti1009
 
Posts: 4
Joined: Mon Jul 10, 2017 6:42 am

Re: Replace text with html content

Postby Betsy.jiang » Mon Jul 10, 2017 8:24 am

Dear shruti1009,

Thanks for your response.
Here is C# code for your kind reference.
Code: Select all
      Document document = new Document(@"F:\sample.docx");
      List<Paragraph> replacement = new List<Paragraph>();

      //create a temp section to contains HTML
      Section tempSection = document.AddSection();
      Paragraph p4 = tempSection.AddParagraph();
      p4.AppendHTML("your HTML");
      foreach (var par in tempSection.Paragraphs)
      {
            Paragraph para = par as Paragraph;
            replacement.Add(para);
      }

      TextSelection[] selections = document.FindAllString("{{{wordToReplace}}}", false, true);
      List<TextRangeLocation> locations = new List<TextRangeLocation>();
      foreach (TextSelection selection in selections)
      {
         locations.Add(new TextRangeLocation(selection.GetAsOneRange()));
      }
      locations.Sort();
      foreach (TextRangeLocation location in locations)
      {
         Replace(location, replacement);
      }

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

      document.SaveToFile("11052.docx",FileFormat.Docx);
   }

        private static void Replace(TextRangeLocation location, IList<Paragraph> replacement)
        {
            //will be replaced
            TextRange textRange = location.Text;

            //textRange index
            int index = location.Index;

            //owener paragraph
            Paragraph paragraph = location.Owner;

            //owner text body
            Body sectionBody = paragraph.OwnerTextBody;

            //get the index of paragraph in section
            int paragraphIndex = sectionBody.ChildObjects.IndexOf(paragraph);
            int replacementIndex = -1;
            if (index == 0)
            {
                //remove
                paragraph.ChildObjects.RemoveAt(0);

                replacementIndex = sectionBody.ChildObjects.IndexOf(paragraph);
            }
            else if (index == paragraph.ChildObjects.Count - 1)
            {
                paragraph.ChildObjects.RemoveAt(index);
                replacementIndex = paragraphIndex + 1;
            }
            else
            {
                //split owner paragraph
                Paragraph paragraph1 = (Paragraph)paragraph.Clone();
                while (paragraph.ChildObjects.Count > index)
                {
                    paragraph.ChildObjects.RemoveAt(index);
                }
                int i = 0;
                int count = index + 1;
                while (i < count)
                {
                    paragraph1.ChildObjects.RemoveAt(0);
                    i += 1;
                }
                sectionBody.ChildObjects.Insert(paragraphIndex + 1, paragraph1);

                replacementIndex = paragraphIndex + 1;
            }
            //insert replacement
            for (int i = 0; i <= replacement.Count - 1; i++)
            {
                sectionBody.ChildObjects.Insert(replacementIndex + i, replacement[i].Clone());
            }
        }
        public class TextRangeLocation : IComparable<TextRangeLocation>
        {
            public TextRangeLocation(TextRange text)
            {
                this.Text = text;
            }
            public TextRange Text
            {
                get { return m_Text; }
                set { m_Text = value; }
            }
            private TextRange m_Text;
            public Paragraph Owner
            {
                get { return this.Text.OwnerParagraph; }
            }
            public int Index
            {
                get { return this.Owner.ChildObjects.IndexOf(this.Text); }
            }
            public int CompareTo(TextRangeLocation other)
            {
                return -(this.Index - other.Index);
            }
        }

If there is still issue, please provide us with the input file and the HTML for testing.

Thanks,
Betsy
E-iceblue support team
User avatar
Betsy.jiang
 
Posts: 1650
Joined: Tue Sep 06, 2016 8:30 am

Re: Replace text with html content

Postby shruti1009 » Mon Jul 10, 2017 10:56 am

Thanks for ur reply. Your code helped me partially. Its working for all Html elements except <table> tag.
Rest all code is working for me. if i add simple html code in p4.AppendHTML("<b>hello world</b>"). It shows me correct Html format. but if i add my html content as
Code: Select all
<table class='widthStyle' style='width:100%;'><tbody><tr><td style='vertical-align: top;'><ul><li>Boat Cover</li><li>Propeller</li><li>Bilge Pump</li><li>Inboard Motors</li></ul></td><td style='vertical-align: top;'><ul style='vertical-align:top;'><li>Canvas Top</li><li>Step Wedges</li><li>Dock Ties</li></ul></td><td style='vertical-align: top;'><ul><li>Fire Extinguisher</li><li>Reboarding Step</li><li>Jet Ski Cover</li></ul></td></tr></tbody></table>

its not working. Is there any problem with <table> tag.
Thank you.
shruti1009
 
Posts: 4
Joined: Mon Jul 10, 2017 6:42 am

Re: Replace text with html content

Postby Betsy.jiang » Tue Jul 11, 2017 2:12 am

Dear shruti1009,

Thanks for your response.
If there is also HTML table, please change the List<Paragraph> to List<DocumentObject>, and adjust the corresponding code.
Whole code for your kind reference.
Code: Select all
            Document document = new Document(@"F:\sample.docx");
            //collect document object, it could get paragraph, table and so on.
            List<DocumentObject> replacement = new List<DocumentObject>();

            Section tempSection = document.AddSection();
            Paragraph p4 = tempSection.AddParagraph();
            p4.AppendHTML("your HTML");

            foreach (var obj in tempSection.Body.ChildObjects)
            {
                DocumentObject O = obj as DocumentObject;
                replacement.Add(O);
            }

            TextSelection[] selections = document.FindAllString("{{{wordToReplace}}}", false, true);
            List<TextRangeLocation> locations = new List<TextRangeLocation>();
            foreach (TextSelection selection in selections)
            {
                locations.Add(new TextRangeLocation(selection.GetAsOneRange()));
            }
            locations.Sort();
            foreach (TextRangeLocation location in locations)
            {
                ReplaceObj(location, replacement);
            }
            //remove the temp section
            document.Sections.Remove(tempSection);
            document.SaveToFile("11052-table.docx", FileFormat.Docx);
        }
        private static void ReplaceObj(TextRangeLocation location, List<DocumentObject> replacement)
        {
            //will be replaced
            TextRange textRange = location.Text;

            //textRange index
            int index = location.Index;

            //owener paragraph
            Paragraph paragraph = location.Owner;

            //owner text body
            Body sectionBody = paragraph.OwnerTextBody;

            //get the index of paragraph in section
            int paragraphIndex = sectionBody.ChildObjects.IndexOf(paragraph);

            int replacementIndex = -1;
            if (index == 0)
            {
                //remove
                paragraph.ChildObjects.RemoveAt(0);

                replacementIndex = sectionBody.ChildObjects.IndexOf(paragraph);
            }
            else if (index == paragraph.ChildObjects.Count - 1)
            {
                paragraph.ChildObjects.RemoveAt(index);
                replacementIndex = paragraphIndex + 1;

            }
            else
            {
                //split owner paragraph
                Paragraph paragraph1 = (Paragraph)paragraph.Clone();
                while (paragraph.ChildObjects.Count > index)
                {
                    paragraph.ChildObjects.RemoveAt(index);
                }
                int i = 0;
                int count = index + 1;
                while (i < count)
                {
                    paragraph1.ChildObjects.RemoveAt(0);
                    i += 1;
                }
                sectionBody.ChildObjects.Insert(paragraphIndex + 1, paragraph1);

                replacementIndex = paragraphIndex + 1;
            }

            //insert replacement
            for (int i = 0; i <= replacement.Count - 1; i++)
            {
                sectionBody.ChildObjects.Insert(replacementIndex + i, replacement[i].Clone());
            }
        }       
       public class TextRangeLocation : IComparable<TextRangeLocation>
        {
            public TextRangeLocation(TextRange text)
            {
                this.Text = text;
            }
            public TextRange Text
            {
                get { return m_Text; }
                set { m_Text = value; }
            }
            private TextRange m_Text;
            public Paragraph Owner
            {
                get { return this.Text.OwnerParagraph; }
            }
            public int Index
            {
                get { return this.Owner.ChildObjects.IndexOf(this.Text); }
            }
            public int CompareTo(TextRangeLocation other)
            {
                return -(this.Index - other.Index);
            }
        }

If there is any question, please let me know.

Sincerely,
Betsy
E-iceblue support team
User avatar
Betsy.jiang
 
Posts: 1650
Joined: Tue Sep 06, 2016 8:30 am

Re: Replace text with html content

Postby shruti1009 » Tue Jul 11, 2017 6:00 am

Dear Betsy,
Thanks for your reply. It worked for me. It helped me alottt. Thanks to you for giving reply on time. :D
shruti1009
 
Posts: 4
Joined: Mon Jul 10, 2017 6:42 am

Re: Replace text with html content

Postby Betsy.jiang » Tue Jul 11, 2017 6:04 am

Dear shruti1009,

Thanks for your feedback.
Please feel free to contact us if there is any question :) .

Sincerely,
Betsy
E-iceblue support team
User avatar
Betsy.jiang
 
Posts: 1650
Joined: Tue Sep 06, 2016 8:30 am


Return to Spire.Doc

Who is online

Users browsing this forum: No registered users and 0 guests