private void button1_Click(object sender, EventArgs e)
        {
            string[] latexMathCode =
            {
                @"x^{2}+\\sqrt{x^{2}+1}=2",
                @"2\alpha - \sin y + x",
                @"1 \over 2 + x",
                @"(1 + \vert x-[a-b] \vert)",
                @"\mbox{if $x=1$ or $x=2$}",
                @"\begin{cases} 1 & \mbox{if $x>0$,} \\ 2 & \mbox{otherwise.} \end{cases}"
            };

            Document doc = new Document();

            doc.LoadFromFile(@"..\..\..\..\..\..\Data\AddMathEquation.docx");
            Section section = doc.Sections[0];

            Paragraph  paragraph;
            OfficeMath officeMath;
            //Add LaTeX code
            Table             table1        = section.Tables[0] as Table;
            List <OfficeMath> mathEquations = new List <OfficeMath>();

            for (int i = 1; i < 7; i++)
            {
                paragraph      = table1.Rows[i].Cells[0].AddParagraph();
                paragraph.Text = latexMathCode[i - 1];
                paragraph      = table1.Rows[i].Cells[1].AddParagraph();
                officeMath     = new OfficeMath(doc);
                officeMath.FromLatexMathCode(latexMathCode[i - 1]);
                paragraph.Items.Add(officeMath);
                mathEquations.Add(officeMath);
            }

            //Add MathML code
            Table table2 = section.Tables[1] as Table;

            for (int i = 1; i < 7; i++)
            {
                paragraph      = table2.Rows[i].Cells[0].AddParagraph();
                paragraph.Text = mathEquations[i - 1].ToMathMLCode();
                paragraph      = table2.Rows[i].Cells[1].AddParagraph();
                officeMath     = new OfficeMath(doc);
                officeMath.FromMathMLCode(mathEquations[i - 1].ToMathMLCode());
                paragraph.Items.Add(officeMath);
            }
            string result = "AddMathEquation_result.docx";

            doc.SaveToFile(result, FileFormat.Docx);
            WordDocViewer(result);
        }
