1

I have a report which prints tables, the processes to generate the table are the same.. by each repetition prints one table... Then, how can I know the available space of my current page? I want to know if in my free space fits two rows or if not how to skip to the next page?

public void Imprime()
    {   
        Console.WriteLine("Generando documento...");



        document.Open();
        Alto = Convert.ToInt32(document.PageSize.Height);
        //PARRAFO
        Paragraph unParrafo = GenerarParrafo();
        document.Add(unParrafo);

        //GENERO Y AGREGO SALTO DE LINEA
        document.Add(new Paragraph(" "));

        //IMAGEN
        //iTextSharp.text.Image unaImagen = GenerarImagen();
        //document.Add(unaImagen);
            //GENERO Y AGREGO SALTO DE LINEA
            document.Add(new Paragraph(" "));

        //TABLA
        //PdfPTable unaTabla = Muestra_Tabla();
        //PdfPTable unaTabla = Arma_Tabla(document);
        //document.Add(unaTabla);
        Arma_Tabla();

        //CIERRO DOCUMENTO
        document.Close();

        //LO EJECUTO
        Process prc = new System.Diagnostics.Process();
        prc.StartInfo.FileName = fileName;
        prc.Start();


    }

public static void Arma_Tabla()
{
    //DataTable Dt_Notas = new DataTable();
    Dt_Notas = Nota_Medica();

    DataRow[] Dr_Separador = null;
    DataRow[] Dr_Resultado = null;
    Dr_Separador = Dt_Notas.Select("Es_Separador = 'SI'");
    Console.WriteLine("Detecta: " + Dr_Separador.Count().ToString() + " separadores.");

    foreach (DataRow Dr in Dr_Separador)
    {
        Dr_Resultado = Dt_Notas.Select("Separador = '" + Dr["Separador"].ToString() + "'");

        Console.Write("La sección: " + Dr["Separador"].ToString() + " tiene " + Dr_Resultado.Count().ToString() + " apartados.");

        Crea_Tabla(Dr_Resultado);
    }

    //return unaTabla;
}
                                                                                                                                                                                                                                                                                 public static void Crea_Tabla(DataRow[] Dr_Por_Separador)
{
    int Renglones = 0;
    int Columnas = 0;
    int Num_Col = 0;
    int Renglones_Totales = 0;
    PdfPTable pdf_Tabla = null;
    PdfPTable pdf_Tabla_Respuestas = null;
    Console.WriteLine("Crear tabla de: " + Dr_Por_Separador[0]["Pregunta"].ToString());
    Renglones_Totales = Dr_Por_Separador.Count();
    DataTable Dt_Tabla = new DataTable();
    Dt_Tabla = Dt_Notas.Clone();
    DataRow[] Dr_Respuestas = null;
    bool Inicia = true;


    foreach (DataRow Dr in Dr_Por_Separador)
    {
        Renglones = Renglones + 1; //Se van contando los renglones para saber cuando termino de leer antes de salir
        if(Inicia == true) //Se inicializa la tabla
        {
            Num_Col = Convert.ToInt32(Dr["Columnas"].ToString());
            pdf_Tabla = new PdfPTable(Num_Col);
            pdf_Tabla_Respuestas = new PdfPTable(Num_Col);
            Inicia = false;
        }


        if (Renglones == 1) //Pinta Separador 
        {
            pdf_Tabla.AddCell(Crea_Celdas(Dr, "Pregunta"));
            pdf_Tabla.SplitLate = false;
        }
        else //Pinta Apartados; Preguntas y respuestas
        {
            Columnas = Columnas + 1;
            pdf_Tabla.AddCell(Crea_Celdas(Dr, "Pregunta"));
            Dt_Tabla.ImportRow(Dr);
        }

        //Si ya termino de leer los renglones
        if (Renglones_Totales == Renglones)
        {
            pdf_Tabla.CompleteRow(); //Completa el ultimo renglon que corresponde a preguntas
            Columnas = Num_Col; //Para que termine de pintar las respuestas
        }

        if (Columnas == Num_Col) //Se ya termino el renglon de preguntas
        {
            Dr_Respuestas = Dt_Tabla.Select();
            foreach (DataRow r in Dr_Respuestas)
            {
                pdf_Tabla.AddCell(Crea_Celdas(r, "Respuesta"));
            }

            pdf_Tabla.CompleteRow();
            Dt_Tabla.Clear();
            Columnas = 0;
            if (Alto <= Convert.ToInt32(pdf_Tabla.TotalHeight))
            {
                document.NewPage();
                Alto = Convert.ToInt32(document.PageSize.Height);
            }
            document.Add(pdf_Tabla);

            MessageBox.Show("Renglon respuestas --> Alto " + Alto.ToString()
                            + " Alto tabla: " + pdf_Tabla.TotalHeight.ToString()

                            );


            Alto = Alto - Convert.ToInt32(pdf_Tabla.TotalHeight);
            pdf_Tabla = null;
            Inicia = true;
        }


    }
}

                                                                                           public static PdfPCell   Crea_Celdas(DataRow Dr, String Tipo)
{
    iTextSharp.text.Font Letra = null;
    BaseColor Fondo = null;
    int Colapsable = 0;
    if (Tipo == "Pregunta")
    {
        if (Dr["Es_Separador"].ToString() == "SI")
        {
            Letra = new iTextSharp.text.Font(iTextSharp.text.Font.NORMAL, 8f, iTextSharp.text.Font.NORMAL, BaseColor.WHITE);
            Fondo = BaseColor.GRAY;
            Colapsable = Convert.ToInt32(Dr["Columnas"].ToString());
        }
        else
        {
            Letra = new iTextSharp.text.Font(iTextSharp.text.Font.NORMAL, 9f, iTextSharp.text.Font.BOLD, BaseColor.BLUE);
            Fondo = BaseColor.LIGHT_GRAY;
            Colapsable = 1;
        }
    }
    else
    {
        if (Dr["Es_Separador"].ToString() == "NO")
        {
            Letra = new iTextSharp.text.Font(iTextSharp.text.Font.NORMAL, 8f, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
            Fondo = BaseColor.WHITE;
            Colapsable = 1;
        }

    }
    Console.WriteLine("Pinta " + Dr[Tipo == "Pregunta" ? "Pregunta" : "Respuesta"].ToString());
    PdfPCell pdf_Celda = new PdfPCell(new Phrase(Dr[Tipo == "Pregunta" ? "Pregunta" : "Respuesta"].ToString(), Letra));
    pdf_Celda.BackgroundColor = Fondo;
    pdf_Celda.Colspan = Colapsable;

    MessageBox.Show(pdf_Celda.GetMaxHeight().ToString());

    return pdf_Celda;
}

                                                                                                    public static DataTable Nota_Medica()
{
    DataTable Dt = new DataTable();
    Dt.Columns.Add("Separador", typeof(string));
    Dt.Columns.Add("Es_Separador", typeof(string));
    Dt.Columns.Add("Columnas", typeof(string));
    Dt.Columns.Add("Pregunta", typeof(string));
    Dt.Columns.Add("Respuesta", typeof(string));
    DataRow Dr = Dt.NewRow();

    for (int Separador = 0; Separador < 1; Separador++)
    {
        for (int i = 0; i < 1; i++)
        {
            Dr = Dt.NewRow();
            Dr["Separador"] = "Separador Num." + Separador.ToString();
            Dr["Es_Separador"] = (i == 0 ? "SI" : "NO");
            Dr["Columnas"] = (Separador + 1).ToString();
            Dr["Pregunta"] = (i == 0 ? "Separador Num." + (Separador + 1).ToString() : "Pregunta Num." + i.ToString());
            Dr["Respuesta"] = "Respuesta Num." + i.ToString() + (i == 5 ? " El estado de salud del paciente es delicado y de observancia" : "");
            Dt.Rows.Add(Dr);
        }
    }

    Console.WriteLine("Carga: " + Dt.Rows.Count.ToString() + " notas.");
    return Dt;
}
Loorenax
  • 23
  • 5
  • 3
    Would you add some code so we can see what you've tried? – Dehli Sep 10 '14 at 16:53
  • 1
    You should be able to use a combination of [getting the current Y position](http://stackoverflow.com/a/7689173/231316) and [finding the height of a table](http://stackoverflow.com/a/7601244/231316) – Chris Haas Sep 10 '14 at 20:37
  • ok. Sorry. My example is about a Test, each test has some sections and each section has a some questions and answers. Each section has indicated a number of columns has to show it. 2, 3 X columns. First print name section, next name questions in full row, next print text of answer in full row. Next again questions, next answers. – Loorenax Sep 10 '14 at 23:14
  • I'm not sure if your comment indicates that you were able to solve your problem using the feedback from Chris or if you still have to solve the problem using the feedback from Chris. If the latter, please explain what you've tried and what doesn't work for you in a *short* example. Your code is way too long to understand what you're asking. – Bruno Lowagie Sep 11 '14 at 07:26

0 Answers0