SelectPdf for .NET - Modify Existing Pdf - VB.NET / ASP.NET MVC Sample

This sample shows how to use SelectPdf Library for .NET to load an existing document, add a new page to it and a new text element.

Here is our initial test pdf document:
Test file

Click on the "Create PDF" button below to see the result.


Sample Code VB.NET



Imports SelectPdf

Namespace Controllers
    Public Class ModifyExistingPdfController
        Inherits Controller

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

        <HttpPost>
        Public Function SubmitAction(collection As FormCollection) As ActionResult
            ' the initial file
            Dim file As String = Server.MapPath("~/files/doc1.pdf")

            ' load the pdf document
            Dim doc As New PdfDocument(file)

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

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

            ' create text element and add it to the new page
            Dim text As New PdfTextElement(100, 100,
                        "Sample text added to an existing pdf document.", font)
            page.Add(text)

            ' 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