Ejemplo n.º 2
0
        public string xuatpdf(long id, string tenmon)
        {
            Document doc     = new Document();
            Section  section = doc.AddSection();

            section.PageSetup.Borders.BorderType = BorderStyle.DoubleWave;

            section.PageSetup.Borders.Color = Color.Black;

            section.PageSetup.Borders.Left.Space = 50;

            section.PageSetup.Borders.Right.Space  = 50;
            section.PageSetup.Borders.Top.Space    = 20;
            section.PageSetup.Borders.Bottom.Space = 20;

            //title
            Paragraph paratitle = section.AddParagraph();

            paratitle.Format.HorizontalAlignment = HorizontalAlignment.Center;
            paratitle.AppendText("TRƯỜNG ĐẠI HỌC GIAO THÔNG VẬN TẢI PHÂN HIỆU TẠI TP HỒ CHÍ MINH");
            ParagraphStyle styletitle = new ParagraphStyle(doc);

            styletitle.Name = "titleStyle";
            styletitle.CharacterFormat.Bold      = true;
            styletitle.CharacterFormat.TextColor = Color.Black;
            styletitle.CharacterFormat.FontName  = "Arial";
            styletitle.CharacterFormat.FontSize  = 16f;
            doc.Styles.Add(styletitle);
            paratitle.ApplyStyle("titleStyle");


            // tne  mon de thi
            Paragraph paratenmon = section.AddParagraph();

            paratenmon.Format.HorizontalAlignment = HorizontalAlignment.Left;
            paratenmon.AppendText("\n\nĐề thi môn:" + tenmon);

            ParagraphStyle styleleft = new ParagraphStyle(doc);

            styleleft.Name = "left";
            styleleft.CharacterFormat.Bold      = true;
            styleleft.CharacterFormat.TextColor = Color.Black;
            styleleft.CharacterFormat.FontName  = "Arial";
            styleleft.CharacterFormat.FontSize  = 12f;
            doc.Styles.Add(styleleft);
            paratenmon.ApplyStyle("left");


            // ma de thi
            Paragraph paramade = section.AddParagraph();

            paramade.Format.HorizontalAlignment = HorizontalAlignment.Right;
            paramade.AppendText("\tMã đề:" + id + "\n\n\n");

            ParagraphStyle styleright = new ParagraphStyle(doc);

            styleright.Name = "right";
            styleright.CharacterFormat.Bold      = true;
            styleright.CharacterFormat.TextColor = Color.Black;
            styleright.CharacterFormat.FontName  = "Arial";
            styleright.CharacterFormat.FontSize  = 12f;

            doc.Styles.Add(styleright);
            paramade.ApplyStyle("right");

            int slcau = 0;
            var khoch = new Bode().listkhocauhoi(id);



            foreach (var item in khoch)
            {
                Paragraph para1 = section.AddParagraph();
                slcau++;
                int slda = 0;
                var s    = tachcongthuc(item.NoiDung);
                para1.AppendText("\n");
                para1.AppendText("Câu " + slcau + ":");
                foreach (var item2 in s)
                {
                    if (item2.Contains("<mml:math"))
                    {
                        try
                        {
                            OfficeMath officeMath;
                            officeMath = new OfficeMath(doc);
                            officeMath.FromMathMLCode(item2);
                            para1.Items.Add(officeMath);
                        }
                        catch
                        {
                        }
                    }
                    else
                    {
                        para1.AppendHTML(item2);
                    }
                }
                para1.AppendText("\n\n");

                if (item.HinhAnh.Length > 0)
                {
                    try
                    {
                        Image image = Image.FromFile(Server.MapPath(item.HinhAnh));


                        DocPicture pic = para1.AppendPicture(image);

                        pic.Height = pic.Height;
                        if (pic.Height > pic.Width)
                        {
                            pic.Height = 180;
                            pic.Width  = 120;
                        }
                        else if (pic.Height < pic.Width)
                        {
                            pic.Height = 120;
                            pic.Width  = 150;
                        }
                        else if (pic.Height == pic.Width)
                        {
                            pic.Height = 150;
                            pic.Width  = 150;
                        }



                        para1.AppendText("\n");
                    }
                    catch { }
                }


                foreach (var da in item.Dap_AN)
                {
                    var s1 = tachcongthuc(da.NoiDung);
                    para1.AppendText("\n");
                    if (da.TrangThai == true)
                    {
                        para1.AppendText("*");
                    }
                    else
                    {
                        para1.AppendText("");
                    }
                    slda++;
                    foreach (var item2 in s1)
                    {
                        if (item2.Contains("<mml:math"))
                        {
                            try
                            {
                                OfficeMath officeMath;
                                officeMath = new OfficeMath(doc);
                                officeMath.FromMathMLCode(item2);
                                para1.Items.Add(officeMath);
                            }
                            catch
                            {
                            }
                        }
                        else
                        {
                            para1.AppendHTML(item2);
                        }
                    }

                    if (da.HinhAnh.Length > 0)
                    {
                        try
                        {
                            para1.AppendText("\n\n");
                            Image image = Image.FromFile(Server.MapPath(da.HinhAnh));


                            DocPicture pic = para1.AppendPicture(image);

                            if (pic.Height > pic.Width)
                            {
                                pic.Height = 120;
                                pic.Width  = 100;
                            }
                            else if (pic.Height < pic.Width)
                            {
                                pic.Height = 100;
                                pic.Width  = 120;
                            }
                            else if (pic.Height == pic.Width)
                            {
                                pic.Height = 120;
                                pic.Width  = 120;
                            }

                            para1.AppendText("\n");
                        }
                        catch { }
                    }
                }
            }
            Paragraph paragraph = section.Paragraphs[section.Count - 1];
            Footnote  footnote  = paragraph.AppendFootnote(FootnoteType.Footnote);
            TextRange text      = footnote.TextBody.AddParagraph().AppendText("HẾT");

            text.CharacterFormat.FontName  = "Arial Black";
            text.CharacterFormat.FontSize  = 12;
            text.CharacterFormat.TextColor = Color.SlateGray;

            doc.SaveToFile(Server.MapPath("~/Content/" + "De_" + id + ".docx"));
            doc.SaveToFile(Server.MapPath("~/Content/" + "De_" + id + ".pdf"));

            return("/Content/" + "De_" + id + ".pdf");
        }