Search code examples
c#.netspire.doc

Spire.Doc, saving input overwrites previous input, windows forms


I have a form that asks questions and you write an answer in a text box. When you hit 'next' or 'add' it should save the input and cycle like this until you hit save. For whatever reason, it only saves the one line and not the subsequent lines. Here's the code I have.

private void add_Click(object sender, EventArgs e)
    {

        Paragraph question = document.AddSection().AddParagraph();
        question.AppendText(questions.Text + "  " + answer.Text);

        document.SaveToFile(teamMember.Text + ".doc", FileFormat.Doc);

    }

    private void finish_Click(object sender, EventArgs e)
    {            
        document.SaveToFile(teamMember.Text + ".doc", FileFormat.Doc);
    }

Solution

  • Not sure if I understand you correctly. From your code, I found that each time you were adding a new section, this will cause each of the question and the answer to be added to a new section of the document. If this is the problem, you can try the following code:

    if (doc.Sections.Count == 0)
    {
        //If the document is null, then add a new section
        Section sec = doc.AddSection();
        Paragraph para = sec.AddParagraph();
        para.AppendText("this is the first para");
    }
    else
    {
        //Else add the text to the last paragraph of the document
        Paragraph paranew = doc.Sections[0].Paragraphs[doc.Sections[0].Paragraphs.Count - 1];
        paranew.AppendText(" " + "this is new para");
    }
    

    Hope it helps.