SelectPdf for .NET - Pdf Image Element Properties - VB.NET / ASP.NET MVC Sample

This sample shows how to create a new PDF document using SelectPdf and add several image elements to it, modifying the properties of these elements.


Sample Code VB.NET



Imports SelectPdf

Namespace Controllers
    Public Class PdfImageElementController
        Inherits Controller

        ' GET: PdfImageElement
        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()

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

            ' get image path
            Dim imgFile As String = Server.MapPath("~/files/logo.png")

            ' define a rendering result object
            Dim result As PdfRenderingResult

            ' create image element from file path with real image size
            Dim img1 As New PdfImageElement(0, 0, imgFile)
            result = page.Add(img1)

            ' create image element from system Image object with fixed size in pdf 
            ' (aspect ratio not preserved)
            Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(imgFile)
            Dim img2 As New PdfImageElement(0,
                        result.PdfPageLastRectangle.Bottom + 50, 100, 100, img)
            result = page.Add(img2)

            ' add text element
            Dim font1 As PdfFont = doc.AddFont(PdfStandardFont.Helvetica)
            font1.Size = 10
            Dim text1 As New PdfTextElement(0,
                        result.PdfPageLastRectangle.Bottom + 50, Helper.SomeText(), font1)
            page.Add(text1)

            ' add image over text (no transparency)
            Dim img3 As New PdfImageElement(0,
                        result.PdfPageLastRectangle.Bottom + 50, imgFile)
            result = page.Add(img3)

            ' add image over text (with transparency) next to the previous image
            Dim img4 As New PdfImageElement(result.PdfPageLastRectangle.Right + 30,
                                            result.PdfPageLastRectangle.Top, imgFile)
            img4.Transparency = 50
            result = page.Add(img4)

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

            ' close pdf document
            doc.Close()

            ' 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