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.

Tue Oct 31, 2017 9:25 am

Hello,


I would like to insert paragraphs between two paragraphs already existing in the document.
however, when I insert paragraphs it writes on the following paragraph.

Code: Select all
 TitleStyle("TESTESTTEST");

            Paragraph parag = new Paragraph(this.doc);

            Paragraph paragraph = new Paragraph(this.doc);
            paragraph.AppendText("Description");
            paragraph.ApplyStyle("Titre3");

            indexdoc++;
            section.Body.ChildObjects.Insert(indexdoc, paragraph);
            indexdoc++;
            section.Body.ChildObjects.Insert(indexdoc, parag);

            Paragraph paragraph1 = new Paragraph(this.doc);
            paragraph1.AppendText("diohjuize huidhezu idhezuid hezuihde uzihdezdhez cbcjsdnjkc diohjuize huidhezu idhezuid hezuihde uzihdezdhez cbcjsdnjkc");
            paragraph1.ApplyStyle("Normal");

            indexdoc++;
            section.Body.ChildObjects.Insert(indexdoc, paragraph1);
            indexdoc++;
            section.Body.ChildObjects.Insert(indexdoc, parag);

            Paragraph paragraph2 = new Paragraph(this.doc);
            paragraph2.AppendText("STATEEEEE");
           

            indexdoc++;
            section.Body.ChildObjects.Insert(indexdoc, paragraph2);
            indexdoc++;
            section.Body.ChildObjects.Insert(indexdoc, parag);

            Paragraph paragraph3 = new Paragraph(this.doc);
            paragraph3.AppendText("");

            indexdoc++;
            section.Body.ChildObjects.Insert(indexdoc, paragraph3);
            indexdoc++;
            section.Body.ChildObjects.Insert(indexdoc, parag);


Please check the attach document.

Thank you

elysiumsecurity
 
Posts: 14
Joined: Mon Oct 09, 2017 8:15 am

Tue Oct 31, 2017 10:33 am

Hello,

Thanks for your inquiry.
Please specify your requirement, where you want to insert the paragraphs? Could you share your entire sample code?

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Tue Oct 31, 2017 1:16 pm

I would like to replace "[HERE]" by my paragraphs.
Here is the entire sample code

Code: Select all
private Document _doc;
private int _indexdoc;
private Section _section;

public int indexdoc
{
   get { return _indexdoc; }
   set { _indexdoc = value; }
}

public Section section
{
   get { return _section; }
   set { _section = value; }
}

public Document doc
{
   get { return _doc; }
   set { _doc = value; }
}

public void TitleStyle(string text)
{
   Paragraph paragraph = new Paragraph(this.doc);
   Paragraph rline = new Paragraph(this.doc);
   //Paragraph paragraph = section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph();
   //paragraph = section.AddParagraph();
   paragraph.AppendText(text);
   paragraph.ApplyStyle(BuiltinStyle.Heading2);
   indexdoc++;
   section.Body.ChildObjects.Insert(indexdoc, paragraph);
   indexdoc = section.Body.ChildObjects.IndexOf(paragraph);
   indexdoc++;
   section.Body.ChildObjects.Insert(indexdoc, rline);
   indexdoc = section.Body.ChildObjects.IndexOf(paragraph);
}

public void InsertPara()
{
   TitleStyle("TESTESTTEST");

   Paragraph parag = new Paragraph(this.doc);

   Paragraph paragraph = new Paragraph(this.doc);
   paragraph.AppendText("Description");
   paragraph.ApplyStyle("Titre3");

   indexdoc++;
   section.Body.ChildObjects.Insert(indexdoc, paragraph);
   indexdoc++;
   section.Body.ChildObjects.Insert(indexdoc, parag);

   Paragraph paragraph1 = new Paragraph(this.doc);
   paragraph1.AppendText("diohjuize huidhezu idhezuid hezuihde uzihdezdhez cbcjsdnjkc diohjuize huidhezu idhezuid hezuihde uzihdezdhez cbcjsdnjkc");
   paragraph1.ApplyStyle("Normal");

   indexdoc++;
   section.Body.ChildObjects.Insert(indexdoc, paragraph1);
   indexdoc++;
   section.Body.ChildObjects.Insert(indexdoc, parag);

   Paragraph paragraph2 = new Paragraph(this.doc);
   paragraph2.AppendText("STATEEEEE");
   

   indexdoc++;
   section.Body.ChildObjects.Insert(indexdoc, paragraph2);
   indexdoc++;
   section.Body.ChildObjects.Insert(indexdoc, parag);

   Paragraph paragraph3 = new Paragraph(this.doc);
   paragraph3.AppendText("");

   indexdoc++;
   section.Body.ChildObjects.Insert(indexdoc, paragraph3);
   indexdoc++;
   section.Body.ChildObjects.Insert(indexdoc, parag);
}

