Beispiel #1
0
        public void W017_InsertBefore_InsertAfter()
        {
            using (var stream = GetStream(TestFiles.Document))
                using (var doc = WordprocessingDocument.Open(stream, false))
                {
                    var mdp     = doc.MainDocumentPart;
                    var newPara = new W.Paragraph(
                        new W.Run(
                            new W.Text("Hello")));
                    var firstPara = mdp.Document.Body.Elements <W.Paragraph>().FirstOrDefault();
                    mdp.Document.Body.InsertBefore(newPara, firstPara);

                    var newPara2 = (OpenXmlElement)newPara.Clone();
                    mdp.Document.Body.InsertAfter(newPara2, firstPara);

                    var v = new OpenXmlValidator(FileFormatVersions.Office2013);

                    Assert.Equal(2, v.Validate(doc).Count());
                }
        }
        public void W017_InsertBefore_InsertAfter()
        {
            using (var stream = GetStream(TestFiles.Document))
                using (var doc = WordprocessingDocument.Open(stream, false))
                {
                    var mdp     = doc.MainDocumentPart;
                    var newPara = new W.Paragraph(
                        new W.Run(
                            new W.Text("Hello")));
                    var firstPara = mdp.Document.Body.Elements <W.Paragraph>().FirstOrDefault();
                    mdp.Document.Body.InsertBefore(newPara, firstPara);

                    var newPara2 = (OpenXmlElement)newPara.Clone();
                    mdp.Document.Body.InsertAfter(newPara2, firstPara);

                    var v    = new OpenXmlValidator(FileFormatVersions.Office2013);
                    var errs = v.Validate(doc);
                    var cnt  = errs.Count();

                    Assert.True(cnt == 416 || cnt == 2); // v3.0 correctly reports fewer errors than v2.5.1
                }
        }
        private void insertDetailsSkill(wp.Paragraph par, MainDocumentPart mainPart)
        {
            // na primira fase fazer sem os gráficos

            // ir buscar o template
            // cria o documento final base com base no template
            String templateDoc = _projecto.Template_Mnemonica;

            String filename = rootPath + "docTemplates\\" + templateDoc + "_SKILS_DETAILS.docx";

            wp.Table tabTitulo1;

            wp.Table tab1;
            wp.TableRow rowTab1Par;
            wp.TableRow rowTab1Impar;

            wp.Run plusSign;
            wp.Run minusSign;
            wp.Run naSign;
            wp.Run eqSign;

            wp.Table legendaTab1;
            wp.Paragraph quebraPagina;

            using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(filename, true))
            {
                // primeira tabela do template
                tabTitulo1 = (wp.Table)wordDoc.MainDocumentPart.Document.Body.Elements<wp.Table>().ElementAt(0).Clone();

                // primeira tabela com comportamentos
                tab1 = (wp.Table)wordDoc.MainDocumentPart.Document.Body.Elements<wp.Table>().ElementAt(1).Clone();
                rowTab1Impar = (wp.TableRow)tab1.Elements<wp.TableRow>().ElementAt(1).Clone();
                rowTab1Par = (wp.TableRow)tab1.Elements<wp.TableRow>().ElementAt(2).Clone();
                tab1.Elements<wp.TableRow>().ElementAt(1).Remove();
                tab1.Elements<wp.TableRow>().ElementAt(1).Remove();

                // apanha os run + e - (mais e menos)
                minusSign = (wp.Run)rowTab1Impar.Elements<wp.TableCell>().ElementAt(1).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Clone();
                naSign = (wp.Run)rowTab1Impar.Elements<wp.TableCell>().ElementAt(2).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Clone();
                plusSign = (wp.Run)rowTab1Par.Elements<wp.TableCell>().ElementAt(1).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Clone();
                eqSign = (wp.Run)rowTab1Par.Elements<wp.TableCell>().ElementAt(2).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Clone();

                // eliminar o mais e o menos das linhas e o tracinho
                rowTab1Impar.Elements<wp.TableCell>().ElementAt(1).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Remove();
                rowTab1Impar.Elements<wp.TableCell>().ElementAt(2).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Remove();
                rowTab1Par.Elements<wp.TableCell>().ElementAt(1).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Remove();
                rowTab1Par.Elements<wp.TableCell>().ElementAt(2).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Remove();

                // depois tenho algures aqui a legenda da tabela
                legendaTab1 = (wp.Table)wordDoc.MainDocumentPart.Document.Body.Elements<wp.Table>().ElementAt(2).Clone();

                //parágrafo quebra de página
                quebraPagina = new wp.Paragraph(new wp.Run(new wp.Text("")));
                quebraPagina.ParagraphProperties = new wp.ParagraphProperties();
                quebraPagina.ParagraphProperties.PageBreakBefore = new wp.PageBreakBefore();
                quebraPagina.ParagraphProperties.PageBreakBefore.Val = DocumentFormat.OpenXml.OnOffValue.FromBoolean(true);

            }
            // fim da recolha dos elementos template.
            // passamos para a inserção destes elementos para cada competência

            // eliminar o texto do marcador
            par.Elements<wp.Run>().First().Elements<wp.Text>().First().Text = "";

            wp.Paragraph p;
            wp.Run r;
            wp.Text t;
            wp.Table tab;

            wp.Table tab1Temp;

            wp.TableRow tab1r;
            wp.TableCell tab1c;

            int count = 0;

            String pergunta;
            String tempTexto;
            int contaPaginas = 1;

            foreach (FamiliaCompetencias fam in _Modelo.Familias.Values)
                foreach (Competencia comp in fam.Competencias.Values)
                {
                    // são duas páginas de detalhes por cada competência
                    // header da 1ªpágina

                    if (comp.Type == "C") continue; // é uma competência comentário
                    if (comp.Type == "D") continue; // é uma zona de derrailers

                    // Nome da competência
                    tab = (wp.Table)tabTitulo1.Clone();
                    p = tab.Elements<wp.TableRow>().ElementAt(0).Elements<wp.TableCell>().First().Elements<wp.Paragraph>().First();
                    r = p.Elements<wp.Run>().First();
                    t = r.Elements<wp.Text>().First();
                    t.Text = repCalcs.getCompetenciaText(comp.CompetenciaID);

                    par.Append(tab);// o nome da competência

                    // PREPARA AS DUAS TABELAS COM OS DETALHES
                    tab1Temp = (wp.Table)tab1.Clone();
                    // tab2Temp = (wp.Table)tab2.Clone();

                    // os valores obtidos em cada comportamento
                    List<ReportRow> dataComportamentos = repCalcs.getAvaliacaoComportamentosCriticos(comp.CompetenciaID);
                    List<ReportRow> dataComportamentosFracosFortes = repCalcs.getAvaliacaoComportamentosCriticos_FracosFortes(comp.CompetenciaID);
                    count = 0;
                    foreach (Pratica prat in comp.Praticas.Values)
                        foreach (Pergunta perg in prat.Perguntas.Values)
                        {
                            pergunta = repCalcs.getPergunta(perg.PerguntaID);
                            if (count % 2 == 0)
                            {
                                // linha par
                                tab1r = (wp.TableRow)rowTab1Par.Clone();
                                // tab2r = (wp.TableRow)rowTab2Par.Clone();
                            }
                            else
                            {
                                // linha impar
                                tab1r = (wp.TableRow)rowTab1Impar.Clone();
                                // tab2r = (wp.TableRow)rowTab2Impar.Clone();
                            }

                            tab1c = tab1r.Elements<wp.TableCell>().ElementAt(0);
                            // tab2c = tab2r.Elements<wp.TableCell>().ElementAt(0);

                            if (count + 1 < dataComportamentos.Count)
                            {
                                // introduz o texto da pergunta
                                tab1c.Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Elements<wp.Text>().First().Text = dataComportamentos[count + 1].dados[0];
                                // tab2c.Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Elements<wp.Text>().First().Text = dataComportamentos[count + 1].dados[0];
                                // introduz os valores de cada prática

                                // os mais e os menos desta linha (prática), por S;B;P;C;O
                                for (int i = 1; i < dataComportamentos[count + 1].dados.Length; i++ )
                                {
                                    tab1c = tab1r.Elements<wp.TableCell>().ElementAt(i);
                                    fillPlusMinus(tab1c, dataComportamentos[count + 1].dados[i], plusSign, minusSign, naSign, eqSign);
                                }
                            }

                            // junta às tabelas as linhas
                            tab1Temp.Append(tab1r);
                            // tab2Temp.Append(tab2r);

                            count++;
                        }

                    // INSERE A TABELA 1
                    par.Append(tab1Temp);
                    // LEGENDA 1

                    tempTexto = "";

                    //legendaTab1.Elements<wp.TableRow>().ElementAt(1).Elements<wp.TableCell>().First().Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Elements<wp.Text>().First().Text = tempTexto;
                    par.Append((wp.Table)legendaTab1.Clone());

                    // QUEBRA DE PÁGINA 1
                    par.Append((wp.Paragraph)quebraPagina.Clone());

                    // um parágrafo de espaçamento para que as tabelas não colem
                    par.Append(new wp.Paragraph());

                    contaPaginas++;
                }
        }
