BoldSignA modern eSignature application with affordable pricing. Sign up today for unlimited document usage!
string text = "Hello world!!! Hello world!!!";
//Creates a new PDF document.
PdfDocument doc = new PdfDocument();
//Adds a page.
PdfPage page = doc.Pages.Add();
//Acquires page's graphics
PdfGraphics graphics = page.Graphics;
PdfStringFormat drawFormat = new PdfStringFormat();
drawFormat.WordWrap = PdfWordWrapType.Word;
drawFormat.Alignment = PdfTextAlignment.Justify;
drawFormat.LineAlignment = PdfVerticalAlignment.Top;
//Set the font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 10f);
//Create a solid brush.
PdfBrush brush = new PdfSolidBrush(new PdfColor(Color.Red));
RectangleF bounds = new RectangleF(new PointF(10, 10), new SizeF(page.Graphics.ClientSize.Width - 30, page.Graphics.ClientSize.Height - 20));
// Draw the string one after another.
graphics.DrawString(text, font, brush, bounds, drawFormat);
graphics.DrawString(text, font, brush, bounds.X ,bounds.Y + 10, drawFormat);
// Creates a PdfLightTable.
PdfLightTable pdfLightTable = new PdfLightTable();
// Initializes DataTable to assign as DateSource to the light table.
DataTable table = new DataTable();
//Includes columns to the DataTable.
table.Columns.Add("Name");
table.Columns.Add("Age");
table.Columns.Add("Sex");
//Includes rows to the DataTable.
table.Rows.Add(new string[] { "abc", "21", "Male" });
//Assigns data source.
pdfLightTable.DataSource = table;
//Includes the style to display the header of the light table.
pdfLightTable.Style.ShowHeader = true;
//Draws PdfLightTable and returns the rendered bounds.
PdfLayoutResult result = pdfLightTable.Draw(page, new PointF(10, 40));
//draw string with returned bounds from table
graphics.DrawString(text, font, brush, result.Bounds.X,result.Bounds.Bottom, drawFormat);
// Creates a new PdfLightTable.
pdfLightTable = new PdfLightTable();
pdfLightTable.DataSource = table;
//Includes the style to display the header of the light table.
pdfLightTable.Style.ShowHeader = true;
//Draws PdfLightTable with returned bounds from previous table
PdfLayoutResult layoutResult = pdfLightTable.Draw(page, new PointF(10, result.Bounds.Bottom + 10));
//draw string with returned bounds from table
graphics.DrawString(text, font, brush, bounds.X, layoutResult.Bounds.Bottom + 10, drawFormat);
//Saves the document.
doc.Save("Output.pdf");
doc.Close(true);
|