SelectPdf for .NET - Pdf Text Element Properties - C# / ASP.NET MVC Sample

This sample shows how to create a new PDF document using SelectPdf and add several text elements to it, modifying the properties (like font family, font size, rotation degree, text foreground color, background color) of these elements.


Sample Code C#



using System.Web.Mvc;

namespace SelectPdf.Samples.Controllers
{
    public class PdfTextElementController : Controller
    {
        // GET: PdfTextElement
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult SubmitAction(FormCollection collection)
        {
            // create a new pdf document
            PdfDocument doc = new PdfDocument();

            // create a new pdf font (component standard font)
            PdfFont font1 = doc.AddFont(PdfStandardFont.Helvetica);
            font1.Size = 20;

            // create a new pdf font (system font)
            PdfFont font2 = doc.AddFont(new System.Drawing.Font("Verdana", 15));

            // add a new page to the document
            PdfPage page = doc.AddPage();

            // define a rendering result object
            PdfRenderingResult result;

            // create text element 
            PdfTextElement text1 = new PdfTextElement(0, 0,
                "Regular text using standard font Helvetica, size 20.", font1);
            result = page.Add(text1);

            // create text element 
            PdfTextElement text2 = new PdfTextElement(0, result.PdfPageLastRectangle.Bottom + 20,
                "Regular text using system font Verdana, size 15.", font2);
            result = page.Add(text2);

            // create colored text element 
            PdfTextElement text3 = new PdfTextElement(0, result.PdfPageLastRectangle.Bottom + 20, 200,
                "Red text over light blue background, using system font Verdana, size 15, limited at a width of 200 points.", font2);
            text3.ForeColor = System.Drawing.Color.Red;
            text3.BackColor = System.Drawing.Color.LightBlue;
            result = page.Add(text3);

            // create colored text element, right aligned
            PdfTextElement text4 = new PdfTextElement(0, result.PdfPageLastRectangle.Bottom + 20, 300,
                "Red text over light green background, right aligned, using system font Verdana, size 15, limited at a width of 300 points.", font2);
            text4.ForeColor = System.Drawing.Color.Red;
            text4.BackColor = System.Drawing.Color.LightGreen;
            text4.HorizontalAlign = PdfTextHorizontalAlign.Right;
            result = page.Add(text4);

            // create text element 
            PdfTextElement text5 = new PdfTextElement(0, result.PdfPageLastRectangle.Bottom + 200, 300,
                "Regular text using system font Verdana, size 15, rotated 45 degrees.", font2);
            text5.Direction = 45; // rotate 45 degrees counter-clockwise
            result = page.Add(text5);

            // save pdf document
            byte[] pdf = doc.Save();

            // close pdf document
            doc.Close();

            // return resulted pdf document
            FileResult fileResult = new FileContentResult(pdf, "application/pdf");
            fileResult.FileDownloadName = "Document.pdf";
            return fileResult;
        }
    }
}