How to Add Action in PDF to Realize to Jump to Top or Bottom – VB.NET

Sometimes, we can find that there are some buttons added in PDF document. By clicking these buttons, we can go to top or bottom of one page or whole contents. In this post, I will show a method about how to add actions in PDF to realize the above function via VB.NET.

In my example, I will draw a table in PDF and add two buttons at the top and bottom of this table. When I click button To Bottom, it will jump to bottom of table. Also, I use Spire.PDF in this method. So, I add its dll file in my project at the beginning.

The following steps show details.

  1. Create a new PDF document and set margin.
  2. Add page and draw page title, including title location and format. Also, I set distance between title and table top for having a better layout.
  3. Set table top location and mode.
  4. Set button format, including text font in button, button width and height. Then, set table layout.
  5. Draw table. Firstly, set format for table. There are two formats. One is for header and other is for body. Secondly, connect database and get necessary data information to dataTable and insert dataTable to PDF. Thirdly, set column width to make appearance be more wonderful. Finally, set table layout and draw table.
  6. Set bottom location and mode.
  7. Design button to bottom, including location, button text and background color. And then add action to realize the function that we can go to bottom by clicking this button.
  8. Design button to top and add action as step 7.
  9. Save and launch file.

Main Coding:

Imports System.Data

Imports System.Data.OleDb

Imports System.Drawing

Imports Spire.Pdf

Imports Spire.Pdf.Actions

Imports Spire.Pdf.Annotations

Imports Spire.Pdf.General

Imports Spire.Pdf.Graphics

Imports Spire.Pdf.Tables

