Hey guys,
It's been a while since I posted a question about iTextSharp but now I switched to version 5.5.13.3 so I could generate a QrCode, but I'm facing a problem that occurs in the line below:
Dim htmlarraylist As ArrayList = HTMLWorker.ParseToList(New StringReader(pData), Nothing)
The return message is:
Error BC30311 Value of type 'List(Of IElement)' cannot be converted to 'ArrayList'.
How can I fix this? Here's the code:
Code
VB.Net
Private Shared Function ImageExternaCell(ByVal path As String, ByVal scale As Single, ByVal align As Integer) As PdfPCell
Dim image As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(New Uri(path))
image.ScalePercent(scale)
image.ScaleAbsolute(140.0F, 80.0F)
Dim cell As New PdfPCell(image)
cell.BorderColor = BaseColor.WHITE
cell.VerticalAlignment = iTextSharp.text.pdf.PdfPCell.ALIGN_TOP
cell.HorizontalAlignment = align
cell.PaddingBottom = 0.0F
cell.PaddingTop = 0.0F
Return cell
End Function
Private Shared Function ImageCell(ByVal path As String, ByVal scale As Single, ByVal 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 = BaseColor.WHITE
cell.VerticalAlignment = iTextSharp.text.pdf.PdfPCell.ALIGN_TOP
cell.HorizontalAlignment = align
cell.PaddingBottom = 0.0F
cell.PaddingTop = 0.0F
Return cell
End Function
Private Shared Function PhraseCell(ByVal phrase As Phrase, ByVal align As Integer) As PdfPCell
Dim cell As New PdfPCell(phrase)
cell.BorderColor = BaseColor.WHITE
cell.VerticalAlignment = iTextSharp.text.pdf.PdfPCell.ALIGN_TOP
cell.HorizontalAlignment = align
cell.PaddingBottom = 2.0F
cell.PaddingTop = 0.0F
Return cell
End Function
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 iTextSharp.text.Font = FontFactory.GetFont("Soleto-Light", 16.0F, iTextSharp.text.Font.NORMAL)
Dim cellFont3 As iTextSharp.text.Font = FontFactory.GetFont("Soleto-Light", 9.0F, 0, BaseColor.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
<span style =""color:" #ff0000;"="">Dim htmlarraylist As ArrayList = HTMLWorker.ParseToList(New StringReader(pDados), Nothing)</span>
phraseDados = New Phrase()
For k As Integer = 0 To htmlarraylist.Count - 1
phraseDados.Add(CType(htmlarraylist(k), IElement))
Dim parapTexto As Paragraph = New Paragraph(phraseDados)
parapTexto.SetLeading(1.0F, 1.5F)
parapTexto.Alignment = ALIGN_JUSTIFIED
Dim cell As PdfPCell = New PdfPCell()
cell.BorderWidth = 0
cell.FixedHeight = 110.0F ' ALTURA DA CÉLULA
cell.AddElement(parapTexto)
table1.AddCell(cell) 'Coluna 2, Linha 1
Next
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, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)))
phraseAss2 = New Phrase()
phraseAss2.Add(New Chunk(pAssCoordenador & vbLf & "Coordenador(a)", FontFactory.GetFont("Soleto-Light", 9, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)))
table3.SpacingBefore = 20.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
Dim jpgAssina1 As iTextSharp.text.Image
If Trim(imageFilePath2) <> "" Then
jpgAssina1 = iTextSharp.text.Image.GetInstance(imageFilePath2)
jpgAssina1.ScalePercent(8.0F)
jpgAssina1.Alignment = iTextSharp.text.Image.UNDERLYING
'jpgAssina1.SetAbsolutePosition(155, 90)
jpgAssina1.SetAbsolutePosition(180, 90)
End If
'Definindo imagem da assinatura 2
Dim imageFilePath3 As String = pImgAssCoordenador
Dim jpgAssina2 As iTextSharp.text.Image
If Trim(imageFilePath3) <> "" Then
jpgAssina2 = iTextSharp.text.Image.GetInstance(imageFilePath3)
jpgAssina2.ScalePercent(8.0F)
jpgAssina2.Alignment = iTextSharp.text.Image.UNDERLYING
'jpgAssina2.SetAbsolutePosition(440, 90)
jpgAssina2.SetAbsolutePosition(465, 90)
End If
'=================================================================================
doc.Open()
doc.Add(jpg)
If Trim(imageFilePath2) <> "" Then doc.Add(jpgAssina1) 'Assinatura do Presidente
If Trim(imageFilePath3) <> "" Then doc.Add(jpgAssina2) 'Assinatura do Coordenador
doc.Add(New Paragraph(" "))
doc.Add(table1)
doc.Add(New Paragraph(" "))
doc.Add(table2)
doc.Add(table3)
doc.NewPage()
doc.Add(New Paragraph(" "))
doc.Add(New Paragraph(" "))
Dim tbEmenta As PdfPTable = New PdfPTable(New Single() {100})
tbEmenta.DefaultCell.HorizontalAlignment = Element.ALIGN_JUSTIFIED 'Justificar
tbEmenta.DefaultCell.BorderWidth = 0
Dim phraseEmenta = New Phrase()
'MOSTRAR TEXTO HTML FORMATADO - EMENTA
Dim vlEmenta As String = pEmenta.Replace(System.Environment.NewLine, "<br />")
'Dim html As ArrayList = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(New StringReader(vlEmenta), Nothing)
Dim html As List(Of iTextSharp.text.IElement)
html = HTMLWorker.ParseToList(New StreamReader(vlEmenta), Nothing)
For k As Integer = 0 To html.Count - 1
phraseEmenta.Add(CType(html(k), IElement))
Next
Dim parapEmenta As Paragraph = New Paragraph(phraseEmenta)
parapEmenta.SetLeading(1.0F, 0.8F) 'Esta linha regula o espaçamento entre linhas.
Dim cellEmenta As PdfPCell = New PdfPCell()
cellEmenta.BorderWidth = 0
parapEmenta.Alignment = ALIGN_JUSTIFIED
cellEmenta.AddElement(parapEmenta)
tbEmenta.AddCell(cellEmenta)
doc.Add(tbEmenta)
' #####################################################################################
'GERA O CÓDIGO DE BARRAS
'Dim barcodeQRCode As BarcodeQrCode = New BarcodeQrCode("http://www.google.com.br", 1000, 1000, Nothing)
'Dim codeQRImage As Image = barcodeQRCode.GetImage()
'codeQRImage.ScaleAbsolute(20, 200)
'doc.Add(codeQRImage)
'If ViewState("ImageBase64") IsNot Nothing Then
' Dim base64 As String = DirectCast(ViewState("ImageBase64"), String)
' Dim imagebytes As Byte() = Convert.FromBase64String(base64)
' Dim imgQrCode As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(imagebytes)
' doc.Add(imgQrCode)
'End If
' #####################################################################################
'doc.NewPage()
Dim barcodeQRCode As BarcodeQRCode = New BarcodeQRCode("http://www.google.com.br", 1000, 1000, Nothing)
Dim codeQRImage As iTextSharp.text.Image = barcodeQRCode.GetImage()
codeQRImage.ScaleAbsolute(20, 200)
doc.Add(codeQRImage)
doc.Close()
' =======================================================================================
' #####################################################################################
' CRIAR SENHA NO DOCUMENTO
' #####################################################################################
If Trim(pSenha) <> "" Then
Dim bytes As Byte() = File.ReadAllBytes(pCaminho + "/" & pCPF & ".pdf")
Using input As New MemoryStream(bytes)
Using output As New MemoryStream()
Dim password As String = pSenha
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.ContentType = "application/pdf"
Response.AddHeader("Content-Disposition", "attachment; filename=Employee.pdf")
Response.ContentType = "application/pdf"
Response.Buffer = True
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.BinaryWrite(bytes)
Response.End()
Response.Close()
End If
' #####################################################################################
If File.Exists(pCaminho + "/" & pCPF & ".pdf") Then
'Response.Redirect("~/aluno/pdfs/" & pCPF & ".pdf")
Dim modified_URL As String = "window.open('" & "pdfs/" & pCPF & ".pdf" & "', '_blank');"
ScriptManager.RegisterStartupScript(Me, GetType(String), "OPEN_WINDOW", modified_URL, True)
End If
End Sub
Yours sincerely,
Ilano.