Beispiel #4
0
 public void W017_InsertBefore_InsertAfter()
 {
     var docName = "Document.docx";
                 var ba = File.ReadAllBytes(Path.Combine(TestUtil.SourceDir.FullName, docName));
     using (MemoryStream ms = new MemoryStream())
     {
         ms.Write(ba, 0, ba.Length);
         using (WordprocessingDocument doc = WordprocessingDocument.Open(ms, true))
         {
             MainDocumentPart mdp = doc.MainDocumentPart;
             var newPara = new W.Paragraph(
                 new W.Run(
                     new W.Text("Hello")));
             var firstPara = mdp.Document.Body.Elements<W.Paragraph>().FirstOrDefault();
             mdp.Document.Body.InsertBefore(newPara, firstPara);
             var newPara2 = (OpenXmlElement)newPara.Clone();
             mdp.Document.Body.InsertAfter(newPara2, firstPara);
             OpenXmlValidator v = new OpenXmlValidator(FileFormatVersions.Office2013);
             var errs = v.Validate(doc);
             Assert.Equal(416, errs.Count());
         }
     }
 }
Beispiel #5
0
    private void insertOverviewSkill(wp.Paragraph par, MainDocumentPart mainPart)
    {
        // ir buscar o template
        // cria o documento final base com base no template
        String templateDoc = _projecto.Template_Mnemonica;

        String filename = templatePath + "\\" + templateDoc + "_OVERVIEW_SKILS_DETAILS.docx";

        wp.Paragraph parHeading = null;

        wp.Table tab1 = null;
        wp.TableRow rowTab1Par = null;
        wp.TableRow rowTab1Impar = null;
        wp.TableRow rowTab1Linha3 = null;

        wp.Run red = null;
        wp.Run yellow = null;
        wp.Run green = null;

        wp.Table legendaTab2 = null;
        wp.Paragraph quebraPagina = null;

        using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(filename, true))
        {
            try
            {
                // o primeiro Parágrafo tem o nome do subcapítulo
                parHeading = (wp.Paragraph)wordDoc.MainDocumentPart.Document.Body.Elements<wp.Paragraph>().ElementAt(0);

                // primeira tabela do template
                tab1 = (wp.Table)wordDoc.MainDocumentPart.Document.Body.Elements<wp.Table>().ElementAt(0).Clone();

                rowTab1Impar = (wp.TableRow)tab1.Elements<wp.TableRow>().ElementAt(1).Clone();
                rowTab1Par = (wp.TableRow)tab1.Elements<wp.TableRow>().ElementAt(2).Clone();
                rowTab1Linha3 = (wp.TableRow)tab1.Elements<wp.TableRow>().ElementAt(3).Clone();
                tab1.Elements<wp.TableRow>().ElementAt(1).Remove();
                tab1.Elements<wp.TableRow>().ElementAt(1).Remove();
                tab1.Elements<wp.TableRow>().ElementAt(1).Remove();

                // apanha os run + e - (mais e menos)
                green = (wp.Run)rowTab1Impar.Elements<wp.TableCell>().ElementAt(2).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Clone();
                red = (wp.Run)rowTab1Par.Elements<wp.TableCell>().ElementAt(2).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Clone();
                yellow = (wp.Run)rowTab1Linha3.Elements<wp.TableCell>().ElementAt(2).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Clone();

                // eliminar o mais e o menos das linhas e o tracinho
                rowTab1Impar.Elements<wp.TableCell>().ElementAt(2).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Remove();
                rowTab1Par.Elements<wp.TableCell>().ElementAt(2).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Remove();
                rowTab1Linha3.Elements<wp.TableCell>().ElementAt(2).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Remove();

            }
            catch
            {

            }

            //parágrafo quebra de página
            quebraPagina = new wp.Paragraph(new wp.Run(new wp.Text("")));
            quebraPagina.ParagraphProperties = new wp.ParagraphProperties();
            quebraPagina.ParagraphProperties.PageBreakBefore = new wp.PageBreakBefore();
            quebraPagina.ParagraphProperties.PageBreakBefore.Val = DocumentFormat.OpenXml.OnOffValue.FromBoolean(true);

        }
        // fim da recolha dos elementos template.
        // passamos para a inserção destes elementos para cada competência

        // eliminar o texto do marcador
        par.Elements<wp.Run>().First().Elements<wp.Text>().First().Text = "";

        wp.Paragraph p = null;
        wp.Run r = null;
        wp.Text t = null;
        wp.Table tab1Temp = null;

        wp.TableRow tab1r = null;
        wp.TableCell tab1c = null;

        int count = 0;

        String pergunta;
        //String tempTexto;
        int contaPaginas = 1;

        String gap;

        foreach (FamiliaCompetencias fam in _Modelo.Familias.Values)
            foreach (Competencia comp in fam.Competencias.Values)
            {
                // são duas páginas de detalhes por cada competência
                // header da 1ªpágina

                if (comp.Type == "C") continue; // é uma competência comentário
                if (comp.Type == "D") continue; // é uma zona de derrailers

                // Nome da competência
                p = (wp.Paragraph)parHeading.Clone();
                r = p.Elements<wp.Run>().First();
                t = r.Elements<wp.Text>().First();
                t.Text = repCalcs.getCompetenciaText(comp.CompetenciaID);

                par.Append(p);

                // PREPARA A TABELA COM OS DETALHES
                tab1Temp = (wp.Table)tab1.Clone();
                tab1r = (wp.TableRow)tab1Temp.Elements<wp.TableRow>().ElementAt(0); // header da tabela
                tab1c = tab1r.Elements<wp.TableCell>().ElementAt(0); // c´clula com o local para o texto

                // os valores obtidos em cada comportamento
                // List<ReportRow> dataComportamentos = repCalcs.getAvaliacaoComportamentosCriticos(comp.CompetenciaID);
                List<ReportRow> dataComportamentosFracosFortes = repCalcs.getAvaliacaoComportamentosCriticos_FracosFortes_3Fatores(comp.CompetenciaID);
                count = 0;
                foreach (Pratica prat in comp.Praticas.Values)
                {
                    if (tab1c.Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Elements<wp.Text>().First().Text == "#TEXT_PRATICA#")
                    {
                        String plainText;
                        if (Utility.theServer != null)
                            plainText = Regex.Replace(Utility.theServer.HtmlDecode(prat.Description), "<[^>]+?>", "");
                        else
                            plainText = prat.Description;

                        plainText = DBHelper.ParseTextProprio(plainText, new Pessoa(), false);

                        tab1c.Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Elements<wp.Text>().First().Text = plainText;

                    }
                    foreach (Pergunta perg in prat.Perguntas.Values)
                    {
                        pergunta = repCalcs.getPergunta(perg.PerguntaID);
                        if (count % 2 == 0)
                        {
                            // linha par
                            tab1r = (wp.TableRow)rowTab1Par.Clone();
                        }
                        else
                        {
                            // linha impar
                            tab1r = (wp.TableRow)rowTab1Impar.Clone();
                        }

                        //seleciona a célula do texto
                        tab1c = tab1r.Elements<wp.TableCell>().ElementAt(0);

                        // introduz o texto da pergunta
                        tab1c.Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Elements<wp.Text>().First().Text = dataComportamentosFracosFortes[count].dados[0];

                        // as cores desta linha (prática)
                        tab1c = tab1r.Elements<wp.TableCell>().ElementAt(2);
                        switch (dataComportamentosFracosFortes[count].dados[1])
                        {
                            case "C":
                                tab1c.Elements<wp.Paragraph>().First().Append((wp.Run)red.Clone());
                                break;
                            case "B":
                                tab1c.Elements<wp.Paragraph>().First().Append((wp.Run)yellow.Clone());
                                break;
                            case "A":
                                tab1c.Elements<wp.Paragraph>().First().Append((wp.Run)green.Clone());
                                break;
                        }

                        // junta às tabelas as linhas
                        tab1Temp.Append(tab1r);
                        count++;
                    }
                }

                // INSERE A TABELA
                par.Append(tab1Temp);
                par.Append((wp.Paragraph)quebraPagina.Clone());

                contaPaginas++;
            }
    }
