The code does not draw a horizontal line in my PDF file

I am trying to add a horizontal line at the top to split the header text into the actual values ​​in my pdf file: enter image description here

Here is my code:

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); } } } 

I do not understand why cb.Stroke () does not work. Any suggestions?

+6
c # itextsharp
source share
1 answer

Drawing with the iTextSharp PdfContentByte class can be a bit confusing. The height is actually relative to the bottom, not the top. So the top of the page is not 0f, but actually document.Top , which is 726f in size of your PageSize.LETTER page. Therefore, if you want to draw your line 30 units on top, try:

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

Just curious - why is it difficult and not using table borders? (I noticed that you set your borders to 0). Trying to draw lines by hand is the biggest pain. If you ever move content in your PDF, you will need to make sure your measurements are still working.

Try the following:

 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; 

This should give you a nice solid black line under the heading, without any measurements:

Table border sample

+12
source share

All Articles