//Creates a new PDF document.
PdfDocument doc = new PdfDocument();
//Adds a page.
PdfPage page = doc.Pages.Add();
//Declares a PdfLightTable.
PdfLightTable pdfLightTable = new PdfLightTable();
//Sets the DataSourceType as Direct.
pdfLightTable.DataSourceType = PdfLightTableDataSourceType.TableDirect;
//Creates columns.
pdfLightTable.Columns.Add(new PdfColumn("Company"));
pdfLightTable.Columns.Add(new PdfColumn("Contact"));
pdfLightTable.Columns.Add(new PdfColumn("Country"));
//Adds rows.
pdfLightTable.Rows.Add(new object[] { "111", "Maxim", "III" });
pdfLightTable.Rows.Add(new object[] { "222", "Bell", "III" });
pdfLightTable.Rows.Add(new object[] { "333", "cell", "III" });
pdfLightTable.Rows.Add(new object[] { "444", "Gell", "III" });
pdfLightTable.Rows.Add(new object[] { "555", "kelvin", "III" });
PdfCellStyle alternatestyle = new PdfCellStyle();
alternatestyle.BackgroundBrush = PdfBrushes.LightBlue;
alternatestyle.BorderPen = new PdfPen(Color.DarkViolet, 1f);
//Set style to alternate row
pdfLightTable.Style.AlternateStyle = alternatestyle;
PdfCellStyle defaultstyle = new PdfCellStyle();
defaultstyle.BorderPen = new PdfPen(Color.DarkViolet, 1f);
pdfLightTable.Style.DefaultStyle = defaultstyle;
PdfCellStyle headerstyle = new PdfCellStyle();
headerstyle.BackgroundBrush = PdfBrushes.Green;
//Set style to header.
pdfLightTable.Style.HeaderStyle = headerstyle;
//Create a new string format
PdfStringFormat format = new PdfStringFormat();
//Set the text Alignment
format.Alignment = PdfTextAlignment.Left;
//Set the line Alignment
format.LineAlignment = PdfVerticalAlignment.Middle;
//Set Stringformat to the lightTable cells.
for (int i = 0; i < 3; i++)
{
pdfLightTable.Columns[i].StringFormat = format;
}
//Show the header of lighttable.
pdfLightTable.Style.ShowHeader = true;
//Set padding to the cells.
pdfLightTable.Style.CellPadding = 2f;
//Draw the light table.
pdfLightTable.Draw(page, PointF.Empty);
doc.Save("output.pdf");
doc.Close(true);
|
Perfect, thanks!
I have an another problem with hungarian accented characters. Eg. ő will be õ.
Can I ask here or do I have to create a new topic?
PdfCellStyle defaultstyle = new PdfCellStyle();
//Create the PdfTrueTypeFont.
PdfFont font= new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Regular), true);
//Assign the font to the cellstyle.
defaultstyle.Font = font;
pdfLightTable.Style.DefaultStyle = defaultstyle; |
Thank you again!