Mi chiedevo quale sarebbe l'approccio migliore per la creazione di tabelle in un pdf con angoli arrotondati utilizzando la libreria iTextSharp 5.x +.Tabelle con angoli arrotondati
risposta
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 :)
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.
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.
Che ha funzionato alla grande. Sai come ho potuto trovare il posizionamento del tavolo in modo da poter disegnare con precisione gli angoli arrotondati. –
La risposta più semplice possibile – hdoghmen
- 1. UIToolbar con angoli arrotondati
- 2. FrameLayout con angoli arrotondati
- 3. Disegno quadrato con angoli arrotondati
- 4. Fluid angoli arrotondati con jQuery
- 5. Angoli arrotondati su UITableView
- 6. Angoli arrotondati di ToolStrip
- 7. NSImageView angoli arrotondati + corsa
- 8. Angoli arrotondati di MKMapView
- 9. angoli arrotondati ritaglio
- 10. CSS angoli arrotondati div
- 11. Angoli arrotondati su UIView
- 12. angoli arrotondati su immagini
- 13. CSS div angoli arrotondati
- 14. NSButton con larghezza variabile, angoli arrotondati
- 15. Bordo con angoli arrotondati e trasparenza
- 16. angoli arrotondati con colore del bordo
- 17. Come creare un div con angoli arrotondati
- 18. Objective C - UINavigationBar con angoli arrotondati
- 19. Angoli interni arrotondati con cornice interna trasparente
- 20. Angoli arrotondati su un fieldset
- 21. Angoli arrotondati CSS in IE8
- 22. imbottitura CardView e angoli arrotondati
- 23. UITableViewCell: angoli arrotondati e ombreggiatura
- 24. Angoli arrotondati UIView o UIImageView
- 25. Imagebutton immagine intera, angoli arrotondati
- 26. Angoli arrotondati solo su alcuni angoli in css
- 27. ImageView con angoli arrotondati dopo il ridimensionamento con ScaleType.CenterCrop
- 28. Angoli più arrotondati con CSS3 o con immagini?
- 29. UITableViewCell angoli arrotondati e sottocartelle clip
- 30. Impostare gli angoli arrotondati per svg: image
Questo è ottimo per le singole celle ma per quanto riguarda l'intera tabella? Come potrebbe implementare gli eventi TableCell per ottenere questo risultato. –
L'aggiunta di un'altra tabella con una cella che contiene la mia tabella di destinazione funziona. Grazie ancora per la tua risposta MK. –
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). –