public void Generate()
{
   int index = 0;
   
   string template = "test.docx";
   string report = "test1.docx";
   File.Delete(report);
   File.Copy(template, report);

   int n = 20;

   this.doc = new Document();
   this.doc.LoadFromFile(report);

    foreach (Section section in doc.Sections)
    {
       foreach (Paragraph paragraph in section.Paragraphs)
       {
          if (paragraph.Text.Contains("[HERE]"))
          {
             if (!paragraph.GetStyle().DefaultStyleType.ToString().Equals("Toc2"))
             {
                this.section = section;
                indexdoc = section.Body.ChildObjects.IndexOf(paragraph);

             }
          }
       }
    }

   InsertPara();
   InsertPara();


   this.doc.SaveToFile(report, FileFormat.Docx);
}

elysiumsecurity
 
Posts: 14
Joined: Mon Oct 09, 2017 8:15 am

Wed Nov 01, 2017 6:17 am

Hello elysiumsecurity,

Thanks for your details.
The main cause of your issue is the repeated use of the paragraph object "parag". Theoretically, one object can only exist in the document once. If you reuse it, only the last time of the appliance would make sense, and what's worse, the document objects would be in disorder.
To figure it out, you need to use the corresponding clone of the paragraph. Please refer to the code below.
Code: Select all
    private Document _doc;
    private int _indexdoc;
    private Section _section;

    public int indexdoc
    {
       get { return _indexdoc; }
       set { _indexdoc = value; }
    }

    public Section section
    {
       get { return _section; }
       set { _section = value; }
    }

    public Document doc
    {
       get { return _doc; }
       set { _doc = value; }
    }

    public void TitleStyle(string text)
    {
       Paragraph paragraph = new Paragraph(this.doc);
       Paragraph rline = new Paragraph(this.doc);
       //Paragraph paragraph = section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph();
       //paragraph = section.AddParagraph();
       paragraph.AppendText(text);
       paragraph.ApplyStyle(BuiltinStyle.Heading2);
       indexdoc++;
       section.Body.ChildObjects.Insert(indexdoc, paragraph);
       indexdoc = section.Body.ChildObjects.IndexOf(paragraph);
       indexdoc++;
       section.Body.ChildObjects.Insert(indexdoc, rline);
       indexdoc = section.Body.ChildObjects.IndexOf(paragraph);
    }

    public void InsertPara()
    {
       TitleStyle("TESTESTTEST");

       Paragraph parag = new Paragraph(this.doc);

       Paragraph paragraph = new Paragraph(this.doc);
       paragraph.AppendText("Description");
       paragraph.ApplyStyle("Titre3");

       indexdoc++;
       section.Body.ChildObjects.Insert(indexdoc, paragraph);
       indexdoc++;
       section.Body.ChildObjects.Insert(indexdoc, parag.Clone());

       Paragraph paragraph1 = new Paragraph(this.doc);
       paragraph1.AppendText("diohjuize huidhezu idhezuid hezuihde uzihdezdhez cbcjsdnjkc diohjuize huidhezu idhezuid hezuihde uzihdezdhez cbcjsdnjkc");
       paragraph1.ApplyStyle("Normal");

       indexdoc++;
       section.Body.ChildObjects.Insert(indexdoc, paragraph1);
       indexdoc++;
       section.Body.ChildObjects.Insert(indexdoc, parag.Clone());

       Paragraph paragraph2 = new Paragraph(this.doc);
       paragraph2.AppendText("STATEEEEE");
       

       indexdoc++;
       section.Body.ChildObjects.Insert(indexdoc, paragraph2);
       indexdoc++;
       section.Body.ChildObjects.Insert(indexdoc, parag.Clone());

       Paragraph paragraph3 = new Paragraph(this.doc);
       paragraph3.AppendText("");

       indexdoc++;
       section.Body.ChildObjects.Insert(indexdoc, paragraph3);
       indexdoc++;
       section.Body.ChildObjects.Insert(indexdoc, parag.Clone());
    }

    public void Generate()
    {
       int index = 0;
       
       string template = "test.docx";
       string report = "test1.docx";
       File.Delete(report);
       File.Copy(template, report);

       int n = 20;

       this.doc = new Document();
       this.doc.LoadFromFile(report);

        foreach (Section section in doc.Sections)
        {
           foreach (Paragraph paragraph in section.Paragraphs)
           {
              if (paragraph.Text.Contains("[HERE]"))
              {
                 if (!paragraph.GetStyle().DefaultStyleType.ToString().Equals("Toc2"))
                 {
                    this.section = section;
                    indexdoc = section.Body.ChildObjects.IndexOf(paragraph);

                 }
              }
           }
        }

       InsertPara();
       InsertPara();


       this.doc.SaveToFile(report, FileFormat.Docx);
    }


Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Tue Nov 07, 2017 8:53 am

Hello elysiumsecurity,

Greetings from e-iceblue!
Did the solution I provided work?
Your feedback will be greatly appreciated.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Return to Spire.Doc