Esempio n. 1
0
        public void ProcessRequest(HttpContext context)
        {
            var surah = int.Parse(context.Request["surah"] ?? "1");
            var fileName = "Quran_Surah_" + surah + ".docx";
            //context.Response.AppendHeader("Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
            context.Response.AppendHeader("Content-Type", "application/msword");
            context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
            //context.Response.ContentEncoding = Encoding.UTF8;

            // Open a Wordprocessing document for editing.
            var path = context.Server.MapPath("~/App_Data/" + fileName);
            if (File.Exists(path))
                File.Delete(path);

            File.Copy(context.Server.MapPath("~/App_Data/Template.docx"), path);
            using (WordprocessingDocument package = WordprocessingDocument.Open(path, true))
            {
                MainDocumentPart main = package.MainDocumentPart;
                StyleDefinitionsPart stylePart = main.StyleDefinitionsPart;
                FootnotesPart fpart = main.FootnotesPart;
                Body body = main.Document.Body;

                //MainDocumentPart main = package.AddMainDocumentPart();
                //StyleDefinitionsPart stylePart = AddStylesPartToPackage(package);
                //CreateDefaultStyles(stylePart);
                //FootnotesPart fpart = main.AddNewPart<FootnotesPart>();

                //main.Document = new Document();
                //Body body = main.Document.AppendChild<Body>(new Body());
                //body.AppendChild(
                //    new SectionProperties(
                //        new FootnoteProperties(
                //            new NumberingFormat() { Val = NumberFormatValues.LowerLetter },
                //            new NumberingRestart() { Val = RestartNumberValues.EachPage })));

                using (var quran = new QuranObjects.QuranContext())
                {
                    var translations = from mt in quran.MyTranslations where mt.SurahNo == surah select mt;
                    var lastVersePara = new Paragraph();

                    foreach (var translation in translations)
                    {
                        // Emit headings as Heading 2
                        if (translation.Heading.Length > 0)
                        {
                            var headingPara = body.AppendChild<Paragraph>(
                                new Paragraph(new Run2(new Text2(translation.Heading))));
                            ApplyStyleToParagraph(stylePart, GetStyleIdFromStyleName(stylePart, "My Heading 2"), headingPara);

                            if (lastVersePara.ChildElements.OfType<Run>().Count() == 0)
                                lastVersePara.Remove();

                            lastVersePara = body.AppendChild<Paragraph>(new Paragraph());
                            ApplyStyleToParagraph(stylePart, GetStyleIdFromStyleName(stylePart, "Verse"), lastVersePara);
                        }

                        // Find all footnotes from the footnote translation
                        var footnotes = new List<string>();
                        var matches = new Regex(@"(\*+)([^\*]*)").Matches(translation.Footnote);
                        foreach (Match match in matches)
                        {
                            var footnoteText = match.Groups[2].Value;
                            footnotes.Add(footnoteText);
                        }

                        // Generate the verse number
                        var translationAyahNoEnglish = translation.AyahNo.ToString();
                        var banglaVerseNo = new String(Array.ConvertAll(translationAyahNoEnglish.ToCharArray(), (c) => (char)('০' + (char)(c - '0'))));

                        AddSuperscriptText(lastVersePara, banglaVerseNo);
                        AddText(lastVersePara, " ", stylePart);

                        // Emit the translation while generating the footnote references
                        var translationFootnotes = new Regex(@"([^\*]*)(\*+)([^\*]*)").Matches(translation.Translation);
                        if (translationFootnotes.Count == 0)
                        {
                            AddText(lastVersePara, translation.Translation, stylePart);

                            // If there was no footnote marker in the translation text, then
                            // add the entire footnote at the end of the translation
                            if (translation.Footnote.Trim().Length > 0)
                            {
                                var footnoteId = AddFootnoteReference(fpart, stylePart, translation.Footnote.Replace("*", ""));
                                AddFootnote(lastVersePara, footnoteId, stylePart);
                            }
                        }

                        foreach (Match match in translationFootnotes)
                        {
                            var beforeText = match.Groups[1].Value;
                            var footnoteMarker = match.Groups[2].Value;
                            var afterText = match.Groups[3].Value;

                            AddText(lastVersePara, beforeText, stylePart);

                            // Create a footnote first and then add a footnote reference
                            var footnoteId = AddFootnoteReference(fpart, stylePart, footnotes[footnoteMarker.Length-1]);
                            AddFootnote(lastVersePara, footnoteId, stylePart);
                            AddText(lastVersePara, afterText, stylePart);
                        }

                        if (lastVersePara.Parent == null)
                        {
                            ApplyStyleToParagraph(stylePart, GetStyleIdFromStyleName(stylePart, "Verse"), lastVersePara);
                            body.AppendChild(lastVersePara);
                        }

                        if (translation.NewParaAfterThis)
                        {
                            lastVersePara = body.AppendChild<Paragraph>(new Paragraph());
                            ApplyStyleToParagraph(stylePart, GetStyleIdFromStyleName(stylePart, "Verse"), lastVersePara);
                        }
                        else
                        {
                            AddText(lastVersePara, " ", stylePart);
                        }
                    }

                    if (lastVersePara.Parent == null)
                        body.AppendChild(lastVersePara);

                }

                package.MainDocumentPart.Document.Save();

                OpenXmlValidator validator = new OpenXmlValidator();
                int count = 0;
                foreach (ValidationErrorInfo error in
                    validator.Validate(package))
                {
                    count++;
                    Debug.WriteLine("Error " + count);
                    Debug.WriteLine("Description: " + error.Description);
                    Debug.WriteLine("ErrorType: " + error.ErrorType);
                    Debug.WriteLine("Node: " + error.Node);
                    Debug.WriteLine("Path: " + error.Path.XPath);
                    Debug.WriteLine("Part: " + error.Part.Uri);
                    Debug.WriteLine("-------------------------------------------");
                }
            }

            context.Response.TransmitFile(path);
        }