Module Action

    Sub Main()

        'Create

        Dim doc As New PdfDocument()

        'Margin

        Dim unitCvtr As New PdfUnitConvertor()

        Dim margin As New PdfMargins()

        margin.Top = unitCvtr.ConvertUnits(2.54F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)

        margin.Bottom = margin.Top

        margin.Left = unitCvtr.ConvertUnits(3.17F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)

        margin.Right = margin.Left

        'Add Page

        Dim page As PdfPageBase = doc.Pages.Add(PdfPageSize.A4, margin)

        Dim y As Single = 10

        y = y + 20

        'Title

        Dim brush1 As PdfBrush = PdfBrushes.Olive

        Dim font1 As New PdfTrueTypeFont(New Font("Calibri", 14.0F, FontStyle.Bold))

        Dim format1 As New PdfStringFormat(PdfTextAlignment.Center)

        page.Canvas.DrawString("Customers Information", font1, brush1, page.Canvas.ClientSize.Width \ 2, y, format1)

        y = y + font1.MeasureString("Customers Information", format1).Height

        y = y + 10

        'Table Top

        Dim tableTopDest As New PdfDestination(page)

        tableTopDest.Location = New PointF(0, y)

        tableTopDest.Mode = PdfDestinationMode.Location

        tableTopDest.Zoom = 1.0F

        'Set Button Format and Table Layout          

        Dim buttonFont As New PdfTrueTypeFont(New Font("Calibri", 12.0F, FontStyle.Bold))

        Dim buttonWidth As Single = 70

        Dim buttonHeight As Single = buttonFont.Height * 1.5F

        Dim tableTop As Single = y

        Dim tableLayoutResult As PdfLayoutResult = DrawTable(page, y + buttonHeight + 5)

        'Table Bottom

        Dim tableBottomDest As New PdfDestination(tableLayoutResult.Page)

        tableBottomDest.Location = New PointF(0, tableLayoutResult.Bounds.Bottom)

        tableBottomDest.Mode = PdfDestinationMode.Location

        tableBottomDest.Zoom = 1.0F

        'To Bottom

        Dim x As Single = page.Canvas.ClientSize.Width – buttonWidth

        Dim format2 As New PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle)

        Dim buttonBounds As New RectangleF(x, tableTop, buttonWidth, buttonHeight)

        page.Canvas.DrawRectangle(PdfBrushes.LimeGreen, buttonBounds)

        page.Canvas.DrawString("To Bottom", buttonFont, PdfBrushes.White, buttonBounds, format2)

        Dim action1 As New PdfGoToAction(tableBottomDest)

        Dim annotation1 As New PdfActionAnnotation(buttonBounds, action1)

        annotation1.Border = New PdfAnnotationBorder(0.75F)

        annotation1.Color = Color.LimeGreen

        TryCast(page, PdfNewPage).Annotations.Add(annotation1)

        'To Top

        Dim tableBottom As Single = tableLayoutResult.Bounds.Bottom + 5

        buttonBounds = New RectangleF(x, tableBottom, buttonWidth, buttonHeight)

        tableLayoutResult.Page.Canvas.DrawRectangle(PdfBrushes.LimeGreen, buttonBounds)

        tableLayoutResult.Page.Canvas.DrawString("To Top", buttonFont, _

                                                 PdfBrushes.White, buttonBounds, format2)

        Dim action2 As New PdfGoToAction(tableTopDest)

        Dim annotation2 As New PdfActionAnnotation(buttonBounds, action2)

        annotation2.Border = New PdfAnnotationBorder(0.75F)

        annotation2.Color = Color.LimeGreen

        TryCast(tableLayoutResult.Page, PdfNewPage).Annotations.Add(annotation2)

        'Save

        doc.SaveToFile("Action.pdf")

        doc.Close()

        'Launch

        Process.Start("Action.pdf")

    End Sub

    Function DrawTable(ByVal page As PdfPageBase, ByVal y As Single) As PdfLayoutResult

        Dim brush1 As PdfBrush = PdfBrushes.Black

        'Create Data Table

        Dim table As New PdfTable()

        table.Style.CellPadding = 2

        table.Style.BorderPen = New PdfPen(brush1, 0.75F)

        table.Style.DefaultStyle.BackgroundBrush = PdfBrushes.White

        table.Style.DefaultStyle.Font = New PdfTrueTypeFont(New Font("Calibri", 9.0F))

        table.Style.DefaultStyle.StringFormat = New PdfStringFormat(PdfTextAlignment.Center)

        table.Style.HeaderSource = PdfHeaderSource.ColumnCaptions

        table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.AliceBlue

        table.Style.HeaderStyle.Font = New PdfTrueTypeFont(New Font("Calibri", 11.0F, FontStyle.Regular))

        table.Style.HeaderStyle.TextBrush = PdfBrushes.DarkOrange

        table.Style.HeaderStyle.StringFormat = New PdfStringFormat(PdfTextAlignment.Center)

        table.Style.ShowHeader = True

 

        Using conn As New OleDbConnection()

            conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\work\My Documents\vip.mdb"

            Dim command As New OleDbCommand()

            command.CommandText = " select Name, Gender, Birthday, Email, Country from VIP "

            command.Connection = conn

            Using dataAdapter As New OleDbDataAdapter(command)

                Dim dataTable As New DataTable()

                dataAdapter.Fill(dataTable)

                table.DataSourceType = PdfTableDataSourceType.TableDirect

                table.DataSource = dataTable

            End Using

        End Using

        Dim width As Single _

            = page.Canvas.ClientSize.Width – (table.Columns.Count + 1) * table.Style.BorderPen.Width

        table.Columns(0).Width = width * 0.55F * width

        table.Columns(1).Width = width * 0.35F * width

        table.Columns(2).Width = width * 0.45F * width

        table.Columns(3).Width = width * 0.95F * width

        table.Columns(4).Width = width * 0.55F * width

 

        Dim tableLayout As New PdfTableLayoutFormat()

        tableLayout.Break = PdfLayoutBreakType.FitElement

        tableLayout.Layout = PdfLayoutType.Paginate

 

        Dim result As PdfLayoutResult = table.Draw(page, New PointF(0, y), tableLayout)

        Return result

    End Function

End Module

Result Shown by Following:

  

_______________________________________________________________________________________________

Click Here to Learn more about Spire.PDF

Click Here to Download Spire.PDF

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s