Cách thay thế nội dung html trong c#

Thứ hai 10 Tháng bảy 2017 8. 24 giờ sáng

Shruti1009 thân mến,

Cảm ơn phản hồi của bạn.
Đây là mã C# để bạn tham khảo.
Mã. Chọn tất cả      Document document = new Document(@"F:\sample.docx");
      List replacement = new List();

      //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 locations = new List();
      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 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
        {
            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);
            }
        }

Nếu vẫn còn sự cố, vui lòng cung cấp cho chúng tôi tệp đầu vào và HTML để kiểm tra.

Cảm ơn,
Betsy
Nhóm hỗ trợ E-iceblue

Cách thay thế nội dung html trong c#

Cách thay thế nội dung html trong c#

Betsy. bài đăng của giang. 3099Đã tham gia. T3 06/09/2016 8. 30 giờ sáng

T3 11/07/2017 2. 12 giờ sáng

Shruti1009 thân mến,

Cảm ơn phản hồi của bạn.
Nếu cũng có bảng HTML, vui lòng thay đổi Danh sách
Toàn bộ mã để bạn tham khảo.
Mã. Chọn tất cả            Document document = new Document(@"F:\sample.docx");
            //collect document object, it could get paragraph, table and so on.
            List replacement = new List();

            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 locations = new List();
            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 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
        {
            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);
            }
        }

Nếu có bất kỳ câu hỏi nào, vui lòng cho tôi biết.

Trân trọng,
Betsy
Nhóm hỗ trợ E-iceblue

Cách thay thế nội dung html trong c#

Cách thay thế nội dung html trong c#

Betsy. bài đăng của giang. 3099Đã tham gia. T3 06/09/2016 8. 30 giờ sáng

T2 21/12/2020 7. 17 giờ sáng

Xin chào nhóm hỗ trợ E-iceblue,
Tôi gặp sự cố với html hiển thị. khi tôi thay thế văn bản bằng html từ mẫu từ, nội dung của bảng không hiển thị trong. tệp đầu ra pdf, dưới đây là mã và html của tôi.
Xin hãy giúp đỡ.
Cảm ơn.
Mã. Chọn tất cả    public class SpireHelper
    {
        public static Document ReplaceTextWithHtml(Document document, string wordToReplace, string html)
        {

            List replacement = new List();

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

            TextSelection[] selections = document.FindAllString("[" + wordToReplace + "]", false, true);
            List locations = new List();
            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);

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

//Html


   


     
         
           
               Kính gửi:         -           BAN LÃNH ĐẠO


   

         

  •          
               
                   We often come across a scenario where we need to merge data to the merge fields which are created by some others

  •      

  •          
               
                   The MailMerge class in Spire.Doc.Reporting namespace exposes the following methods which return a collection of merge field names or group (region) names in a word document

  •    

   


     
         


   


     
         We often come across a scenario where we need to merge data to the merge fields which are created by some others, and we are not sure about the merge fields’ names. So in order to accomplish the mail merge purpose, first we need to read the names of all the merge fields


   


     
         


   
     
         
           
           
           
           
           
           
         
         
           
           
           
           
           
           
         
         
           
           
           
           
           
           
         
     
   

               

Num


           

               


                 
                     
                       
                           Name


           

               


                 
                     
                       
                           No.


           

               


                 
                     
                       
                           Amount


           

               


                 
                     
                       
                           UP


           

               


                 
                     
                       
                           Total


           

               


                 
                     
                        1


           

               


                 
                     
                        Đường thô


           

               


                 
                     
                        120015A, 120017A


           

               


                 
                     
                        22,500


           

               


                 
                     
                        403.98


           

               


                 
                     
                        1,711,800


           

               
                 

               
                 

               


                 
                     
                       


           

               


                 
                     
                       


           

               


                 
                     
                       
                           TỔNG CỘNG


           

               


                 
                     
                       
                           1,711,800


           

   


     
         


   


     
                  Kính trình Ban lãnh đạo thuận duyệt chi phí trên để P. KDQT triển khai thực hiện đảm bảo đúng lịch nhập hàng.


   


     
         


   


     
            Trân trọng kính trình./






Cách thay thế nội dung html trong c#

thienthaiho Bài đăng. 3Đã tham gia. Thứ ba 28/04/2020 6. 44 giờ sáng

T2 21/12/2020 3. 15 giờ chiều

Kính gửi Brian,

Cảm ơn phản hồi của bạn,
nhưng bảng table in the code secment below cannot display in .pdf result file.

Mã. Chọn tất cả


   


     
         
           
               Kính gửi:         -           BAN LÃNH ĐẠO


   

         

  •          
               
                   We often come across a scenario where we need to merge data to the merge fields which are created by some others

  •      

  •          
               
                   The MailMerge class in Spire.Doc.Reporting namespace exposes the following methods which return a collection of merge field names or group (region) names in a word document

  •    

   


     
         


   


     
         We often come across a scenario where we need to merge data to the merge fields which are created by some others, and we are not sure about the merge fields’ names. So in order to accomplish the mail merge purpose, first we need to read the names of all the merge fields


   


     
         


   
     
         
           
           
           
           
           
           
         
         
           
           
           
           
           
           
         
         
           
           
           
           
           
           
         
     
   

               

Num


           

               


                 
                     
                       
                           Name


           

               


                 
                     
                       
                           No.


           

               


                 
                     
                       
                           Amount


           

               


                 
                     
                       
                           UP


           

               


                 
                     
                       
                           Total


           

               


                 
                     
                        1


           

               


                 
                     
                        Đường thô


           

               


                 
                     
                        120015A, 120017A


           

               


                 
                     
                        22,500


           

               


                 
                     
                        403.98


           

               


                 
                     
                        1,711,800


           

               
                 

               
                 

               


                 
                     
                       


           

               


                 
                     
                       


           

               


                 
                     
                       
                           TỔNG CỘNG


           

               


                 
                     
                       
                           1,711,800


           

   


     
         


   


     
                  Kính trình Ban lãnh đạo thuận duyệt chi phí trên để P. KDQT triển khai thực hiện đảm bảo đúng lịch nhập hàng.


   


     
         


   


     
            Trân trọng kính trình./




Đăng nhập để xem các tập tin đính kèm với bài viết này

Cách thay thế nội dung html trong c#

thienthaiho Bài đăng. 3Đã tham gia. Thứ ba 28/04/2020 6. 44 giờ sáng