Please refer below modified code.
Code
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindEmployeesDropDown()
End If
End Sub
Private Sub BindEmployeesDropDown()
ddlEmployees.DataSource = GetData("SELECT EmployeeId, (FirstName + ' ' + LastName) Name FROM Employees")
ddlEmployees.DataTextField = "Name"
ddlEmployees.DataValueField = "EmployeeId"
ddlEmployees.DataBind()
End Sub
Private Function GetData(query As String) As DataTable
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim cmd As New SqlCommand(query)
Using con As New SqlConnection(conString)
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Protected Sub GenerateReport(sender As Object, e As EventArgs)
Dim dr As DataRow = GetData("SELECT * FROM Employees where EmployeeId = " + ddlEmployees.SelectedItem.Value).Rows(0)
Dim document As New Document(PageSize.A4, 88.0F, 88.0F, 10.0F, 10.0F)
Dim NormalFont As Font = FontFactory.GetFont("Arial", 12, Font.NORMAL, Color.BLACK)
Using MemoryStream As New MemoryStream()
Dim writer As PdfWriter = PdfWriter.GetInstance(document, MemoryStream)
Dim phrase As Phrase = Nothing
Dim cell As PdfPCell = Nothing
Dim table As PdfPTable = Nothing
Dim color__1 As Color = Nothing
document.Open()
'Header Table
table = New PdfPTable(2)
table.TotalWidth = 500.0F
table.LockedWidth = True
table.SetWidths(New Single() {0.3F, 0.7F})
'Company Logo
cell = ImageCell("~/images/northwindlogo.gif", 30.0F, PdfPCell.ALIGN_CENTER)
table.AddCell(cell)
'Company Name and Address
phrase = New Phrase()
phrase.Add(New Chunk("Microsoft Northwind Traders Company" & vbLf & vbLf, FontFactory.GetFont("Arial", 16, Font.BOLD, Color.RED)))
phrase.Add(New Chunk("107, Park site," & vbLf, FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)))
phrase.Add(New Chunk("Salt Lake Road," & vbLf, FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)))
phrase.Add(New Chunk("Seattle, USA", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
cell.VerticalAlignment = PdfCell.ALIGN_TOP
table.AddCell(cell)
'Separater Line
color__1 = New Color(System.Drawing.ColorTranslator.FromHtml("#A9A9A9"))
DrawLine(writer, 25.0F, document.Top - 79.0F, document.PageSize.Width - 25.0F, document.Top - 79.0F, color__1)
DrawLine(writer, 25.0F, document.Top - 80.0F, document.PageSize.Width - 25.0F, document.Top - 80.0F, color__1)
document.Add(table)
table = New PdfPTable(2)
table.HorizontalAlignment = Element.ALIGN_LEFT
table.SetWidths(New Single() {0.3F, 1.0F})
table.SpacingBefore = 20.0F
'Employee Details
cell = PhraseCell(New Phrase("Employee Record", FontFactory.GetFont("Arial", 12, Font.UNDERLINE, Color.BLACK)), PdfPCell.ALIGN_CENTER)
cell.Colspan = 2
table.AddCell(cell)
cell = PhraseCell(New Phrase(), PdfPCell.ALIGN_CENTER)
cell.Colspan = 2
cell.PaddingBottom = 30.0F
table.AddCell(cell)
'Photo
cell = ImageCell(String.Format("~/photos/{0}.jpg", dr("EmployeeId")), 25.0F, PdfPCell.ALIGN_CENTER)
table.AddCell(cell)
'Name
phrase = New Phrase()
phrase.Add(New Chunk(dr("TitleOfCourtesy").ToString & " " + dr("FirstName").ToString & " " + dr("LastName").ToString, FontFactory.GetFont("Arial", 10, Font.BOLD, Color.BLACK)))
phrase.Add(New Chunk("(" + dr("Title").ToString() + ")", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE
table.AddCell(cell)
document.Add(table)
DrawLine(writer, 160.0F, 80.0F, 160.0F, 690.0F, Color.BLACK)
DrawLine(writer, 115.0F, document.Top - 200.0F, document.PageSize.Width - 100.0F, document.Top - 200.0F, Color.BLACK)
table = New PdfPTable(2)
table.SetWidths(New Single() {0.5F, 2.0F})
table.TotalWidth = 340.0F
table.LockedWidth = True
table.SpacingBefore = 20.0F
table.HorizontalAlignment = Element.ALIGN_RIGHT
'Employee Id
table.AddCell(PhraseCell(New Phrase("Employee code:", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT))
table.AddCell(PhraseCell(New Phrase("000" + dr("EmployeeId"), FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)), PdfPCell.ALIGN_LEFT))
cell = PhraseCell(New Phrase(), PdfPCell.ALIGN_CENTER)
cell.Colspan = 2
cell.PaddingBottom = 10.0F
table.AddCell(cell)
'Address
table.AddCell(PhraseCell(New Phrase("Address:", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT))
phrase = New Phrase(New Chunk(dr("Address").ToString, FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)))
phrase.Add(New Chunk(dr("City").ToString + vbLf, FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)))
phrase.Add(New Chunk(dr("Region").ToString + " " + dr("Country").ToString + " " + dr("PostalCode").ToString, FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)))
table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT))
cell = PhraseCell(New Phrase(), PdfPCell.ALIGN_CENTER)
cell.Colspan = 2
cell.PaddingBottom = 10.0F
table.AddCell(cell)
'Date of Birth
table.AddCell(PhraseCell(New Phrase("Date of Birth:", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT))
table.AddCell(PhraseCell(New Phrase(Convert.ToDateTime(dr("BirthDate")).ToString("dd MMMM, yyyy"), FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)), PdfPCell.ALIGN_LEFT))
cell = PhraseCell(New Phrase(), PdfPCell.ALIGN_CENTER)
cell.Colspan = 2
cell.PaddingBottom = 10.0F
table.AddCell(cell)
'Phone
table.AddCell(PhraseCell(New Phrase("Phone Number:", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT))
table.AddCell(PhraseCell(New Phrase(dr("HomePhone") + " Ext: " + dr("Extension"), FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)), PdfPCell.ALIGN_LEFT))
cell = PhraseCell(New Phrase(), PdfPCell.ALIGN_CENTER)
cell.Colspan = 2
cell.PaddingBottom = 10.0F
table.AddCell(cell)
'Addtional Information
table.AddCell(PhraseCell(New Phrase("Addtional Information:", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT))
table.AddCell(PhraseCell(New Phrase(dr("Notes").ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)), PdfPCell.ALIGN_JUSTIFIED))
document.Add(table)
document.Close()
Dim bytes As Byte() = MemoryStream.ToArray()
MemoryStream.Close()
Using input As New MemoryStream(bytes)
Using output As New MemoryStream()
Dim password As String = ddlEmployees.SelectedItem.Value & dr("FirstName") 'Set dynamic password as per your logic.
Dim reader As New PdfReader(input)
PdfEncryptor.Encrypt(reader, output, True, password, password, PdfWriter.ALLOW_SCREENREADERS)
bytes = output.ToArray()
End Using
End Using
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Disposition", "attachment; filename=Employee.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.BinaryWrite(bytes)
Response.End()
Response.Close()
End Using
End Sub
Private Sub CriaRelatorioPDF(ByVal pCPF As String, ByVal pDados As String, ByVal pEmenta As String, ByVal pDataEmissao As String, ByVal pImgAssPresidente As String,
ByVal pImgAssCoordenador As String, ByVal pAssPresidente As String, ByVal pAssCoordenador As String, ByVal pSenha As String)
Dim pCaminho As String
pCaminho = Server.MapPath("pdfs")
If File.Exists(pCaminho + "/" & pCPF & ".pdf") Then
File.Delete(pCaminho + "/" & pCPF & ".pdf")
End If
'Atribuir fontes do servidor na aplicação
FontFactory.RegisterDirectory("C:\WINDOWS\Fonts")
Dim cellFont As Font = FontFactory.GetFont("Soleto-Light", 16.0F, Font.NORMAL)
Dim cellFont3 As Font = FontFactory.GetFont("Soleto-Light", 9.0F, 0, Color.BLACK)
'Criar uma instância de document
Dim doc As New Document(iTextSharp.text.PageSize.A4.Rotate, 20, 20, 20, 20)
PdfWriter.GetInstance(doc, New FileStream(pCaminho + "/" & pCPF & ".pdf", FileMode.Create))
hfArquivo.Value = "pdfs/" & pCPF & ".pdf"
Dim phraseDados As Phrase = Nothing 'Texto
Dim phraseAss1 As Phrase = Nothing 'Frase da Assinatura 1
Dim cellAss1 As PdfPCell = Nothing 'Célula da assinatura 1
Dim cellImg1 As PdfPCell = Nothing 'Célula da imagem da assinatura 1
Dim phraseAss2 As Phrase = Nothing 'Frase da Assinatura 2
Dim cellAss2 As PdfPCell = Nothing 'Célula da assinatura 2
Dim cellImg2 As PdfPCell = Nothing 'Célula da imagem da assinatura 2
'Criar tabela ====================================================================
Dim table1 As PdfPTable = New PdfPTable(New Single() {10, 80, 10})
table1.DefaultCell.HorizontalAlignment = Element.ALIGN_JUSTIFIED 'Justificar texto
table1.SpacingBefore = 220.0F 'Definir altura onde a tabela será criada
table1.DefaultCell.BorderWidth = 0 'Largura da borda da tabela 1
table1.DefaultCell.FixedHeight = 50.0F 'Altura da tabela 1
table1.AddCell(" ") 'Coluna 1, Linha 1
Dim htmlarraylist As ArrayList = HTMLWorker.ParseToList(New StringReader(pDados), Nothing)
phraseDados = New Phrase()
For k As Integer = 0 To htmlarraylist.Count - 1
phraseDados.Add(CType(htmlarraylist(k), IElement))
Next
table1.AddCell(phraseDados) 'Coluna 2, Linha 1
table1.AddCell(" ") 'Coluna 3, Linha 1
Dim table2 As PdfPTable = New PdfPTable(New Single() {10, 80, 10})
table2.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER 'Centralizar
table2.DefaultCell.BorderWidth = 0 'Largura da borda da tabela 2
table2.DefaultCell.FixedHeight = 50.0F 'Altura da tabela 2
table2.AddCell(" ") 'Coluna 1, Linha 1
table2.AddCell(pDataEmissao) 'Coluna 2, Linha 1
table2.AddCell(" ") 'Coluna 3, Linha 1
Dim table3 As PdfPTable = New PdfPTable(New Single() {10, 30, 10, 30, 10})
phraseAss1 = New Phrase()
phraseAss1.Add(New Chunk(pAssPresidente & vbLf & "Diretor Presidente", FontFactory.GetFont("Soleto-Light", 9, Font.NORMAL, Color.BLACK)))
phraseAss2 = New Phrase()
phraseAss2.Add(New Chunk(pAssCoordenador & vbLf & "Coordenador(a)", FontFactory.GetFont("Soleto-Light", 9, Font.NORMAL, Color.BLACK)))
table3.SpacingBefore = 60.0F 'Definir altura onde a tabela será criada
table3.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER 'Centralizar texto
table3.DefaultCell.BorderWidth = 0 'Largura da borda da tabela
table3.AddCell("") 'Coluna 1, Linha 1
table3.AddCell("") 'Coluna 2, Linha 1
table3.AddCell(" ") 'Coluna 3, Linha 1
table3.AddCell(" ") 'Coluna 4, Linha 1
table3.AddCell(" ") 'Coluna 5, Linha 1
table3.AddCell("") 'Coluna 1, Linha 1
table3.AddCell("_______________________________") 'Coluna 2, Linha 1
table3.AddCell(" ") 'Coluna 3, Linha 1
table3.AddCell("_______________________________") 'Coluna 4, Linha 1
table3.AddCell(" ") 'Coluna 5, Linha 1
table3.AddCell(" ") 'Coluna 1, Linha 2
table3.AddCell(phraseAss1) 'Coluna 2, Linha 2
table3.AddCell(" ") 'Coluna 3, Linha 2
table3.AddCell(phraseAss2) 'Coluna 4, Linha 2
table3.AddCell(" ") 'Coluna 5, Linha 2
table3.AddCell(" ") 'Coluna 1, Linha 2
table3.AddCell("") 'Coluna 2, Linha 2
table3.AddCell(" ") 'Coluna 3, Linha 2
table3.AddCell("") 'Coluna 4, Linha 2
table3.AddCell(" ") 'Coluna 5, Linha 2
'Definindo a imagem de fundo do documento
Dim imageFilePath As String = Server.MapPath("~/imagens/Cedula_Certificado_01.jpg")
Dim jpg As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(imageFilePath)
jpg.ScaleToFit(850, 650)
jpg.Alignment = iTextSharp.text.Image.UNDERLYING
jpg.SetAbsolutePosition(0, 0)
'Definindo imagem da assinatura 1
Dim imageFilePath2 As String = pImgAssPresidente 'Server.MapPath("~/imagens/Cedula_Certificado_01.jpg")
Dim jpgAssina1 As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(imageFilePath2)
jpgAssina1.ScalePercent(10.0F)
jpgAssina1.Alignment = iTextSharp.text.Image.UNDERLYING
jpgAssina1.SetAbsolutePosition(155, 100)
'Definindo imagem da assinatura 2
Dim imageFilePath3 As String = pImgAssCoordenador 'Server.MapPath("~/imagens/Cedula_Certificado_01.jpg")
Dim jpgAssina2 As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(imageFilePath3)
jpgAssina2.ScalePercent(10.0F)
jpgAssina2.Alignment = iTextSharp.text.Image.UNDERLYING
jpgAssina2.SetAbsolutePosition(440, 100)
'=================================================================================
doc.Open()
doc.Add(jpg)
doc.Add(jpgAssina1) 'Assinatura do Presidente
doc.Add(jpgAssina2) 'Assinatura do Coordenador
doc.Add(New Paragraph(" "))
doc.Add(table1)
doc.Add(New Paragraph(" "))
doc.Add(table2)
doc.Add(table3)
' ######################################################################################
'THIS PAGE DOES NOT CREATE
' ######################################################################################
doc.NewPage()
Dim html As ArrayList = HTMLWorker.ParseToList(New StringReader(pEmenta), Nothing)
For k As Integer = 0 To html.Count - 1
doc.Add(CType(html(k), IElement))
Next
doc.Close()
If File.Exists(pCaminho + "/" & pCPF & ".pdf") Then
hlDownload.NavigateUrl = "~/aluno/pdfs/" & pCPF & ".pdf"
hlDownload.Visible = True
End If
' =================================================================================
' #####################################################################################
' CREATE PASSWORD ON DOCUMENT
' #####################################################################################
Dim bytes As Byte() = File.ReadAllBytes(pCaminho + "/" & pCPF & ".pdf")
Using input As New MemoryStream(bytes)
Using output As New MemoryStream()
Dim password As String = ""
Dim reader As New PdfReader(input)
PdfEncryptor.Encrypt(reader, output, True, password, password, PdfWriter.ALLOW_SCREENREADERS)
bytes = output.ToArray()
End Using
End Using
' #####################################################################################
Response.Buffer = True
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=Employee.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.BinaryWrite(bytes)
Response.End()
Response.Close()
End Sub
Private Shared Sub DrawLine(writer As PdfWriter, x1 As Single, y1 As Single, x2 As Single, y2 As Single, color As Color)
Dim contentByte As PdfContentByte = writer.DirectContent
contentByte.SetColorStroke(color)
contentByte.MoveTo(x1, y1)
contentByte.LineTo(x2, y2)
contentByte.Stroke()
End Sub
Private Shared Function PhraseCell(phrase As Phrase, align As Integer) As PdfPCell
Dim cell As New PdfPCell(phrase)
cell.BorderColor = Color.WHITE
cell.VerticalAlignment = PdfCell.ALIGN_TOP
cell.HorizontalAlignment = align
cell.PaddingBottom = 2.0F
cell.PaddingTop = 0.0F
Return cell
End Function
Private Shared Function ImageCell(path As String, scale As Single, align As Integer) As PdfPCell
Dim image As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath(path))
image.ScalePercent(scale)
Dim cell As New PdfPCell(image)
cell.BorderColor = Color.WHITE
cell.VerticalAlignment = PdfCell.ALIGN_TOP
cell.HorizontalAlignment = align
cell.PaddingBottom = 0.0F
cell.PaddingTop = 0.0F
Return cell
End Function