Esempio n. 2
0
        public void ProcessRequest(HttpContext context)
        {
            var surah    = int.Parse(context.Request["surah"] ?? "1");
            var fileName = "Quran_Surah_" + surah + ".docx";

            //context.Response.AppendHeader("Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
            context.Response.AppendHeader("Content-Type", "application/msword");
            context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
            //context.Response.ContentEncoding = Encoding.UTF8;

            // Open a Wordprocessing document for editing.
            var path = context.Server.MapPath("~/App_Data/" + fileName);

            if (File.Exists(path))
            {
                File.Delete(path);
            }

            File.Copy(context.Server.MapPath("~/App_Data/Template.docx"), path);
            using (WordprocessingDocument package = WordprocessingDocument.Open(path, true))
            {
                MainDocumentPart     main      = package.MainDocumentPart;
                StyleDefinitionsPart stylePart = main.StyleDefinitionsPart;
                FootnotesPart        fpart     = main.FootnotesPart;
                Body body = main.Document.Body;


                //MainDocumentPart main = package.AddMainDocumentPart();
                //StyleDefinitionsPart stylePart = AddStylesPartToPackage(package);
                //CreateDefaultStyles(stylePart);
                //FootnotesPart fpart = main.AddNewPart<FootnotesPart>();

                //main.Document = new Document();
                //Body body = main.Document.AppendChild<Body>(new Body());
                //body.AppendChild(
                //    new SectionProperties(
                //        new FootnoteProperties(
                //            new NumberingFormat() { Val = NumberFormatValues.LowerLetter },
                //            new NumberingRestart() { Val = RestartNumberValues.EachPage })));

                using (var quran = new QuranObjects.QuranContext())
                {
                    var translations  = from mt in quran.MyTranslations where mt.SurahNo == surah select mt;
                    var lastVersePara = new Paragraph();

                    foreach (var translation in translations)
                    {
                        // Emit headings as Heading 2
                        if (translation.Heading.Length > 0)
                        {
                            var headingPara = body.AppendChild <Paragraph>(
                                new Paragraph(new Run2(new Text2(translation.Heading))));
                            ApplyStyleToParagraph(stylePart, GetStyleIdFromStyleName(stylePart, "My Heading 2"), headingPara);

                            if (lastVersePara.ChildElements.OfType <Run>().Count() == 0)
                            {
                                lastVersePara.Remove();
                            }

                            lastVersePara = body.AppendChild <Paragraph>(new Paragraph());
                            ApplyStyleToParagraph(stylePart, GetStyleIdFromStyleName(stylePart, "Verse"), lastVersePara);
                        }

                        // Find all footnotes from the footnote translation
                        var footnotes = new List <string>();
                        var matches   = new Regex(@"(\*+)([^\*]*)").Matches(translation.Footnote);
                        foreach (Match match in matches)
                        {
                            var footnoteText = match.Groups[2].Value;
                            footnotes.Add(footnoteText);
                        }

                        // Generate the verse number
                        var translationAyahNoEnglish = translation.AyahNo.ToString();
                        var banglaVerseNo            = new String(Array.ConvertAll(translationAyahNoEnglish.ToCharArray(), (c) => (char)('০' + (char)(c - '0'))));

                        AddSuperscriptText(lastVersePara, banglaVerseNo);
                        AddText(lastVersePara, " ", stylePart);

                        // Emit the translation while generating the footnote references
                        var translationFootnotes = new Regex(@"([^\*]*)(\*+)([^\*]*)").Matches(translation.Translation);
                        if (translationFootnotes.Count == 0)
                        {
                            AddText(lastVersePara, translation.Translation, stylePart);

                            // If there was no footnote marker in the translation text, then
                            // add the entire footnote at the end of the translation
                            if (translation.Footnote.Trim().Length > 0)
                            {
                                var footnoteId = AddFootnoteReference(fpart, stylePart, translation.Footnote.Replace("*", ""));
                                AddFootnote(lastVersePara, footnoteId, stylePart);
                            }
                        }

                        foreach (Match match in translationFootnotes)
                        {
                            var beforeText     = match.Groups[1].Value;
                            var footnoteMarker = match.Groups[2].Value;
                            var afterText      = match.Groups[3].Value;

                            AddText(lastVersePara, beforeText, stylePart);

                            // Create a footnote first and then add a footnote reference
                            var footnoteId = AddFootnoteReference(fpart, stylePart, footnotes[footnoteMarker.Length - 1]);
                            AddFootnote(lastVersePara, footnoteId, stylePart);
                            AddText(lastVersePara, afterText, stylePart);
                        }

                        if (lastVersePara.Parent == null)
                        {
                            ApplyStyleToParagraph(stylePart, GetStyleIdFromStyleName(stylePart, "Verse"), lastVersePara);
                            body.AppendChild(lastVersePara);
                        }

                        if (translation.NewParaAfterThis)
                        {
                            lastVersePara = body.AppendChild <Paragraph>(new Paragraph());
                            ApplyStyleToParagraph(stylePart, GetStyleIdFromStyleName(stylePart, "Verse"), lastVersePara);
                        }
                        else
                        {
                            AddText(lastVersePara, " ", stylePart);
                        }
                    }

                    if (lastVersePara.Parent == null)
                    {
                        body.AppendChild(lastVersePara);
                    }
                }

                package.MainDocumentPart.Document.Save();


                OpenXmlValidator validator = new OpenXmlValidator();
                int count = 0;
                foreach (ValidationErrorInfo error in
                         validator.Validate(package))
                {
                    count++;
                    Debug.WriteLine("Error " + count);
                    Debug.WriteLine("Description: " + error.Description);
                    Debug.WriteLine("ErrorType: " + error.ErrorType);
                    Debug.WriteLine("Node: " + error.Node);
                    Debug.WriteLine("Path: " + error.Path.XPath);
                    Debug.WriteLine("Part: " + error.Part.Uri);
                    Debug.WriteLine("-------------------------------------------");
                }
            }

            context.Response.TransmitFile(path);
        }