SelectPdf for .NET - Pdf Text Element Properties - VB.NET / 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 VB.NET

Imports SelectPdf

Namespace Controllers
    Public Class PdfTextElementController
        Inherits Controller

        ' GET: PdfTextElement
        Public Function Index() As ActionResult
            Return View()
        End Function

        <HttpPost> _
        Public Function SubmitAction(collection As FormCollection) As ActionResult
            ' create a new pdf document
            Dim doc As New PdfDocument()

            ' create a new pdf font (component standard font)
            Dim font1 As PdfFont = doc.AddFont(PdfStandardFont.Helvetica)
            font1.Size = 20

            ' create a new pdf font (system font)
            Dim font2 As PdfFont = doc.AddFont(New System.Drawing.Font("Verdana", 15))

            ' add a new page to the document
            Dim page As PdfPage = doc.AddPage()

            ' define a rendering result object
            Dim result As PdfRenderingResult

            ' create text element 
            Dim text1 As New PdfTextElement(0, 0,
                    "Regular text using standard font Helvetica, size 20.", font1)
            result = page.Add(text1)

            ' create text element 
            Dim text2 As New PdfTextElement(0,
                    result.PdfPageLastRectangle.Bottom + 20,
                    "Regular text using system font Verdana, size 15.", font2)
            result = page.Add(text2)

            ' create colored text element 
            Dim text3 As 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
            Dim text4 As 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.",
            text4.ForeColor = System.Drawing.Color.Red
            text4.BackColor = System.Drawing.Color.LightGreen
            text4.HorizontalAlign = PdfTextHorizontalAlign.Right
            result = page.Add(text4)

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

            ' save pdf document
            Dim pdf As Byte() = doc.Save()

            ' close pdf document

            ' return resulted pdf document
            Dim fileResult As FileResult = New FileContentResult(pdf, "application/pdf")
            fileResult.FileDownloadName = "Document.pdf"
            Return fileResult
        End Function
    End Class
End Namespace