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.

Wed Oct 21, 2015 3:17 pm

I have a problem when trying to output an HTML table to a paragraph. My HTML is an empty paragraph, although it shouldnt be. I have used this example
Tutorials/Spire.Doc/Spire.Doc-Program-Guide/How-to-Replace-Text-in-a-Word-Template-with-HTML.html
only difference is that I have replaced this

Code: Select all
p4.AppendHTML("<p style='text-align: Right'><span style='background-color: yellow'><i><b>here is the topic text for Governing Body Oversight</b></i></span></p><p style='text-align: left'><span style='background-color: yellow'><i><b>&nbsp;&nbsp;&nbsp; fdsafds</b></i></span></p>")


with this

Code: Select all
Dim sWriter As New StringWriter()
        Dim hWriter As New HtmlTextWriter(sWriter)

        Dim GridView1 As New GridView()
        GridView1.DataSource = DataSetAuswertung.Tables(0)
        GridView1.DataBind()
        GridView1.RenderControl(hWriter)
        Dim controlHtml As String = sWriter.ToString()
        p4.AppendHTML(controlHtml)




It works when I output some other HTML text, like the one in the example.

controlHTML is an HTML table and it has correct structure. This is table's HTML

Code: Select all
<div>
   <table cellspacing="0" rules="all" border="1" style="border-collapse:collapse;">
      <tr>
         <th scope="col">Klinik</th><th scope="col">Jahr</th><th scope="col">Code</th><th scope="col">Anzahl</th><th scope="col">Prozent</th><th scope="col">Prioritaet</th>
      </tr><tr>
         <td>*</td><td>*</td><td>&nbsp;</td><td>2</td><td>0</td><td>&nbsp;</td>
      </tr><tr>
         <td>*</td><td>*</td><td>1</td><td>6</td><td>0</td><td>&nbsp;</td>
      </tr><tr>
         <td>*</td><td>*</td><td>2</td><td>83</td><td>0</td><td>&nbsp;</td>
      </tr><tr>
         <td>*</td><td>*</td><td>3</td><td>178</td><td>0</td><td>&nbsp;</td>
      </tr><tr>
         <td>*</td><td>*</td><td>4</td><td>230</td><td>0</td><td>&nbsp;</td>
      </tr><tr>
         <td>*</td><td>*</td><td>5</td><td>265</td><td>0</td><td>&nbsp;</td>
      </tr><tr>
         <td>*</td><td>*</td><td>6</td><td>299</td><td>0</td><td>&nbsp;</td>
      </tr><tr>
         <td>*</td><td>*</td><td>7</td><td>287</td><td>0</td><td>&nbsp;</td>
      </tr><tr>
         <td>*</td><td>*</td><td>8</td><td>271</td><td>0</td><td>&nbsp;</td>
      </tr><tr>
         <td>*</td><td>*</td><td>9</td><td>251</td><td>0</td><td>&nbsp;</td>
      </tr><tr>
         <td>*</td><td>*</td><td>10</td><td>42</td><td>0</td><td>&nbsp;</td>
      </tr><tr>
         <td>*</td><td>*</td><td>11</td><td>1</td><td>0</td><td>&nbsp;</td>
      </tr>
   </table>
</div>

chili
 
Posts: 4
Joined: Wed Oct 21, 2015 6:39 am

Thu Oct 22, 2015 3:36 am

Hello,

Here is sample code for your reference:
Code: Select all
            Document doc = new Document(@"F:\testing\5167\bin\Debug\Summary_of_Science.doc");
            IList<Table> replacement = new List<Table>();
            string z = "<div><table cellspacing=\"0\" rules=\"all\" border=\"1\" style=\"border-collapse:collapse;\"><tr><th scope=\"col\">Klinik</th><th scope=\"col\">Jahr</th><th scope=\"col\">Code</th><th scope=\"col\">Anzahl</th><th scope=\"col\">Prozent</th><th scope=\"col\">Prioritaet</th></tr><tr><td>*</td><td>*</td><td>&nbsp;</td><td>2</td><td>0</td><td>&nbsp;</td></tr><tr><td>*</td><td>*</td><td>1</td><td>6</td><td>0</td><td>&nbsp;</td></tr><tr><td>*</td><td>*</td><td>2</td><td>83</td><td>0</td><td>&nbsp;</td></tr><tr><td>*</td><td>*</td><td>3</td><td>178</td><td>0</td><td>&nbsp;</td></tr><tr><td>*</td><td>*</td><td>4</td><td>230</td><td>0</td><td>&nbsp;</td></tr><tr><td>*</td><td>*</td><td>5</td><td>265</td><td>0</td><td>&nbsp;</td></tr><tr><td>*</td><td>*</td><td>6</td><td>299</td><td>0</td><td>&nbsp;</td></tr><tr><td>*</td><td>*</td><td>7</td><td>287</td><td>0</td><td>&nbsp;</td></tr><tr><td>*</td><td>*</td><td>8</td><td>271</td><td>0</td><td>&nbsp;</td></tr><tr><td>*</td><td>*</td><td>9</td><td>251</td><td>0</td><td>&nbsp;</td></tr><tr><td>*</td><td>*</td><td>10</td><td>42</td><td>0</td><td>&nbsp;</td></tr><tr><td>*</td><td>*</td><td>11</td><td>1</td><td>0</td><td>&nbsp;</td></tr> </table></div>";
            Section tempSection1 = doc.AddSection();
            Paragraph p41 = tempSection1.AddParagraph();

            p41.AppendHTML(z);
            foreach (Table tempP_loopVariable in tempSection1.Tables)
            {
                replacement.Add(tempP_loopVariable);
            }

            TextSelection[] selections = doc.FindAllString("Situacao", 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
            doc.Sections.Remove(tempSection1);

            doc.SaveToFile("6068.docx");
            System.Diagnostics.Process.Start("6068.docx");
        }

        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);
            }
        }

        private static void Replace(TextRangeLocation location, IList<Table> 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());
            }
        }

If there is any question, welcome to get it back to us.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Fri Oct 23, 2015 8:01 am

Hello,

Did you try the code I provided ? Has it met your requirement ?
Thanks for your feedback.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Tue Oct 27, 2015 9:33 am

Hi Betsi,

it solved the problem. Thanks a lot.

Best,
Filip

chili
 
Posts: 4
Joined: Wed Oct 21, 2015 6:39 am

Wed Oct 28, 2015 1:30 am

Hi Filip,

Thanks for your reply. Please feel free to contact us if you have any question.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Return to Spire.Doc

cron