2010-11-04 28 views
23

Estoy tratando de encontrar la forma de que mi texto dentro de un PdfPCell se muestre en el medio. He intentado muchas opciones diferentes, como:iTextsharp, PdfPCell.VerticalAlignment y PdfPCell.HorizontalAlignment

 
myCell.VerticalAlignment = Element.ALIGN_MIDDLE; 
myCell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; 
myCell.VerticalAlignment = Rectangle.ALIGN_MIDDLE; 

Nada de eso me sirve. VerticalAlignment toma un int, por lo que trató de hacer un lazo, para ver si podía encontrar el número correcto, pero todo se quede Alinear abajo ..

Document myDocument = new Document(PageSize.A4); 

    PdfWriter myPdfWriter = PdfWriter.GetInstance(myDocument, new FileStream(strPDFFilePath, FileMode.Create)); 
    myDocument.Open(); 

    myDocument.NewPage(); 

    PdfContentByte myPdfContentByte = myPdfWriter.DirectContent; 

    PdfPCell myCell; 
    Paragraph myParagraph; 

    PdfPTable myTable = new PdfPTable(4); 
    myTable.WidthPercentage = 100; 
    myTable.SetWidths(new int[4] { 25, 25, 25, 25 }); 

    myTable.DefaultCell.BorderWidth = 1; 
    myTable.DefaultCell.BorderColor = BaseColor.RED;     

    for (int i = -100; i < 100; i++) 
    { 
     myParagraph = new Paragraph(String.Format("Alignment: {0}", i)); 
     myParagraph.Font.SetFamily("Verdana"); 
     myParagraph.Font.SetColor(72, 72, 72); 
     myParagraph.Font.Size = 11; 

     myCell = new PdfPCell(); 
     myCell.AddElement(myParagraph); 
     myCell.HorizontalAlignment = i; 
     myCell.VerticalAlignment = i;      
     myTable.AddCell(myCell); 
    } 

    myDocument.Add(myTable); 
    myDocument.Add(new Chunk(String.Empty)); 
    myDocument.Close(); 

Respuesta

32

creo que el problema básico que está teniendo es que está agregando texto a los objetos iTextSharp Paragraph y luego intenta establecer la alineación de este texto utilizando el objeto PdfPCell que lo contiene. No estoy seguro si la propiedad PdfPCell.VerticalAlignment es solo para un texto PdfPCell, o si alinear el objeto Paragraph dentro del PdfPCell no tiene ningún efecto que pueda ver en su prueba.

También está configurando myCell.HorizontalAlignment y myCell.VerticalAlignment en el valor del índice en su lazo for. Creo que quisiste usar 1 instread de i.

De todos modos, la configuración de las propiedades HorizontalAlignment y VerticalAlignment de PdfPCell sí funciona bien. A continuación se muestra un pequeño método que demuestra esto. Lo escribí muy libremente en base a lo que estabas tratando de hacer; si está lo suficientemente cerca de lo que estás tratando de hacer, tal vez puedas usar esto como punto de partida en tu proyecto.

private void TestTableCreation() { 
    using (FileStream fs = new FileStream("TableTest.pdf", FileMode.Create)) { 
     Document doc = new Document(PageSize.A4); 
     PdfWriter.GetInstance(doc, fs); 
     doc.Open(); 

     PdfPTable table = new PdfPTable(4); 

     for (int i = -100; i < 100; i++) { 
      PdfPCell cell = new PdfPCell(new Phrase(String.Format("Alignment: {0}", i))); 
      // Give our rows some height so we can see test vertical alignment. 
      cell.FixedHeight = 30.0f; 

      // ** Try it ** 
      //cell.HorizontalAlignment = Element.ALIGN_LEFT; 
      //cell.HorizontalAlignment = Element.ALIGN_CENTER; 
      cell.HorizontalAlignment = Element.ALIGN_RIGHT; 

      cell.VerticalAlignment = Element.ALIGN_TOP; 
      //cell.VerticalAlignment = Element.ALIGN_MIDDLE; 
      //cell.VerticalAlignment = Element.ALIGN_BOTTOM; 

      table.AddCell(cell); 
     } 

     doc.Add(table); 
     doc.Close(); 
    } 
} 
+2

Gracias, funcionó mejor cuando empecé a usar la frase en lugar de Párrafo – Jimmy

0

Utilice código dado, espero ser de gran ayuda para aquellos que quieran imprimir un texto en una celda de alineación media y superior protegida void Page_Load (object sender, EventArgs e) { gettable(); }

void gettable() 
    { 
     using (FileStream fs = new FileStream(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Vedic-Chart-Life-Report.pdf"), FileMode.Create, FileAccess.Write, FileShare.Read)) 
     { 

      Document doc = new Document(PageSize.LETTER); 
      PdfWriter writer = PdfWriter.GetInstance(doc, fs); 
      doc.Open(); 
      doc.NewPage(); 

      BaseFont bf = BaseFont.CreateFont("C:/WINDOWS/Fonts/krdv010.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 
      Font f = new Font(bf, 16, Font.BOLD); 

      PdfContentByte cb = writer.DirectContent; 

      cb.MoveTo(0, doc.PageSize.Height - 20); 
      cb.LineTo(doc.PageSize.Width, doc.PageSize.Height - 20); 
      cb.Stroke(); 
      cb.ClosePathStroke(); 

      PdfPTable table = new PdfPTable(1); 
      PdfPCell cell = new PdfPCell(new Phrase("eaxynks'k foospu", f)); 

      // Give our rows some height so we can see test vertical alignment. 
      cell.FixedHeight = 15f; 
      cell.HorizontalAlignment = 1; 
      cell.VerticalAlignment = Element.ALIGN_TOP; 

      table.AddCell(cell); 
      doc.Add(table); 

      //cb.RoundRectangle(10f, 550f, 592f, 200f, 20f); 
      //cb.Stroke(); 

      //doc.Add(new Phrase("eaxynks'k foospu", f)); 

      doc.Close(); 
     } 

    } 
solución