Beispiel #6
0
    private void insertDetailsSkill(wp.Paragraph par, MainDocumentPart mainPart)
    {
        // na primira fase fazer sem os gráficos

        // ir buscar o template
        // cria o documento final base com base no template
        String templateDoc = _projecto.Template_Mnemonica;

        String filename = templatePath + "\\" + templateDoc + "_SKILS_DETAILS.docx";

        wp.Table tabTitulo1 = null;
        wp.Table tabTitulo2 = null;

        wp.Table tab1 = null;
        wp.TableRow rowTab1Par = null;
        wp.TableRow rowTab1Impar = null;

        wp.Table tab2 = null;
        wp.TableRow rowTab2Par = null;
        wp.TableRow rowTab2Impar = null;

        wp.Run plusSign = null;
        wp.Run minusSign = null;
        wp.Run naSign = null;
        wp.Run eqSign = null;

        wp.Run red = null;
        wp.Run orange = null;
        wp.Run yellow = null;
        wp.Run green = null;
        wp.Paragraph NRs = null;

        wp.Table legendaTab1 = null;
        wp.Table legendaTab2 = null;
        wp.Paragraph quebraPagina = null;

        Boolean inserirChart = true;
        Boolean Pagina1 = true;
        Boolean Pagina2 = true;

        using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(filename, true))
        {
            try
            {
                // primeira tabela do template
                tabTitulo1 = (wp.Table)wordDoc.MainDocumentPart.Document.Body.Elements<wp.Table>().ElementAt(0).Clone();

                // primeira tabela com comportamentos
                tab1 = (wp.Table)wordDoc.MainDocumentPart.Document.Body.Elements<wp.Table>().ElementAt(1).Clone();
                rowTab1Impar = (wp.TableRow)tab1.Elements<wp.TableRow>().ElementAt(1).Clone();
                rowTab1Par = (wp.TableRow)tab1.Elements<wp.TableRow>().ElementAt(2).Clone();
                tab1.Elements<wp.TableRow>().ElementAt(1).Remove();
                tab1.Elements<wp.TableRow>().ElementAt(1).Remove();

                // apanha os run + e - (mais e menos)
                minusSign = (wp.Run)rowTab1Impar.Elements<wp.TableCell>().ElementAt(1).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Clone();
                naSign = (wp.Run)rowTab1Impar.Elements<wp.TableCell>().ElementAt(2).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Clone();
                plusSign = (wp.Run)rowTab1Par.Elements<wp.TableCell>().ElementAt(1).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Clone();
                try
                {
                    eqSign = (wp.Run)rowTab1Par.Elements<wp.TableCell>().ElementAt(2).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Clone();
                }
                catch
                {
                    eqSign = null;
                }
                // eliminar o mais e o menos das linhas e o tracinho
                rowTab1Impar.Elements<wp.TableCell>().ElementAt(1).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Remove();
                rowTab1Impar.Elements<wp.TableCell>().ElementAt(2).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Remove();
                rowTab1Par.Elements<wp.TableCell>().ElementAt(1).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Remove();
                if (eqSign != null) rowTab1Par.Elements<wp.TableCell>().ElementAt(2).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Remove();

                // depois tenho algures aqui a legenda da tabela
                legendaTab1 = (wp.Table)wordDoc.MainDocumentPart.Document.Body.Elements<wp.Table>().ElementAt(2).Clone();

                // o primeiro parágrafpo tem idiciação de ter ou não o chart
                try
                {
                    wp.Text chartPar = (wp.Text)wordDoc.MainDocumentPart.Document.Body.Elements<wp.Paragraph>().FirstOrDefault().Elements<wp.Run>().FirstOrDefault().Elements<wp.Text>().FirstOrDefault();
                    if (chartPar.Text != "")
                        inserirChart = true;
                    else
                        inserirChart = false;

                }
                catch
                {
                    inserirChart = false;
                }
            }
            catch
            {
                Pagina1 = false;
            }

            //parágrafo quebra de página
            quebraPagina = new wp.Paragraph(new wp.Run(new wp.Text("")));
            quebraPagina.ParagraphProperties = new wp.ParagraphProperties();
            quebraPagina.ParagraphProperties.PageBreakBefore = new wp.PageBreakBefore();
            quebraPagina.ParagraphProperties.PageBreakBefore.Val = DocumentFormat.OpenXml.OnOffValue.FromBoolean(true);

            try
            {
                // segundo header, também uma tabela
                //terá de se analisar o seu interior
                tabTitulo2 = (wp.Table)wordDoc.MainDocumentPart.Document.Body.Elements<wp.Table>().ElementAt(3).Clone();

                // tsegunda tabela
                tab2 = (wp.Table)wordDoc.MainDocumentPart.Document.Body.Elements<wp.Table>().ElementAt(4).Clone();
                rowTab2Impar = (wp.TableRow)tab2.Elements<wp.TableRow>().ElementAt(1).Clone();
                rowTab2Par = (wp.TableRow)tab2.Elements<wp.TableRow>().ElementAt(2).Clone();
                tab2.Elements<wp.TableRow>().ElementAt(1).Remove();
                tab2.Elements<wp.TableRow>().ElementAt(1).Remove();

                // agora os paragrafos de cor
                red = (wp.Run)rowTab2Impar.Elements<wp.TableCell>().ElementAt(2).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Clone();
                orange = (wp.Run)rowTab2Impar.Elements<wp.TableCell>().ElementAt(3).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Clone();
                yellow = (wp.Run)rowTab2Par.Elements<wp.TableCell>().ElementAt(2).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Clone();
                green = (wp.Run)rowTab2Par.Elements<wp.TableCell>().ElementAt(3).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Clone();
                NRs = (wp.Paragraph)rowTab2Impar.Elements<wp.TableCell>().ElementAt(5).Elements<wp.Paragraph>().First().Clone();

                // eliminar os quadrados do template
                rowTab2Impar.Elements<wp.TableCell>().ElementAt(2).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Remove();
                rowTab2Impar.Elements<wp.TableCell>().ElementAt(3).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Remove();
                rowTab2Impar.Elements<wp.TableCell>().ElementAt(5).Elements<wp.Paragraph>().First().Remove();

                rowTab2Par.Elements<wp.TableCell>().ElementAt(2).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Remove();
                rowTab2Par.Elements<wp.TableCell>().ElementAt(3).Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Remove();
                rowTab2Par.Elements<wp.TableCell>().ElementAt(5).Elements<wp.Paragraph>().First().Remove();

                // legenda tabela 2
                // depois tenho algures aqui a legenda da tabela
                legendaTab2 = (wp.Table)wordDoc.MainDocumentPart.Document.Body.Elements<wp.Table>().ElementAt(5).Clone();
            }
            catch
            {
                Pagina2 = false;
            }
        }
        // fim da recolha dos elementos template.
        // passamos para a inserção destes elementos para cada competência

        // eliminar o texto do marcador
        par.Elements<wp.Run>().First().Elements<wp.Text>().First().Text = "";

        wp.Paragraph p = null;
        wp.Run r = null;
        wp.Text t = null;
        wp.Table tab = null;
        wp.Table tabTemp = null;
        wp.Table tab1Temp = null;
        wp.Table tab2Temp = null;

        wp.TableRow tab1r = null;
        wp.TableCell tab1c = null;
        wp.TableRow tab2r = null;
        wp.TableCell tab2c = null;

        wp.TableCell tcTemp = null;
        wp.TableRow trTemp = null;
        wp.TableCellProperties tcProps = null;

        int[] critic;
        int count = 0;

        String pergunta;
        //String tempTexto;
        int contaPaginas = 1;
        int ranking;
        String gap;

        foreach (FamiliaCompetencias fam in _Modelo.Familias.Values)
            foreach (Competencia comp in fam.Competencias.Values)
            {
                // são duas páginas de detalhes por cada competência
                // header da 1ªpágina
                if (Pagina1)
                {
                    if (comp.Type == "C") continue; // é uma competência comentário
                    if (comp.Type == "D") continue; // é uma zona de derrailers

                    // Nome da competência
                    tab = (wp.Table)tabTitulo1.Clone();
                    p = tab.Elements<wp.TableRow>().ElementAt(0).Elements<wp.TableCell>().First().Elements<wp.Paragraph>().First();
                    r = p.Elements<wp.Run>().First();
                    t = r.Elements<wp.Text>().First();
                    t.Text = repCalcs.getCompetenciaText(comp.CompetenciaID);

                    // Matriz de criticalidade
                    try
                    {
                        tabTemp = (wp.Table)tab.Elements<wp.TableRow>().First().Elements<wp.TableCell>().ElementAt(1).Elements<wp.Table>().First();

                        // ajustar a cor de cada backgroud destas células
                        critic = repCalcs.getCompetenciaCriticalidadeMatrixPos(comp.CompetenciaID);
                        String porRef = critic[1].ToString() + critic[0].ToString();
                        String pos;
                        for (int x = 0; x < 3; x++)
                        {
                            trTemp = tabTemp.Elements<wp.TableRow>().ElementAt(x);
                            for (int y = 0; y < 3; y++)
                            {
                                pos = x.ToString() + y.ToString();
                                tcTemp = trTemp.Elements<wp.TableCell>().ElementAt(y);
                                tcProps = tcTemp.Elements<wp.TableCellProperties>().First();
                                if (pos != porRef)
                                {
                                    tcProps.Shading.Fill = "C0C0C0";
                                }
                                //if ((critic[0] - x) * 10 + critic[1] - y != 0)

                            }
                        }
                    }
                    catch
                    {

                    }
                    par.Append(tab);

                    // um parágrafo de espaçamento para que as tabelas não colem

                    String spreadsheetTemplate = templatePath + "\\" + templateDoc + "_CHART_BARS.xlsx";
                    // String spreadsheetFileName = rootPath + "generatedFiles\\" + templateDoc + _self.Email + ".xlsx";

                    // GRÁFICO
                    // insert um gráfico com o template definido neste parágrafo 5372100, Cy = 1914525
                    String relID;

                    if (inserirChart)
                    {
                        relID = insertChart(mainPart, par, spreadsheetTemplate, 5372100, 1914525);
                        ChartsToUpdate.Add(relID, repCalcs.DB_getDADOS_GRAFICO(comp.CompetenciaID));
                    }
                }

                // PREPARA AS DUAS TABELAS COM OS DETALHES
                if (Pagina1) tab1Temp = (wp.Table)tab1.Clone();
                if (Pagina2) tab2Temp = (wp.Table)tab2.Clone();

                // os valores obtidos em cada comportamento
                List<ReportRow> dataComportamentos = repCalcs.getAvaliacaoComportamentosCriticos(comp.CompetenciaID);
                List<ReportRow> dataComportamentosFracosFortes = repCalcs.getAvaliacaoComportamentosCriticos_FracosFortes(comp.CompetenciaID);
                count = 0;
                foreach (Pratica prat in comp.Praticas.Values)
                {
                    if (Pagina1)
                    {
                        if (tab1Temp.Elements<wp.TableRow>().First().Elements<wp.TableCell>().First().Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Elements<wp.Text>().First().Text == "#TEXT_PRATICA#")
                        {
                            String plainText;
                            if (Utility.theServer != null)
                                plainText = Regex.Replace(Utility.theServer.HtmlDecode(prat.Description), "<[^>]+?>", "");
                            else
                                plainText = prat.Description;

                            plainText = DBHelper.ParseTextProprio(plainText, new Pessoa(), false);

                            tab1Temp.Elements<wp.TableRow>().First().Elements<wp.TableCell>().First().Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Elements<wp.Text>().First().Text = plainText;

                        }
                    }
                    if (Pagina2)
                    {
                        if (tab2Temp.Elements<wp.TableRow>().First().Elements<wp.TableCell>().First().Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Elements<wp.Text>().First().Text == "#TEXT_PRATICA#")
                        {
                            String plainText;
                            if (Utility.theServer != null)
                                plainText = Regex.Replace(Utility.theServer.HtmlDecode(prat.Description), "<[^>]+?>", "");
                            else
                                plainText = prat.Description;

                            plainText = DBHelper.ParseTextProprio(plainText, new Pessoa(), false);

                            tab2Temp.Elements<wp.TableRow>().First().Elements<wp.TableCell>().First().Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Elements<wp.Text>().First().Text = plainText;

                        }
                    }
                    foreach (Pergunta perg in prat.Perguntas.Values)
                    {
                        pergunta = repCalcs.getPergunta(perg.PerguntaID);
                        if (count % 2 == 0)
                        {
                            // linha par
                            if (Pagina1) tab1r = (wp.TableRow)rowTab1Par.Clone();
                            if (Pagina2) tab2r = (wp.TableRow)rowTab2Par.Clone();

                        }
                        else
                        {
                            // linha impar
                            if (Pagina1) tab1r = (wp.TableRow)rowTab1Impar.Clone();
                            if (Pagina2) tab2r = (wp.TableRow)rowTab2Impar.Clone();
                        }

                        if (Pagina1) tab1c = tab1r.Elements<wp.TableCell>().ElementAt(0);
                        if (Pagina2) tab2c = tab2r.Elements<wp.TableCell>().ElementAt(0);

                        if (count + 1 < dataComportamentos.Count)
                        {
                            // introduz o texto da pergunta
                            if (Pagina1) tab1c.Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Elements<wp.Text>().First().Text = dataComportamentos[count + 1].dados[0];
                            if (Pagina2) tab2c.Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Elements<wp.Text>().First().Text = dataComportamentos[count + 1].dados[0];
                            // introduz os valores de cada prática
                            if (Pagina1)
                            {
                                for (int i = 1; i <= _projecto.GroupsCodes.Count(); i++)
                                {

                                    tab1c = tab1r.Elements<wp.TableCell>().ElementAt(i);
                                    fillPlusMinus(tab1c, dataComportamentos[count + 1].dados[i], plusSign, minusSign, naSign, eqSign);

                                }
                            }

                        }
                        // as cores desta linha (prática)
                        if (Pagina2)
                        {
                            tab2c = tab2r.Elements<wp.TableCell>().ElementAt(2);
                            switch (dataComportamentosFracosFortes[count].dados[1])
                            {
                                case "C":
                                    tab2c.Elements<wp.Paragraph>().First().Append((wp.Run)red.Clone());
                                    break;
                                case "B":
                                    tab2c.Elements<wp.Paragraph>().First().Append((wp.Run)orange.Clone());
                                    break;
                                case "A":
                                    tab2c.Elements<wp.Paragraph>().First().Append((wp.Run)yellow.Clone());
                                    break;
                            }
                        }

                        if (Pagina2)
                        {
                            tab2c = tab2r.Elements<wp.TableCell>().ElementAt(3);
                            if (dataComportamentosFracosFortes[count].dados[2] == "D")
                                tab2c.Elements<wp.Paragraph>().First().Append((wp.Run)green.Clone());

                            // a percentagem de não respostas
                            tab2c = tab2r.Elements<wp.TableCell>().ElementAt(5);
                            NRs.Elements<wp.Run>().First().Elements<wp.Text>().First().Text = dataComportamentosFracosFortes[count].dados[3] + "%";

                            tab2c.Append((wp.Paragraph)NRs.Clone());
                        }
                        // junta às tabelas as linhas
                        if (Pagina1) tab1Temp.Append(tab1r);
                        if (Pagina2) tab2Temp.Append(tab2r);

                        count++;
                    }
                }

                // INSERE A TABELA 1
                if (Pagina1) par.Append(tab1Temp);
                // LEGENDA 1

                //tempTexto = "";

                //legendaTab1.Elements<wp.TableRow>().ElementAt(1).Elements<wp.TableCell>().First().Elements<wp.Paragraph>().First().Elements<wp.Run>().First().Elements<wp.Text>().First().Text = tempTexto;
                if (Pagina1) par.Append((wp.Table)legendaTab1.Clone());

                // QUEBRA DE PÁGINA 1
                if (Pagina1) par.Append((wp.Paragraph)quebraPagina.Clone());

                // HEADER da 2ªpágina
                if (Pagina2)
                {
                    tab = (wp.Table)tabTitulo2.Clone();
                    p = tab.Elements<wp.TableRow>().ElementAt(1).Elements<wp.TableCell>().ElementAt(0).Elements<wp.Paragraph>().First();
                    r = p.Elements<wp.Run>().First();
                    t = r.Elements<wp.Text>().First();
                    t.Text = repCalcs.getCompetenciaText(comp.CompetenciaID);
                    try
                    {
                        // quadro dos RANKINGS ainda no header2
                        p = tab.Elements<wp.TableRow>().ElementAt(0).Elements<wp.TableCell>().ElementAt(2).Elements<wp.Paragraph>().First();
                        r = p.Elements<wp.Run>().First();
                        t = r.Elements<wp.Text>().First();
                        ranking = repCalcs.getCompetenciaRanking(comp.CompetenciaID, "T");
                        t.Text = ranking == 0 ? "-" : ranking.ToString();

                        for (int i = 1; i < _projecto.GroupsCodes.Count(); i++)
                        {
                            p = tab.Elements<wp.TableRow>().ElementAt(0).Elements<wp.TableCell>().ElementAt(i + 2).Elements<wp.Paragraph>().First();
                            r = p.Elements<wp.Run>().First();
                            t = r.Elements<wp.Text>().First();
                            ranking = repCalcs.getCompetenciaRanking(comp.CompetenciaID, repCalcs.ReportGroupCodes[i]);
                            t.Text = ranking == 0 ? "-" : ranking.ToString();
                        }

                        // quadro dos GAPS ainda no header2
                        p = tab.Elements<wp.TableRow>().ElementAt(1).Elements<wp.TableCell>().ElementAt(2).Elements<wp.Paragraph>().First();
                        r = p.Elements<wp.Run>().First();
                        gap = repCalcs.getCompetenciaGAP(comp.CompetenciaID, "T");
                        r.Elements<wp.SymbolChar>().First().Char = gap;

                        for (int i = 1; i < _projecto.GroupsCodes.Count(); i++)
                        {
                            p = tab.Elements<wp.TableRow>().ElementAt(1).Elements<wp.TableCell>().ElementAt(i + 2).Elements<wp.Paragraph>().First();
                            r = p.Elements<wp.Run>().First();
                            gap = repCalcs.getCompetenciaGAP(comp.CompetenciaID, repCalcs.ReportGroupCodes[i]);
                            r.Elements<wp.SymbolChar>().First().Char = gap;
                        }

                    }
                    catch
                    {

                    }

                    // INSERTE A TABELA DETALHE PAGINA 2
                    par.Append(tab);

                    // um parágrafo de espaçamento para que as tabelas não colem
                    par.Append(new wp.Paragraph());

                    // INSERE A TABELA 2
                    par.Append(tab2Temp);

                    // LEGENDA 2
                    //tempTexto = "";
                    par.Append((wp.Table)legendaTab2.Clone());

                    // QUEBRA DE PÁGINA 2
                    if (contaPaginas < repCalcs.getQuantidadeDeCompetencias())
                        par.Append((wp.Paragraph)quebraPagina.Clone());
                }
                contaPaginas++;
            }
    }