2010-03-31 16 views

risposta

13

Se hai iTextSharp codice sorgente, aggiungere quanto segue PdfContentByte classe:

/// <summary> 
     /// Enumuration for defining corners you want rounded. 
     /// </summary> 
     [Flags()] 
     public enum Corners {None = 0,All=15,Top=3,Bottom=12, TopLeft = 1, TopRight=2, BottomLeft=4, BottomRight=8}; 

     /// <summary> 
     /// Adds a round rectangle to the current path, with rounded conrners as specified by roundCorners. 
     /// </summary> 
     /// <param name="x"></param> 
     /// <param name="y"></param> 
     /// <param name="w"></param> 
     /// <param name="h"></param> 
     /// <param name="r"></param> 
     /// <param name="roundCorners"></param> 
     public void RoundRectangle(float x, float y, float w, float h, float r,Corners roundCorners) 
     { 
      if (w < 0) 
      { 
       x += w; 
       w = -w; 
      } 
      if (h < 0) 
      { 
       y += h; 
       h = -h; 
      } 
      if (r < 0) 
       r = -r; 
      float b = 0.4477f; 
      if((roundCorners & Corners.BottomLeft) == Corners.BottomLeft) 
       MoveTo(x + r, y); 
      else 
       MoveTo(x, y); 

      if ((roundCorners & Corners.BottomRight) == Corners.BottomRight) 
      { 
       LineTo(x + w - r, y); 
       CurveTo(x + w - r * b, y, x + w, y + r * b, x + w, y + r); 
      } 
      else 
       LineTo(x + w, y); 

      if ((roundCorners & Corners.TopRight) == Corners.TopRight) 
      { 
       LineTo(x + w, y + h - r); 
       CurveTo(x + w, y + h - r * b, x + w - r * b, y + h, x + w - r, y + h); 
      } 
      else 
       LineTo(x + w, y + h); 

      if ((roundCorners & Corners.TopLeft) == Corners.TopLeft) 
      { 
       LineTo(x + r, y + h); 
       CurveTo(x + r * b, y + h, x, y + h - r * b, x, y + h - r); 
      } 
      else 
       LineTo(x , y + h); 

      if ((roundCorners & Corners.BottomLeft) == Corners.BottomLeft) 
      { 
       LineTo(x, y + r); 
       CurveTo(x, y + r * b, x + r * b, y, x + r, y); 
      }else 
       LineTo(x, y); 
     }   

passo successivo è quello di implment IPdfPCellEvent interfaccia:

public class myCellEvent : IPdfPCellEvent 
    { 
     #region members 
     PdfContentByte.Corners _roundedCorners; 
     float _roundness; 
     #endregion 

     #region ctor 
     public myCellEvent() 
      :this(PdfContentByte.Corners.All,2f) 
     { 

     } 

     public myCellEvent(PdfContentByte.Corners roundedCorners) 
      : this(roundedCorners, 2f) 
     { 

     } 

     public myCellEvent(PdfContentByte.Corners roundedCorners,float roundness) 
     { 
      _roundedCorners = roundedCorners; 
      _roundness = roundness; 

     } 
     #endregion 

     #region IPdfPCellEvent Members 
     public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) 
     { 
      PdfContentByte cb = canvases[PdfPTable.LINECANVAS]; 
      cb.RoundRectangle(position.Left, position.Bottom, position.Right - position.Left, position.Top - position.Bottom, this._roundness, this._roundedCorners); 
      cb.SetColorStroke(new BaseColor(System.Drawing.Color.Black)); 
      cb.SetColorFill(new BaseColor(System.Drawing.Color.Beige));    
      cb.FillStroke(); 
      cb.Stroke(); 
     } 
     #endregion 
    } 

Poi si completano che cosa mai gli angoli si vuole produrre la tavola rotonda scelta:

PdfPTable table = new PdfPTable(1); 


       PdfPCell cell = new PdfPCell(new Phrase(10, atext, f2)); 
       cell.Border = 0; 
       cell.Padding = 5f; 
       cell.CellEvent = new myCellEvent(PdfContentByte.Corners.Top,2f); 
       table.AddCell(cell); 

Se ti piace la risposta ... dai un voto :)

+0

Questo è ottimo per le singole celle ma per quanto riguarda l'intera tabella? Come potrebbe implementare gli eventi TableCell per ottenere questo risultato. –

+1

L'aggiunta di un'altra tabella con una cella che contiene la mia tabella di destinazione funziona. Grazie ancora per la tua risposta MK. –

+1

Sono contento di aver trovato la risposta, un altro approccio sarebbe arrotondando un angolo di ogni cella dell'angolo del tavolo (TopLeft, TopRight, BottomLeft e BottomRight). –

2

Suppongo che se desideri un tavolo con angoli arrotondati, stai cercando una formattazione appariscente (o almeno carina) sul tuo tavolo. In questo caso, ti consiglio di disattivare tutti i bordi nella tabella iTextSharp e di utilizzare un'immagine grafica come sfondo della pagina. È quindi possibile rendere lo sfondo grafico intricato e di fantasia come si desidera (ombreggiatura, sfumature, angoli arrotondati, ecc.) Con pochissimi problemi. Quindi, allinea il tuo tavolo con l'immagine di sfondo.

Se il formato dei dati cambierà spesso, non vorrai utilizzare questo approccio.

5

Non è possibile creare un iTextSharp PdfPTable con gli angoli arrotondati, ma ciò che si potrebbe fare è disegnare un bordo del tavolo con gli angoli arrotondati usando il metodo PdfContentByte.RoundRectangle().

Ecco un frammento che illustra questo:

Document doc = new Document(); 
using (FileStream fs = new FileStream("RoundRectangle Demo.pdf", FileMode.Create)) { 
PdfWriter writer = PdfWriter.GetInstance(doc, fs); 
    doc.Open(); 
    PdfContentByte cb = writer.DirectContent; 

    // Bottom left coordinates x & y, followed by width, height and radius of corners. 
    cb.RoundRectangle(100f, 500f, 200f, 200f, 2f); 
    cb.Stroke(); 

    doc.Close(); 
} 

Questo frammento è una versione condensata di codice che ho trovato in questo articolo iTextSharp - Drawing shapes and Graphics su www.mikesdotnetting.com. Dovresti dare un'occhiata da vicino ai suoi altri articoli se non l'hai già fatto.

+1

Che ha funzionato alla grande. Sai come ho potuto trovare il posizionamento del tavolo in modo da poter disegnare con precisione gli angoli arrotondati. –

+0

La risposta più semplice possibile – hdoghmen