Пример #1
0
        private void InsertPageHeaderFooter(WordDocument doc, IWSection section1)
        {
            // Add a new paragraph for header to the document.
            IWParagraph headerPar = new WParagraph(doc);

            // Add a new table to the header
            IWTable table = section1.HeadersFooters.Header.AddTable();

            RowFormat format = new RowFormat();

            // Setting Single table border style.
            format.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;

            // Inserting table with a row and two columns.
            table.ResetCells(1, 2, format, 265);

            // Inserting logo image to the table first cell.
            headerPar = table[0, 0].AddParagraph() as WParagraph;
#if NETCORE
            headerPar.AppendPicture(Image.FromFile(@"..\..\..\..\..\..\..\common\images\DocIO\Northwind_logo.png"));
#else
            headerPar.AppendPicture(Image.FromFile(@"..\..\..\..\..\..\common\images\DocIO\Northwind_logo.png"));
#endif
            //Set Image size.
            (headerPar.Items[0] as WPicture).Width  = 232.5f;
            (headerPar.Items[0] as WPicture).Height = 54.75f;
            // Inserting text to the table second cell.
            headerPar = table[0, 1].AddParagraph() as WParagraph;
            IWTextRange txt = headerPar.AppendText("Company Headquarters,\n2501 Aerial Center Parkway,\nSuite 110, Morrisville, NC 27560,\nTEL 1-888-936-8638.");
            txt.CharacterFormat.FontSize                  = 12;
            txt.CharacterFormat.CharacterSpacing          = 1.7f;
            headerPar.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Right;

            // Add a footer paragraph text to the document.
            WParagraph footerPar = new WParagraph(doc);
            footerPar.ParagraphFormat.Tabs.AddTab(523f, TabJustification.Right, TabLeader.NoLeader);
            // Add text.
            footerPar.AppendText("Copyright Northwind Inc. 2001 - 2017");
            // Add page and Number of pages field to the document.
            footerPar.AppendText("\tPage ");
            IWField ff = footerPar.AppendField("Page", Syncfusion.DocIO.FieldType.FieldPage);

            section1.HeadersFooters.Footer.Paragraphs.Add(footerPar);

            #region Page Number Settings
            section1.PageSetup.RestartPageNumbering = true;
            section1.PageSetup.PageStartingNumber   = Convert.ToInt32(this.numericUpDown1.Value);
            section1.PageSetup.PageNumberStyle      = (PageNumberStyle)Enum.Parse(typeof(PageNumberStyle), this.comboBox1.SelectedItem.ToString(), true);
            #endregion
        }
 static void Main(string[] args)
 {
     //Creates new Word document instance for Word processing
     using (WordDocument document = new WordDocument())
     {
         //Opens the input Word document
         Stream docStream = File.OpenRead(Path.GetFullPath(@"../../../Template.docx"));
         document.Open(docStream, FormatType.Docx);
         docStream.Dispose();
         //Finds all the image placeholder text in the Word document.
         TextSelection[] textSelections = document.FindAll(new Regex("^//(.*)"));
         for (int i = 0; i < textSelections.Length; i++)
         {
             //Replaces the image placeholder text with desired image.
             Stream     imageStream = File.OpenRead(Path.GetFullPath(@"../../../" + textSelections[i].SelectedText + ".png"));
             WParagraph paragraph   = new WParagraph(document);
             WPicture   picture     = paragraph.AppendPicture(imageStream) as WPicture;
             imageStream.Dispose();
             TextSelection newSelection = new TextSelection(paragraph, 0, 1);
             TextBodyPart  bodyPart     = new TextBodyPart(document);
             bodyPart.BodyItems.Add(paragraph);
             document.Replace(textSelections[i].SelectedText, bodyPart, true, true);
         }
         //Saves the resultant file in the given path
         docStream = File.Create(Path.GetFullPath(@"Result.docx"));
         document.Save(docStream, FormatType.Docx);
         docStream.Dispose();
     }
 }
Пример #3
0
        private void InsertFirstPageHeaderFooter(WordDocument doc, IWSection section)
        {
            Assembly execAssm = typeof(HeadersAndFootersDemo).GetTypeInfo().Assembly;

            Stream inputStream = execAssm.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.DocIO.DocIO.Assets.Northwind_logo.png");

            // Add a new paragraph for header to the document.
            IWParagraph headerPar = new WParagraph(doc);

            // Add a new table to the header.
            IWTable table = section.HeadersFooters.FirstPageHeader.AddTable();

            RowFormat format = new RowFormat();

            // Setting cleared table border style.
            format.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Cleared;

            // Inserting table with a row and two columns.
            table.ResetCells(1, 2, format, 265);

            // Inserting logo image to the table first cell.
            headerPar = table[0, 0].AddParagraph() as WParagraph;
            headerPar.AppendPicture(inputStream);
            //Set Image size
            (headerPar.Items[0] as WPicture).Width  = 232.5f;
            (headerPar.Items[0] as WPicture).Height = 54.75f;
            // Inserting text to the table second cell.
            headerPar = table[0, 1].AddParagraph() as WParagraph;
            IWTextRange txt = headerPar.AppendText("Company Headquarters,\n2501 Aerial Center Parkway,\nSuite 110, Morrisville, NC 27560,\nTEL 1-888-936-8638.");

            txt.CharacterFormat.FontSize                  = 12;
            txt.CharacterFormat.CharacterSpacing          = 1.7f;
            headerPar.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Right;
            // Add a new paragraph to the header with address text.
            headerPar = new WParagraph(doc);
            headerPar.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            txt = headerPar.AppendText("\nFirst Page Header");
            txt.CharacterFormat.CharacterSpacing = 1.7f;
            section.HeadersFooters.FirstPageHeader.Paragraphs.Add(headerPar);

            // Add a footer paragraph text to the document.
            WParagraph footerPar = new WParagraph(doc);

            footerPar.ParagraphFormat.Tabs.AddTab(523f, TabJustification.Right, TabLeader.NoLeader);
            // Add text.
            footerPar.AppendText("Copyright Northwind Inc. 2001 - 2015");
            // Add page and Number of pages field to the document.
            footerPar.AppendText("\tFirst Page ");
            footerPar.AppendField("Page", FieldType.FieldPage);
            section.HeadersFooters.FirstPageFooter.Paragraphs.Add(footerPar);
            #region Page Number Settings
            section.PageSetup.RestartPageNumbering = true;
            section.PageSetup.PageStartingNumber   = Convert.ToInt32(this.numericUpDown1.Value);
            section.PageSetup.PageNumberStyle      = (PageNumberStyle)Enum.Parse(typeof(PageNumberStyle), this.comboBox1.SelectedItem.ToString(), true);
            #endregion
        }
