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