2011-02-04 23 views
6

Estoy intentando agregar una línea horizontal en la parte superior para dividir el texto de cabecera de los valores reales en mi archivo pdf: enter image description hereCódigo no está dibujando una línea horizontal en mi PDF

Aquí está mi código:

public class StudentList 
{ 
    public void PrintStudentList(int gradeParaleloID) 
    { 
     StudentRepository repo = new StudentRepository(); 
     var students = repo.FindAllStudents() 
          .Where(s => s.IDGradeParalelo == gradeParaleloID); 

     try 
     { 
      Document document = new Document(PageSize.LETTER); 

      PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\Alumnos.pdf", FileMode.Create)); 
      document.Open(); 

      PdfContentByte cb = writer.DirectContent; 
      cb.SetLineWidth(2.0f); // Make a bit thicker than 1.0 default 
      cb.SetGrayStroke(0.95f); // 1 = black, 0 = white 
      cb.MoveTo(20, 30); 
      cb.LineTo(400, 30); 
      cb.Stroke(); 

      PdfPTable table = new PdfPTable(3);     
      float[] widths = new float[] { 0.6f, 0.75f, 2f }; 
      table.SetWidths(widths); 
      PdfPCell numeroCell = new PdfPCell(new Phrase("Nro.")); 
      numeroCell.Border = 0; 
      numeroCell.HorizontalAlignment = 0; 
      table.AddCell(numeroCell); 

      PdfPCell codigoCell = new PdfPCell(new Phrase("RUDE")); 
      codigoCell.Border = 0; 
      codigoCell.HorizontalAlignment = 0; 
      table.AddCell(codigoCell); 

      PdfPCell nombreCell = new PdfPCell(new Phrase("Apellidos y Nombres")); 
      nombreCell.Border = 0; 
      nombreCell.HorizontalAlignment = 0; 
      table.AddCell(nombreCell); 

      int c = 1; 
      foreach (var student in students) 
      { 
       PdfPCell cell = new PdfPCell(new Phrase(c.ToString())); 
       cell.Border = 0; 
       cell.HorizontalAlignment = 0; 
       table.AddCell(cell); 

       cell = new PdfPCell(new Phrase(student.Rude.ToString())); 
       cell.Border = 0; 
       cell.HorizontalAlignment = 0; 
       table.AddCell(cell); 

       cell = new PdfPCell(new Phrase(student.LastNameFather + " " + student.LastNameMother + " " + student.Name)); 
       cell.Border = 0; 
       cell.HorizontalAlignment = 0; 
       table.AddCell(cell); 
       c++; 
      } 
      table.SpacingBefore = 20f; 
      table.SpacingAfter = 30f; 

      document.Add(table); 
      document.Close(); 
     } 
     catch (DocumentException de) 
     { 
      Debug.WriteLine(de.Message); 
     } 
     catch (IOException ioe) 
     { 
      Debug.WriteLine(ioe.Message); 
     } 

    } 
} 

No entiendo por qué el cb.Stroke() no está funcionando. ¿Alguna sugerencia?

+0

FYI: SetGrayStroke parece ser en realidad 0 = negro , 1 = blanco. –

Respuesta

12

Dibujar con la clase PdfContentByte de iTextSharp puede ser un poco confuso. La altura es en realidad relativa a la parte inferior, no a la parte superior. Por lo tanto, la parte superior de la página no es 0f, sino que en realidad es document.Top, que en su tamaño de página de PageSize.LETTER es 726f. Así que si quieres llamar su línea de 30 unidades desde la parte superior, intente:

cb.MoveTo(20, document.Top - 30f); 
cb.LineTo(400, document.Top - 30f); 

Sólo por curiosidad - ¿por qué hacerlo de la manera más dura en lugar de utilizar bordes de la tabla? (Noté que estableces tus fronteras en 0) Intentar espaciar las líneas a mano es el mayor dolor. Si alguna vez mueve el contenido de su PDF, tendrá que asegurarse de que las mediciones sigan funcionando.

Prueba esto:

numeroCell.Border = 0; 
numeroCell.BorderColorBottom = new BaseColor(System.Drawing.Color.Black); 
numeroCell.BorderWidthBottom = 1f; 

codigoCell.Border = 0; 
codigoCell.BorderColorBottom = new BaseColor(System.Drawing.Color.Black); 
codigoCell.BorderWidthBottom = 1f; 

nombreCell.Border = 0; 
nombreCell.BorderColorBottom = new BaseColor(System.Drawing.Color.Black); 
nombreCell.BorderWidthBottom = 1f; 

Eso debería darle una línea de color negro sólido agradable debajo de la fila de cabecera, no hay mediciones necesarias:

Table border sample

+0

¡Eso es fantástico! ¡Gracias! –

Cuestiones relacionadas