Пример #4
0
        private void InsertFirstPageHeaderFooter(WordDocument doc, IWSection section)
        {
            // Add a new paragraph for header to the document.
            IWParagraph headerPar = new WParagraph(doc);

            // Add a new table to the header.
            IWTable table = section.HeadersFooters.FirstPageHeader.AddTable();

            RowFormat format = new RowFormat();

            // Setting cleared table border style.
            format.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Cleared;

            // Inserting table with a row and two columns.
            table.ResetCells(1, 2, format, 265);

            // Inserting logo image to the table first cell.
            headerPar = table[0, 0].AddParagraph() as WParagraph;
            string     basePath    = _hostingEnvironment.WebRootPath;
            FileStream imageStream = new FileStream(basePath + @"/images/DocIO/Northwind_logo.png", FileMode.Open, FileAccess.Read);

            headerPar.AppendPicture(imageStream);
            //Set Image size
            (headerPar.Items[0] as WPicture).Width  = 232.5f;
            (headerPar.Items[0] as WPicture).Height = 54.75f;
            // Inserting text to the table second cell.
            headerPar = table[0, 1].AddParagraph() as WParagraph;
            IWTextRange txt = headerPar.AppendText("Company Headquarters,\n2501 Aerial Center Parkway,\nSuite 110, Morrisville, NC 27560,\nTEL 1-888-936-8638.");

            txt.CharacterFormat.FontSize                  = 12;
            txt.CharacterFormat.CharacterSpacing          = 1.7f;
            headerPar.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Right;
            // Add a new paragraph to the header with address text.
            headerPar = new WParagraph(doc);
            headerPar.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            txt = headerPar.AppendText("\nFirst Page Header");
            txt.CharacterFormat.CharacterSpacing = 1.7f;
            section.HeadersFooters.FirstPageHeader.Paragraphs.Add(headerPar);

            // Add a footer paragraph text to the document.
            WParagraph footerPar = new WParagraph(doc);

            footerPar.ParagraphFormat.Tabs.AddTab(523f, TabJustification.Right, TabLeader.NoLeader);
            // Add text.
            footerPar.AppendText("Copyright Northwind Inc. 2001 - 2017");
            // Add page and Number of pages field to the document.
            footerPar.AppendText("\tFirst Page ");
            footerPar.AppendField("Page", FieldType.FieldPage);
            section.HeadersFooters.FirstPageFooter.Paragraphs.Add(footerPar);
            #region Page Number Settings
            section.PageSetup.RestartPageNumbering = true;
            section.PageSetup.PageStartingNumber   = 1;
            section.PageSetup.PageNumberStyle      = PageNumberStyle.Arabic;
            #endregion Page Number Settings
        }
Пример #5
0
        private void InsertPageHeaderFooter(WordDocument doc, IWSection section1)
        {
            // Add a new paragraph for header to the document.
            IWParagraph headerPar = new WParagraph(doc);

            // Add a new table to the header
            IWTable table = section1.HeadersFooters.Header.AddTable();

            RowFormat format = new RowFormat();

            // Setting Single table border style.
            format.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;

            // Inserting table with a row and two columns.
            table.ResetCells(1, 2, format, 265);

            // Inserting logo image to the table first cell.
            headerPar = table[0, 0].AddParagraph() as WParagraph;
            string s = ResolveApplicationDataPath("Northwind_logo.png", "Content\\DocIO");

            headerPar.AppendPicture(System.Drawing.Image.FromFile(s));
            //Set Image size.
            (headerPar.Items[0] as WPicture).Width  = 232.5f;
            (headerPar.Items[0] as WPicture).Height = 54.75f;
            // Inserting text to the table second cell.
            headerPar = table[0, 1].AddParagraph() as WParagraph;
            IWTextRange txt = headerPar.AppendText("Company Headquarters,\n2501 Aerial Center Parkway,\nSuite 110, Morrisville, NC 27560,\nTEL 1-888-936-8638.");

            txt.CharacterFormat.FontSize                  = 12;
            txt.CharacterFormat.CharacterSpacing          = 1.7f;
            headerPar.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Right;

            // Add a footer paragraph text to the document.
            WParagraph footerPar = new WParagraph(doc);

            footerPar.ParagraphFormat.Tabs.AddTab(523f, TabJustification.Right, TabLeader.NoLeader);
            // Add text.
            footerPar.AppendText("Copyright Northwind Inc. 2001 - 2017");
            // Add page and Number of pages field to the document.
            footerPar.AppendText("\tPage ");
            IWField ff = footerPar.AppendField("Page", FieldType.FieldPage);

            section1.HeadersFooters.Footer.Paragraphs.Add(footerPar);

            #region Page Number Settings
            section1.PageSetup.RestartPageNumbering = true;
            section1.PageSetup.PageStartingNumber   = 1;
            section1.PageSetup.PageNumberStyle      = PageNumberStyle.Arabic;
            #endregion Page Number Settings
        }
