2011-02-04 11 views
6

मैं अपने पीडीएफ फाइल में वास्तविक मान से शीर्ष पाठ विभाजित करने के लिए शीर्ष पर एक क्षैतिज रेखा में जोड़ने के लिए कोशिश कर रहा हूँ: enter image description hereकोड मेरी पीडीएफ में एक क्षैतिज रेखा खींचने नहीं है

यहाँ मेरी कोड है:

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

    } 
} 

मुझे समझ में नहीं आता कि cb.Stroke() क्यों काम नहीं कर रहा है। कोई सुझाव?

+0

FYI करें: SetGrayStroke वास्तव में 0 = काला हो रहा है , 1 = सफेद। –

उत्तर

12

iTextSharp के PdfContentByte कक्षा के साथ चित्रण थोड़ा उलझन में हो सकता है। ऊंचाई वास्तव में नीचे के नजदीक है, शीर्ष पर नहीं। तो पृष्ठ का शीर्ष 0 एफ नहीं है, लेकिन इसके बजाय वास्तव में document.Top है, जो आपके पृष्ठ आकार PageSize.LETTER 726f है। तो अगर आप ऊपर से 30 इकाइयों अपनी लाइन आकर्षित करने के लिए चाहते हैं, की कोशिश:

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

बस उत्सुक - कारण है कि यह मुश्किल तरीके के बजाय कर तालिका बॉर्डर का उपयोग करने का? (मैंने देखा है कि आप अपनी सीमाओं को 0 पर सेट करते हैं) हाथ से लाइनों को स्थानांतरित करने का प्रयास करना सबसे बड़ा दर्द है। यदि आप कभी भी अपने पीडीएफ में सामग्री को स्थानांतरित करते हैं, तो आपको यह सुनिश्चित करना होगा कि आपके माप अभी भी काम करते हैं।

इस प्रयास करें:

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; 

कि आप अपने शीर्ष लेख पंक्ति के तहत एक अच्छा ठोस काली रेखा देना चाहिए, कोई माप की जरूरत:

Table border sample

+0

यह शानदार है! धन्यवाद! –

संबंधित मुद्दे