मैं सोच रहा था कि iTextSharp 5.x + लाइब्रेरी का उपयोग करके गोलाकार कोनों में एक पीडीएफ में टेबल बनाने के लिए सबसे अच्छा तरीका क्या होगा।गोलाकार कोनों के साथ टेबल्स
उत्तर
आप iTextSharp स्रोत कोड है, PdfContentByte
वर्ग के लिए निम्नलिखित जोड़ें:
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
}
तो फिर तुम दौर क्या कभी कोनों आप का उत्पादन करना चाहते हैं:
/// <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);
}
अगला चरण implment को IPdfPCellEvent
इंटरफेस है पसंद की गोलाकार तालिका:
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);
अगर आपको जवाब पसंद है ... इसे वोट दें :)
मुझे लगता है कि यदि आप गोल कोनों के साथ एक टेबल चाहते हैं, तो आप अपनी मेज पर कुछ चमकदार (या कम से कम सुंदर) स्वरूपण की तलाश में हैं। यदि ऐसा है, तो मैं आपके iTextSharp तालिका में सभी सीमाओं को बंद करने और आपके पृष्ठ पर पृष्ठभूमि के रूप में ग्राफ़िक छवि का उपयोग करने की अनुशंसा करता हूं। आप ग्राफिक पृष्ठभूमि को जटिल और फैंसी के रूप में बना सकते हैं जैसे आप चाहते हैं (छायांकन, ग्रेडियेंट, गोलाकार कोनों, आदि) बहुत कम परेशानी के साथ। फिर, बस अपनी छवि को पृष्ठभूमि छवि के साथ लाइन करें।
यदि आपका डेटा प्रारूप अक्सर बदल जाएगा, तो आप इस दृष्टिकोण का उपयोग नहीं करना चाहेंगे।
गोलाकार कोनों के साथ iTextSharp PdfPTable
बनाने का कोई तरीका नहीं है लेकिन आप क्या कर सकते हैं PdfContentByte.RoundRectangle()
विधि का उपयोग करके गोलाकार कोनों के साथ तालिका सीमा खींचें।
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();
}
यह टुकड़ा मैं www.mikesdotnetting.com पर लेख iTextSharp - Drawing shapes and Graphics में पाया कोड का एक संक्षिप्त संस्करण है:
यहाँ एक टुकड़ा है कि इस दर्शाता है। यदि आप पहले से नहीं हैं तो आप अपने अन्य लेखों पर नज़र डालना चाहेंगे।
यह बहुत अच्छा काम करता है। क्या आप जानते हैं कि मुझे टेबल की स्थिति कैसे मिल सकती है ताकि मैं गोलाकार कोनों को सटीक रूप से आकर्षित कर सकूं। –
सरल संभव उत्तर – hdoghmen
यह व्यक्तिगत कोशिकाओं के लिए अच्छा है लेकिन पूरी तालिका के बारे में क्या है? इसे पूरा करने के लिए टेबलसेल घटनाक्रम को कैसे कार्यान्वित किया जा सकता है। –
एक सेल के साथ एक और तालिका जोड़ना जिसमें मेरी लक्ष्य तालिका कार्य शामिल है। आपके उत्तर एमके के लिए फिर से धन्यवाद। –
मुझे खुशी है कि आपको जवाब मिल गया है, एक और दृष्टिकोण प्रत्येक टेबल कोने सेल (टॉपलेफ्ट, टॉपराइट, बोटम लाइफ और बोटम राइट) के एक कोने को गोल करके होगा। –