Пример #6
0
        public ActionResult Word(int id, int type, byte[] image, bool pdf = false)
        {
            var    dba = new ApplicationDbContext();
            int    idCustomer;
            string idConsultant;

            global::Interview.Interview interview = null;
            Campaign campaign = null;

            byte[] byteArrayImg;
            if (type == 1 || type == 2)
            {
                idCustomer   = db.Interviews.Where(i => i.IdCampaign == id).Include(i => i.Campaign.Customer).Select(i => i.Campaign.Customer.IdCustomer).FirstOrDefault();
                idConsultant = db.Interviews.Where(i => i.IdCampaign == id).Select(u => u.Id).FirstOrDefault();
                var idFirm = db.Users.Where(u => u.Id == idConsultant).Select(u => u.IdFirm).FirstOrDefault();
                byteArrayImg = ImageController.GetImage("Firms", idFirm.ToString(), true);
            }
            else
            {
                idCustomer   = db.Interviews.Where(i => i.IdInterview == id).Include(i => i.Campaign.Customer).Select(i => i.Campaign.Customer.IdCustomer).FirstOrDefault();
                idConsultant = db.Interviews.Where(i => i.IdInterview == id).Select(u => u.Id).FirstOrDefault();
                byteArrayImg = ImageController.GetImage("Users", idConsultant, true);
            }

            var    byteArrayCust = ImageController.GetImage("Customers", idCustomer.ToString(), true);
            Stream streamCust    = new MemoryStream(byteArrayCust);
            Stream streamImg     = new MemoryStream(byteArrayImg);

            WordDocument document = new WordDocument();
            IWSection    section  = document.AddSection();

            section.PageSetup.DifferentFirstPage = true;
            section.PageSetup.InsertPageNumbers(false, PageNumberAlignment.Center);
            section.PageSetup.Margins.Left  = 50;
            section.PageSetup.Margins.Right = 50;

            // Add a new paragraph for header to the document.
            IWParagraph headerPar = new WParagraph(document);

            // Add a new table to the header.
            IWTable table = section.HeadersFooters.FirstPageHeader.AddTable();

            // Styles
            const string font = "Segoe UI";

            Style styleTitle = (WParagraphStyle)document.AddParagraphStyle("Title");

            styleTitle.CharacterFormat.FontName  = font;
            styleTitle.CharacterFormat.FontSize  = 25;
            styleTitle.CharacterFormat.TextColor = Color.FromArgb(31, 73, 125);

            Style stylePNode = (WParagraphStyle)document.AddParagraphStyle("PNode");

            stylePNode.CharacterFormat.FontName  = font;
            stylePNode.CharacterFormat.TextColor = Color.FromArgb(238, 131, 0);
            stylePNode.CharacterFormat.FontSize  = 20;

            Style styleNode = (WParagraphStyle)document.AddParagraphStyle("Node");

            styleNode.CharacterFormat.FontName  = font;
            styleNode.CharacterFormat.TextColor = Color.FromArgb(195, 23, 0);
            styleNode.CharacterFormat.FontSize  = 17;

            Style styleQuestion = (WParagraphStyle)document.AddParagraphStyle("Question");

            styleQuestion.CharacterFormat.FontName = font;
            styleQuestion.CharacterFormat.Bold     = true;
            styleQuestion.CharacterFormat.FontSize = 11;

            Style styleAnswer = (WParagraphStyle)document.AddParagraphStyle("Answer");

            styleAnswer.CharacterFormat.FontName  = font;
            styleAnswer.CharacterFormat.FontSize  = 11;
            styleAnswer.CharacterFormat.TextColor = Color.FromArgb(31, 73, 125);

            Style styleList = (WParagraphStyle)document.AddParagraphStyle("List");

            styleList.CharacterFormat.FontName       = font;
            styleList.CharacterFormat.TextColor      = Color.FromArgb(51, 132, 204);
            styleList.CharacterFormat.FontSize       = 11;
            styleList.CharacterFormat.UnderlineStyle = UnderlineStyle.Single;

            Style styleComment = (WParagraphStyle)document.AddParagraphStyle("Comment");

            styleComment.CharacterFormat.FontName = font;
            styleComment.CharacterFormat.Italic   = true;
            styleComment.CharacterFormat.FontSize = 11;

            var format = new RowFormat();

            // Setting cleared table border style.
            format.Borders.BorderType = BorderStyle.Cleared;

            // Inserting table with a row and two columns.
            table.ResetCells(1, 2, format, 265);
            IWTextRange textRange;

            // Inserting logo image to the table cells.
            headerPar = table[0, 0].AddParagraph() as WParagraph;
            headerPar.AppendPicture(Image.FromStream(streamImg));
            (headerPar.Items[0] as WPicture).Width  = 50;
            (headerPar.Items[0] as WPicture).Height = 50;
            headerPar.AppendText(Environment.NewLine);
            var userName = db.Users.Find(idConsultant).FullName;

            switch (type)
            {
            case 1:
            case 2:
                textRange = headerPar.AppendText("Manager: " + userName);
                textRange.CharacterFormat.FontName = font;
                headerPar.AppendText(Environment.NewLine);
                textRange = headerPar.AppendText("Type : Campaign");
                textRange.CharacterFormat.FontName = font;
                break;

            case 3:
            case 4:
                textRange = headerPar.AppendText("Consultant: " + userName);
                textRange.CharacterFormat.FontName = font;
                headerPar.AppendText(Environment.NewLine);
                textRange = headerPar.AppendText("Type: Interview");
                textRange.CharacterFormat.FontName = font;
                break;
            }

            headerPar.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Left;

            var customerName = db.Customers.Find(idCustomer).Name;
            var industryName =
                db.Customers.Where(c => c.IdCustomer == idCustomer)
                .Include(c => c.Industry)
                .Select(c => c.Industry.Name).FirstOrDefault();

            // Inserting text to the table second cell.
            headerPar = table[0, 1].AddParagraph() as WParagraph;

            headerPar.AppendPicture(Image.FromStream(streamCust));
            ((WPicture)headerPar.Items[0]).Width  = 50;
            ((WPicture)headerPar.Items[0]).Height = 50;
            headerPar.AppendText(Environment.NewLine);
            headerPar.AppendText("Customer: " + customerName);
            headerPar.AppendText(Environment.NewLine);
            headerPar.AppendText("Industry: " + industryName);
            headerPar.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Right;

            section           = document.AddSection();
            section.BreakCode = SectionBreakCode.NoBreak;
            IWParagraph paragraph = section.AddParagraph();

            //TableOfContent toc = paragraph.AppendTOC(1, 3);
            //toc.UseHeadingStyles = false;
            //toc.SetTOCLevelStyle(1, "Title");
            //toc.SetTOCLevelStyle(2, "PNode");
            //toc.SetTOCLevelStyle(3, "Node");

            if (type == 1 || type == 2)
            {
                campaign = db.Campaigns.Find(id);
                var idQuestionnaire   = db.Interviews.Where(i => i.Campaign.IdCampaign == id).Include(i => i.Campaign).Select(i => i.Campaign.IdQuestionnaire).FirstOrDefault();
                var questions         = db.Questions.Where(q => q.IdQuestionnaire == idQuestionnaire);
                var questionnairename = db.Questionnaires.Find(idQuestionnaire).Name;
                var interviewees      =
                    db.CandidateCampaigns.Include(cc => cc.Candidate).Include(x => x.Group)
                    .Where(cc => cc.IdCampaign == campaign.IdCampaign)
                    .Select(x => x.Candidate);
                var consultants =
                    db.Campaigns.Where(c => c.IdCampaign == campaign.IdCampaign)
                    .Include(c => c.ApplicationUsers)
                    .Select(c => c.ApplicationUsers);
                string start = "";

                if (campaign.StartDate != null)
                {
                    start = campaign.StartDate.Value.ToShortDateString();
                }

                var end = "";

                if (campaign.EndDate != null)
                {
                    end = campaign.EndDate.Value.ToShortDateString();
                }

                paragraph.AppendText(Environment.NewLine + Environment.NewLine + Environment.NewLine);
                textRange = paragraph.AppendText("Campaign: " + campaign.Name);
                paragraph.ApplyStyle("Title");
                paragraph.AppendText(Environment.NewLine + Environment.NewLine);
                paragraph = section.AddParagraph();
                textRange = paragraph.AppendText("Start: " + start + " -> Expected End: " + end);
                textRange.CharacterFormat.FontName            = font;
                paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Left;
                paragraph.AppendText(Environment.NewLine + Environment.NewLine);
                textRange = paragraph.AppendText("Questionnaire: " + questionnairename);
                textRange.CharacterFormat.FontName = font;
                paragraph.AppendText(Environment.NewLine + Environment.NewLine);
                textRange = paragraph.AppendText("Description: " + campaign.Description);
                textRange.CharacterFormat.FontName = font;
                paragraph.AppendText(Environment.NewLine + Environment.NewLine);

                if (type == 2 && !string.IsNullOrEmpty(campaign.Comment))
                {
                    textRange = paragraph.AppendText("Comment: " + campaign.Comment);
                    textRange.CharacterFormat.FontName = font;
                }

                paragraph.AppendText(Environment.NewLine + Environment.NewLine);

                paragraph = section.AddParagraph();
                paragraph.AppendText("Consultants: ");
                paragraph.ApplyStyle("List");

                foreach (var consultant in consultants)
                {
                    paragraph = section.AddParagraph();
                    textRange = paragraph.AppendText(consultant.FullName);
                    textRange.CharacterFormat.FontName = font;
                    paragraph.ListFormat.ApplyDefBulletStyle();
                }
                var dbg = new ApplicationDbContext();
                if (type == 2)
                {
                    paragraph = section.AddParagraph();
                    paragraph.AppendText("Interviewees: ");
                    paragraph.ApplyStyle("List");

                    foreach (var interviewee in interviewees)
                    {
                        paragraph = section.AddParagraph();
                        var group     = dbg.CandidateCampaigns.Where(cc => cc.IdCandidate == interviewee.IdCandidate).Select(cc => cc.Group).FirstOrDefault();
                        var groupname = "";
                        if (group != null)
                        {
                            groupname = group.Name;
                        }
                        textRange = paragraph.AppendText(interviewee.FullNameAndFunction + " - " + groupname);
                        textRange.CharacterFormat.FontName = font;
                        paragraph.ListFormat.ApplyDefBulletStyle();
                    }
                }

                // Nodes
                var fullnodes = db.Nodes.Where(x => x.IdQuestionnaire == idQuestionnaire).ToList();

                if (fullnodes.Any())
                {
                    var root = fullnodes.FirstOrDefault(r => r.IsRoot);
                    if (root != null)
                    {
                        var nodes  = fullnodes.Where(n => !n.IsRoot && n.IdParentNode != root.IdNode).ToList();
                        var pnodes = fullnodes.FindAll(p => p.IdParentNode == root.IdNode).ToList();

                        foreach (var pnode in pnodes)
                        {
                            section   = document.AddSection();
                            paragraph = section.AddParagraph();
                            paragraph.AppendText(Environment.NewLine);
                            paragraph.AppendText(pnode.Name);
                            paragraph.ApplyStyle("PNode");

                            if (db.Questions.Any(x => x.IdNode == pnode.IdNode))
                            {
                                foreach (var question in questions.Where(q => q.IdNode == pnode.IdNode))
                                {
                                    paragraph = section.AddParagraph();
                                    paragraph.AppendText(question.Description);
                                    paragraph.ApplyStyle("Question");
                                    paragraph.AppendText(Environment.NewLine);

                                    if (!question.IsRelevant)
                                    {
                                        textRange.CharacterFormat.TextColor = Color.DimGray;
                                    }

                                    section.AddParagraph().AppendText(Environment.NewLine);
                                    var dbi = new ApplicationDbContext();

                                    foreach (var answer in dba.Answers.Where(a => a.IdQuestion == question.IdQuestion && a.Interview.IdCampaign == campaign.IdCampaign && !a.Interview.Deleted))
                                    {
                                        var intervname = dbi.Interviews.Find(answer.IdInterview).Name;
                                        paragraph = section.AddParagraph();
                                        paragraph.AppendText("Answer from Interview " + intervname + ":");
                                        paragraph.ApplyStyle("Answer");

                                        if (answer != null && answer.IntervieweeAnswer != null)
                                        {
                                            if (question.AnswerType == AnswerType.Integer)
                                            {
                                                textRange = paragraph.AppendText(answer.IntervieweeAnswer + " / 5");
                                            }
                                            else
                                            {
                                                textRange = paragraph.AppendText(answer.IntervieweeAnswer);
                                            }
                                            textRange.CharacterFormat.FontName = font;
                                            textRange.CharacterFormat.Bold     = true;
                                            paragraph.AppendText(Environment.NewLine);
                                        }
                                        else
                                        {
                                            paragraph = section.AddParagraph();
                                            textRange = paragraph.AppendText("No answer yet");
                                            textRange.CharacterFormat.FontName  = font;
                                            textRange.CharacterFormat.TextColor = Color.DarkGray;
                                            paragraph.AppendText(Environment.NewLine);
                                        }

                                        if (type == 2 && !string.IsNullOrEmpty(answer.ConsultantComment))
                                        {
                                            if (answer.Comment != null)
                                            {
                                                paragraph = section.AddParagraph();
                                                paragraph.AppendText("Comment: " + answer.Comment);
                                                paragraph.ApplyStyle("Comment");
                                                paragraph.AppendText(Environment.NewLine);
                                            }
                                            paragraph = section.AddParagraph();
                                            paragraph.AppendText("Consultant's comment: " + answer.ConsultantComment);
                                            paragraph.ApplyStyle("Comment");
                                            paragraph.AppendText(Environment.NewLine);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                foreach (var node in nodes.Where(n => n.IdParentNode == pnode.IdNode))
                                {
                                    paragraph = section.AddParagraph();
                                    paragraph.AppendText(Environment.NewLine);
                                    paragraph.AppendText(" > " + node.Name);
                                    paragraph.ApplyStyle("Node");
                                    paragraph.AppendText(Environment.NewLine);

                                    foreach (var question in questions.Where(q => q.IdNode == node.IdNode))
                                    {
                                        paragraph = section.AddParagraph();
                                        paragraph.AppendText(question.Description);
                                        paragraph.ApplyStyle("Question");
                                        paragraph.AppendText(Environment.NewLine);

                                        if (!question.IsRelevant)
                                        {
                                            textRange.CharacterFormat.TextColor = Color.DimGray;
                                        }
                                        var dbi = new ApplicationDbContext();

                                        foreach (var answer in dba.Answers.Where(a => a.IdQuestion == question.IdQuestion && a.Interview.IdCampaign == campaign.IdCampaign && !a.Interview.Deleted))
                                        {
                                            var intervname = dbi.Interviews.Find(answer.IdInterview).Name;
                                            paragraph = section.AddParagraph();
                                            paragraph.AppendText("Answer from Interview " + intervname + ":");
                                            paragraph.ApplyStyle("Answer");

                                            if (answer.IntervieweeAnswer != null)
                                            {
                                                paragraph = section.AddParagraph();
                                                if (question.AnswerType == AnswerType.Integer)
                                                {
                                                    textRange = paragraph.AppendText(answer.IntervieweeAnswer + " / 5");
                                                }
                                                else
                                                {
                                                    textRange = paragraph.AppendText(answer.IntervieweeAnswer);
                                                }
                                                textRange.CharacterFormat.FontName = font;
                                                textRange.CharacterFormat.Bold     = true;
                                                paragraph.AppendText(Environment.NewLine);
                                            }
                                            else
                                            {
                                                paragraph = section.AddParagraph();
                                                textRange = paragraph.AppendText("No answer yet");
                                                textRange.CharacterFormat.FontName  = font;
                                                textRange.CharacterFormat.TextColor = Color.DarkGray;
                                                paragraph.AppendText(Environment.NewLine);
                                            }

                                            if (type == 2 && !string.IsNullOrEmpty(answer.ConsultantComment))
                                            {
                                                if (answer.Comment != null)
                                                {
                                                    paragraph = section.AddParagraph();
                                                    paragraph.AppendText("Comment: " + answer.Comment);
                                                    paragraph.ApplyStyle("Comment");
                                                    paragraph.AppendText(Environment.NewLine);
                                                }
                                                paragraph = section.AddParagraph();
                                                paragraph.AppendText("Consultant's comment: " + answer.ConsultantComment);
                                                paragraph.ApplyStyle("Comment");
                                                paragraph.AppendText(Environment.NewLine);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else if (type == 3 || type == 4)
            {
                interview = db.Interviews.Find(id);
                var idInterviewee   = db.Interviewees.Where(i => i.IdInterview == id).Select(i => i.IdCandidate).FirstOrDefault();
                var idQuestionnaire = db.Interviews.Where(i => i.IdInterview == id).Include(i => i.Campaign).Select(i => i.Campaign.IdQuestionnaire).FirstOrDefault();
                var questions       = db.Questions.Where(q => q.IdQuestionnaire == idQuestionnaire);
                var interviewees    =
                    db.Interviewees.Include(cc => cc.Candidate)
                    .Where(cc => cc.IdInterview == interview.IdInterview)
                    .Select(x => x.Candidate);
                var consultant = interview.ApplicationUsers;

                paragraph.AppendText(Environment.NewLine + Environment.NewLine + Environment.NewLine);
                paragraph.AppendText("Interview: " + interview.Name);
                paragraph.ApplyStyle("Title");
                paragraph.AppendText(Environment.NewLine + Environment.NewLine);
                paragraph = section.AddParagraph();
                textRange = paragraph.AppendText("Date: " + interview.Date.ToShortDateString() + Environment.NewLine);
                textRange.CharacterFormat.FontName = font;
                paragraph.AppendText(Environment.NewLine + Environment.NewLine);

                textRange = paragraph.AppendText("Description: " + interview.Description);
                textRange.CharacterFormat.FontName = font;
                paragraph.AppendText(Environment.NewLine + Environment.NewLine);

                if (type == 4 && interview.Comment != null)
                {
                    textRange = paragraph.AppendText("Comment: " + interview.Comment + Environment.NewLine);
                    textRange.CharacterFormat.FontName = font;
                }

                paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Left;
                paragraph.AppendText(Environment.NewLine + Environment.NewLine);

                paragraph = section.AddParagraph();
                paragraph.AppendText("Consultants: ");
                paragraph.ApplyStyle("List");

                paragraph = section.AddParagraph();
                textRange = paragraph.AppendText(consultant.FullName);
                textRange.CharacterFormat.FontName = font;
                paragraph.ListFormat.ApplyDefBulletStyle();

                paragraph = section.AddParagraph();
                paragraph.AppendText("Interviewee(s): ");
                paragraph.ApplyStyle("List");

                var dbc = new ApplicationDbContext();

                foreach (var i in interviewees)
                {
                    paragraph = section.AddParagraph();
                    var group =
                        dbc.CandidateCampaigns.Where(
                            cc => cc.IdCandidate == i.IdCandidate && cc.IdCampaign == interview.IdCampaign).Select(cc => cc.Group).FirstOrDefault();
                    var groupname = "";
                    if (group != null)
                    {
                        groupname = group.Name;
                    }
                    textRange = paragraph.AppendText(i.FullNameAndFunction + " - " + groupname);
                    textRange.CharacterFormat.FontName = font;
                    textRange.CharacterFormat.Bold     = false;
                    paragraph.ListFormat.ApplyDefBulletStyle();
                }

                var fullnodes = db.Nodes.Where(x => x.IdQuestionnaire == idQuestionnaire).ToList();

                if (fullnodes.Any())
                {
                    var root = fullnodes.FirstOrDefault(r => r.IsRoot);
                    if (root != null)
                    {
                        var nodes  = fullnodes.Where(n => !n.IsRoot && n.IdParentNode != root.IdNode).ToList();
                        var pnodes = fullnodes.FindAll(p => p.IdParentNode == root.IdNode).ToList();

                        foreach (var pnode in pnodes)
                        {
                            var score = db.CustomScores.FirstOrDefault(c => c.IdNode == pnode.IdNode && c.IdInterview == interview.IdInterview);
                            var val   = 0;

                            if (score != null)
                            {
                                val = score.Value != null ? score.Value : 0;
                            }
                            section   = document.AddSection();
                            paragraph = section.AddParagraph();
                            paragraph.AppendText(Environment.NewLine);
                            paragraph.AppendText(pnode.Name + "     " + val + " / 5");
                            paragraph.ApplyStyle("PNode");

                            if (db.Questions.Any(x => x.IdNode == pnode.IdNode))
                            {
                                foreach (var question in questions.Where(q => q.IdNode == pnode.IdNode))
                                {
                                    var answer = dba.Answers.FirstOrDefault(a => a.IdQuestion == question.IdQuestion && a.IdInterview == id);

                                    paragraph = section.AddParagraph();
                                    paragraph.AppendText(question.Description);
                                    paragraph.ApplyStyle("Question");
                                    paragraph.AppendText(Environment.NewLine);
                                    if (answer != null && answer.IntervieweeAnswer != null)
                                    {
                                        paragraph = section.AddParagraph();
                                        paragraph.AppendText("Answer:");
                                        paragraph.ApplyStyle("Answer");
                                        paragraph = section.AddParagraph();

                                        if (question.AnswerType == AnswerType.Integer)
                                        {
                                            paragraph.AppendText(answer.IntervieweeAnswer + " / 5");
                                        }
                                        else
                                        {
                                            paragraph.AppendText(answer.IntervieweeAnswer);
                                        }
                                        textRange.CharacterFormat.FontName = font;
                                        textRange.CharacterFormat.Bold     = true;
                                        paragraph.AppendText(Environment.NewLine);

                                        if (type == 4 && !string.IsNullOrEmpty(answer.ConsultantComment))
                                        {
                                            if (answer.Comment != null)
                                            {
                                                paragraph = section.AddParagraph();
                                                paragraph.AppendText("Comment: " + answer.Comment);
                                                paragraph.ApplyStyle("Comment");
                                                paragraph.AppendText(Environment.NewLine);
                                            }
                                            paragraph = section.AddParagraph();
                                            paragraph.AppendText("Consultant's comment: " + answer.ConsultantComment);
                                            paragraph.ApplyStyle("Comment");
                                            paragraph.AppendText(Environment.NewLine);
                                        }
                                    }
                                    else
                                    {
                                        paragraph = section.AddParagraph();
                                        textRange = paragraph.AppendText("No answer yet");
                                        textRange.CharacterFormat.FontName  = font;
                                        textRange.CharacterFormat.TextColor = Color.DarkGray;
                                        paragraph.AppendText(Environment.NewLine);
                                    }
                                }
                            }
                            else
                            {
                                foreach (var node in nodes.Where(n => n.IdParentNode == pnode.IdNode))
                                {
                                    paragraph = section.AddParagraph();
                                    paragraph.AppendText(Environment.NewLine);
                                    paragraph.AppendText(" > " + node.Name + "     " + val + " / 5");
                                    paragraph.ApplyStyle("Node");
                                    paragraph.AppendText(Environment.NewLine);

                                    foreach (var question in questions.Where(q => q.IdNode == node.IdNode))
                                    {
                                        var answer = dba.Answers.FirstOrDefault(a => a.IdQuestion == question.IdQuestion && a.IdInterview == id);

                                        paragraph = section.AddParagraph();
                                        paragraph.AppendText(question.Description);
                                        paragraph.ApplyStyle("Question");
                                        paragraph.AppendText(Environment.NewLine);

                                        if (answer != null && answer.IntervieweeAnswer != null)
                                        {
                                            paragraph = section.AddParagraph();
                                            paragraph.AppendText("Answer:");
                                            paragraph.ApplyStyle("Answer");
                                            paragraph = section.AddParagraph();
                                            if (question.AnswerType == AnswerType.Integer)
                                            {
                                                paragraph.AppendText(answer.IntervieweeAnswer + " / 5");
                                            }
                                            else
                                            {
                                                paragraph.AppendText(answer.IntervieweeAnswer);
                                            }
                                            textRange.CharacterFormat.FontName = font;
                                            textRange.CharacterFormat.Bold     = true;
                                            paragraph.AppendText(Environment.NewLine);

                                            if (type == 4 && !string.IsNullOrEmpty(answer.ConsultantComment))
                                            {
                                                if (answer.Comment != null)
                                                {
                                                    paragraph = section.AddParagraph();
                                                    paragraph.AppendText("Comment: " + answer.Comment);
                                                    paragraph.ApplyStyle("Comment");
                                                    paragraph.AppendText(Environment.NewLine);
                                                }

                                                paragraph = section.AddParagraph();
                                                paragraph.AppendText("Consultant's comment: " + answer.ConsultantComment);
                                                paragraph.ApplyStyle("Comment");
                                                paragraph.AppendText(Environment.NewLine);
                                            }
                                        }
                                        else
                                        {
                                            paragraph = section.AddParagraph();
                                            textRange = paragraph.AppendText("No answer yet");
                                            textRange.CharacterFormat.FontName  = font;
                                            textRange.CharacterFormat.TextColor = Color.DarkGray;
                                            paragraph.AppendText(Environment.NewLine);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            switch (type)
            {
            case 1:
                if (!pdf)
                {
                    return(document.ExportAsActionResult(DateTime.Now.ToShortDateString() + "_" + campaign.Name + "_Customer.docx", FormatType.Word2013, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment));
                }
                else
                {
                    var converted = ConvertToPDF(document);
                    return(converted.ExportAsActionResult(DateTime.Now.ToShortDateString() + "_" + campaign.Name + "_Customer.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save));
                }

            case 2:
                if (!pdf)
                {
                    return(document.ExportAsActionResult(DateTime.Now.ToShortDateString() + "_" + campaign.Name + "_Firm.docx", FormatType.Word2013, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment));
                }
                else
                {
                    var converted = ConvertToPDF(document);
                    return(converted.ExportAsActionResult(DateTime.Now.ToShortDateString() + "_" + campaign.Name + "_Firm.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save));
                }

            case 3:
                if (!pdf)
                {
                    return(document.ExportAsActionResult(DateTime.Now.ToShortDateString() + "_" + interview.Name + "_Customer.docx", FormatType.Word2013, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment));
                }
                else
                {
                    var converted = ConvertToPDF(document);
                    return(converted.ExportAsActionResult(DateTime.Now.ToShortDateString() + "_" + interview.Name + "_Customer.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save));
                }

            case 4:
                if (!pdf)
                {
                    return(document.ExportAsActionResult(DateTime.Now.ToShortDateString() + "_" + interview.Name + "_Firm.docx", FormatType.Word2013, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment));
                }
                else
                {
                    var converted = ConvertToPDF(document);
                    return(converted.ExportAsActionResult(DateTime.Now.ToShortDateString() + "_" + interview.Name + "_Firm.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save));
                }
            }

            return(document.ExportAsActionResult("Report.docx", FormatType.Word2013, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment));
        }
Пример #7
0
        private async void btnGuardar_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
        {
            try
            {
                if (document != null)
                {
                    document.Close();
                    document = null;
                }
                if (document == null)
                {
                    document  = new WordDocument();
                    docStream = File.OpenRead(Path.GetFullPath(sFilePathWord));
                    await document.OpenAsync(docStream, FormatType.Docx);

                    docStream.Dispose();
                }
                foreach (var x in textBoxes)
                {
                    replace(x.Name, x.Text, sFilePathWord);
                }
                foreach (var z in richEditBoxes)
                {
                    string valor;
                    z.TextDocument.GetText(Windows.UI.Text.TextGetOptions.None, out valor);
                    replace(z.Name, valor, sFilePathWord);
                }
                foreach (var x in comboBoxes)
                {
                    if (x.SelectedItem == null)
                    {
                        replace(x.Name, "", sFilePathWord);
                    }
                    else
                    if (x.Name.ToLower().Contains("nombre_tutor"))
                    {
                        replace("Grupo", (x.SelectedItem as TutoresProfesores).grupo + " de " + (x.SelectedItem as TutoresProfesores).semestre + " semestre", sFilePathWord);
                        replace("Correo", (x.SelectedItem as TutoresProfesores).correo, sFilePathWord);
                        if ((x.SelectedItem as TutoresProfesores).imagen != null)
                        {
                            //TextSelection textSelections = document.Find(new Regex("^<[Imagen]>"));
                            Stream     imageStream = (x.SelectedItem as TutoresProfesores).imagen;
                            WParagraph paragraph   = new WParagraph(document);
                            WPicture   picture     = paragraph.AppendPicture(imageStream) as WPicture;
                            picture.Width  = 60;
                            picture.Height = 60;
                            TextSelection newSelection = new TextSelection(paragraph, 0, 1);
                            TextBodyPart  bodyPart     = new TextBodyPart(document);
                            bodyPart.BodyItems.Add(paragraph);
                            document.Replace("<[Imagen]>", bodyPart, true, true);
                        }
                        replace(x.Name, x.SelectedItem.ToString(), sFilePathWord);
                    }
                    replace(x.Name, x.SelectedItem.ToString(), sFilePathWord);
                }
                foreach (var fecha in datePickers)
                {
                    if (fecha.SelectedDate == null)
                    {
                        fecha.SelectedDate = DateTime.UtcNow;
                    }
                    if (fecha.Name.ToLower().Contains("año"))
                    {
                        replace(fecha.Name, fecha.Date.DateTime.ToString("yyyy"), sFilePathWord);
                    }
                    else
                    {
                        replace(fecha.Name, fecha.Date.DateTime.ToString("MMMM"), sFilePathWord);
                    }
                }
                foreach (var dp in calendarDatePickers)
                {
                    if (dp.Date.Value.DateTime == null)
                    {
                        dp.Date = DateTime.UtcNow;
                    }
                    replace(dp.Name, dp.Date.Value.DateTime.ToString("dd/MMMM/yyyy"), sFilePathWord);
                }
                if (checkBoxes != null)
                {
                    foreach (var cb in checkBoxes)
                    {
                        if (cb.IsChecked == true)
                        {
                            replace(cb.Name, "x", sFilePathWord);
                        }
                        else
                        {
                            replace(cb.Name, "", sFilePathWord);
                        }
                    }
                }
                replace("Jefe_Tutorias", await DBAssets.getStringAsync((App.Current as App).ConnectionString, "SELECT nombre FROM CoordinadoresTutorias;"), sFilePathWord);
                replace("Jefe_Departamento", await DBAssets.getStringAsync((App.Current as App).ConnectionString, "SELECT nombre FROM JefesDepartamentos WHERE id_jefe = 1;"), sFilePathWord);
                replace("Jefe_Tutorias_Institucional", await DBAssets.getStringAsync((App.Current as App).ConnectionString, "SELECT nombre FROM CoordinadoresTutoriasInstitucionales;"), sFilePathWord);
                replace("Jefe_Desarrollo_Académico", await DBAssets.getStringAsync((App.Current as App).ConnectionString, "SELECT nombre FROM JefesDepartamentos WHERE id_jefe = 2;"), sFilePathWord);

                FileSavePicker savePicker = new FileSavePicker();
                savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
                savePicker.SuggestedFileName      = "Resultado";
                savePicker.FileTypeChoices.Add("Word Documents", new List <string>()
                {
                    ".docx"
                });
                StorageFile outputStorageFile = await savePicker.PickSaveFileAsync();

                await document.SaveAsync(outputStorageFile, FormatType.Docx);
            }
            catch (Exception ex)
            {
                var err = new MessageDialog("Unable to open File!" + ex.Message);
                await err.ShowAsync();
            }
        }