Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files from any .NET platform (C#, VB.NET, ASP.NET, .NET Core) and Java applications (J2SE and J2EE) with fast and high quality performance.

Tue Jan 05, 2021 4:36 pm

I am trying to use the doc in asp.net c# webforms and i am looping through a repeater to get the label text which is questions and i have a child repeater with a textbox and getting data out of the page.


I have the application working in essence i just need some help on applying formatting to paragraphs I would like to apply the bold formatting to questions and the normal formatting to the answers. The normal formatting is only applied. I've obviously missed something and any help would be gratefully received

Code: Select all
<asp:Repeater runat="server" OnDataBinding="rptQuestions_OnDataBinding" OnItemDataBound="rptQuestions_OnItemDataBound" ID="rptQuestions">
        <ItemTemplate>
            <asp:HiddenField runat="server" ID="hiddenId" Value='<%# Eval("QHiD").ToString() %>'/>
            <p><asp:Label runat="server" ID="lbl1" Text='<%# Eval("Question") %>'></asp:Label></p>
            <asp:Repeater runat="server" ID="rptChild" >
                <ItemTemplate>
                    <p><asp:Label runat="server" ID="lblChild" Text='<%# Eval("QDetail").ToString() %>'></asp:Label>
                        <asp:TextBox runat="server" TextMode="MultiLine" Width="100%" Height="25%" ID="txtAnswer"></asp:TextBox>
                    </p>
                   
                </ItemTemplate>
            </asp:Repeater>
        </ItemTemplate>
    </asp:Repeater>
    <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_OnClick" />


code behind

Code: Select all
 protected void btnSave_OnClick(object sender, EventArgs e)
        {
            Document doc = new Document();
            Section sec = doc.AddSection();
            Paragraph par = sec.AddParagraph();

            ParagraphStyle boldStyle = new ParagraphStyle(doc)
            {
                Name = "FontStyle",
                CharacterFormat = {Bold = true, FontName = "Century Gothic", FontSize = 11}

            };

            ParagraphStyle normalStyle = new ParagraphStyle(doc)
            {
                Name = "FontStyle",
                CharacterFormat = {Bold = false, FontName = "Century Gothic", FontSize = 11}

            };

            foreach (RepeaterItem ri in rptQuestions.Items)
            {

                var q = (Label) ri.FindControl("lbl1");


                par.AppendText(q.Text);

                doc.Styles.Add(boldStyle);
                par.ApplyStyle(boldStyle.Name);

                par.AppendBreak(BreakType.LineBreak);


                Repeater rpt = ri.FindControl("rptChild") as Repeater;

                foreach (RepeaterItem item in rpt.Items)
                {
                    var box = (TextBox) item.FindControl("txtAnswer");



                    par.AppendText(box.Text);

                    doc.Styles.Add(normalStyle);
                    par.ApplyStyle(normalStyle.Name);


                    par.AppendBreak(BreakType.LineBreak);

                }

            }

            //Save Word
            doc.SaveToFile(@"C:\Users\User\Documents\OperateWord.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start(@"C:\Users\User\Documents\OperateWord.docx");
        }

webgeeks
 
Posts: 2
Joined: Tue Jan 05, 2021 4:16 pm

Tue Jan 05, 2021 4:50 pm

I have fixed it by using character formatting

webgeeks
 
Posts: 2
Joined: Tue Jan 05, 2021 4:16 pm

Wed Jan 06, 2021 2:23 am

Hello,

Glad to hear that you have found the solution yourself.
If you encounter any issues related to our products in the future, please feel free to contact us.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 305
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.Doc