Hey guys,
I was able to print the content saved in the database, including the table created in SKEditor also saved in the database. Now all that remains is to make some final touches.
If you notice, the first table, created in ITextSharp is aligned on the page but the rest of the content, including the image, are outside the margins.
How do I get all the content aligned correctly?
Can you help me with this?
Imports System.IO
Imports iTextSharp
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.Image
Imports iTextSharp.text.html.simpleparser
Imports iTextSharp.text.Paragraph
Partial Class ci_relatorios_relDocPDF
Inherits System.Web.UI.Page
Private Documento As New CENTEC.objDocumento
Private Sub RecebeParametros()
If Not Request("relID") Is Nothing Then
ViewState("vlrID") = Request("relID")
Else
If Request("relID") Is Nothing Then
ViewState("vlrID") = 0
End If
End If
If Not Request("relTdoc") Is Nothing Then
ViewState("vlrdoc") = Request("relTdoc")
Else
If Request("relTdoc") Is Nothing Then
ViewState("vlrTdoc") = 0
End If
End If
End Sub
Private Sub Consulta()
Dim pTexto As String = DBNull.Value.ToString
Dim Ds As Data.DataSet
Dim pSQL As String = "SET LANGUAGE BRAZILIAN " &
" SELECT A.ID, A.ASSUNTO, " &
" NUMERODOC = CONCAT(REPLICATE('0', 3 - LEN(CAST(A.NUMERO AS VARCHAR))) + CAST(A.NUMERO AS VARCHAR), '/', YEAR(A.DTCRIACAO)), " &
" USUORIGEM = dbo.fnAjustaNome(B.NOME), SETOR_ORIGEM = H.DESCRICAO, USUDESTINO = dbo.fnAjustaNome(C.NOME), SETOR_DESTINO = L.DESCRICAO, " &
" USUSETOORIGEM = dbo.fnAjustaNome(B.NOME) + CHAR(13) + CHAR(10) + H.DESCRICAO, " &
" USUSETORDESTINO = dbo.fnAjustaNome(C.NOME) + CHAR(13) + CHAR(10) + L.DESCRICAO, " &
" DATA_CRIACAO = CONVERT(VARCHAR(10), A.DTCRIACAO, 103), TIPO_DOCUMENTO = D.NOME, " &
" TEXTO = CONVERT(VARCHAR(MAX), A.TEXTO), " &
" DATA_ATUAL = dbo.ft_DiaMesAnoExtenso(GETDATE()) " &
" " &
" FROM ZMDDOCUMENTO A(NOLOCK) " &
" INNER JOIN PFUNC B(NOLOCK) ON (B.CHAPA = A.CODUSUARIOORIGEM) " &
" INNER JOIN PFCOMPL BB(NOLOCK) ON (BB.CHAPA = B.CHAPA And BB.CODCOLIGADA = B.CODCOLIGADA) " &
" INNER JOIN PFUNC C(NOLOCK) ON (C.CHAPA = A.CODUSUARIOADESTINO) " &
" INNER JOIN PFCOMPL CC(NOLOCK) ON (CC.CHAPA = C.CHAPA And CC.CODCOLIGADA = C.CODCOLIGADA) " &
" INNER JOIN ZMDTIPODOCUMENTO D (NOLOCK) ON (D .ID = A.TIPODOCUMENTO) " &
" INNER JOIN PSECAO E(NOLOCK) ON (E.CODIGO = B.CODSECAO And E.CODCOLIGADA = B.CODCOLIGADA) " &
" INNER JOIN PFUNCAO F(NOLOCK) ON (F.CODIGO = B.CODFUNCAO And F.CODCOLIGADA = B.CODCOLIGADA) " &
" INNER JOIN GDEPTO G(NOLOCK) ON (G.CODDEPARTAMENTO = E.CODDEPTO And G.CODFILIAL = E.CODFILIAL And G.CODCOLIGADA = E.CODCOLIGADA) " &
" INNER JOIN GCONSIST H ON (H.CODCLIENTE = BB.SETORCENTEC And H.CODTABELA = 'SETCENTEC') " &
" INNER JOIN PSECAO I(NOLOCK) ON (I.CODIGO = C.CODSECAO AND I.CODCOLIGADA = C.CODCOLIGADA) " &
" INNER JOIN PFUNCAO J(NOLOCK) ON (J.CODIGO = C.CODFUNCAO AND J.CODCOLIGADA = C.CODCOLIGADA) " &
" INNER JOIN GDEPTO K(NOLOCK) ON (K.CODDEPARTAMENTO = I.CODDEPTO AND K.CODFILIAL = I.CODFILIAL AND K.CODCOLIGADA = I.CODCOLIGADA) " &
" INNER JOIN GCONSIST L ON (L.CODCLIENTE = CC.SETORCENTEC AND L.CODTABELA = 'SETCENTEC') " &
" " &
" WHERE A.ID IN (" & ViewState("vlrID") & ") " &
" ORDER BY A.ID "
Ds = Documento.ConsultaGenerica(pSQL)
If Not Ds Is Nothing Then
If Ds.Tables(0).Rows.Count > 0 Then
LbDados.Text = "<div style='background-image:url(" & Session("sDominio") & "/imagens/pdf-file.svg); background-position:center; background-repeat:no-repeat; background-size:contain; height:50px;'></div>" &
"<div class='h5' style='text-align:center;'>" & Ds.Tables(0).Rows(0)("ASSUNTO") & "</div>" &
"<div style='text-align:center;'>Documento Nº " & Ds.Tables(0).Rows(0)("NUMERODOC") & "</div>"
pTexto = Server.HtmlEncode(Ds.Tables(0).Rows(0)("TEXTO"))
MeuPDFTeste(Ds.Tables(0).Rows(0)("TIPO_DOCUMENTO"),
Ds.Tables(0).Rows(0)("USUORIGEM"),
Ds.Tables(0).Rows(0)("SETOR_ORIGEM"),
Ds.Tables(0).Rows(0)("USUDESTINO"),
Ds.Tables(0).Rows(0)("SETOR_DESTINO"),
Ds.Tables(0).Rows(0)("NUMERODOC"),
Ds.Tables(0).Rows(0)("DATA_ATUAL"),
Ds.Tables(0).Rows(0)("ASSUNTO"),
Ds.Tables(0).Rows(0)("TEXTO"))
End If
End If
End Sub
Function Conteudo() As String
Dim pRetorno As String = DBNull.Value.ToString
Dim Ds As Data.DataSet
Ds = Documento.Consultar(Session("sUsuario"), "", "")
If Not Ds Is Nothing Then
If Ds.Tables(0).Rows.Count > 0 Then
pRetorno = Server.HtmlEncode(Ds.Tables(0).Rows(0)("TEXTO"))
End If
End If
Return pRetorno
End Function
Function Usuario() As String
Dim Ds As Data.DataSet
Dim pRetorno As String = DBNull.Value.ToString
Ds = Documento.ConsultaGenerica(" SELECT A.CODUSUARIO " &
" FROM GUSUARIO A " &
" WHERE A.STATUS = 1 AND A.CODUSUARIO = '00802' ")
If Not Ds Is Nothing Then
If Ds.Tables(0).Rows.Count > 0 Then pRetorno = Ds.Tables(0).Rows(0)("CODUSUARIO")
End If
Return pRetorno
End Function
Private Sub MeuPDFTeste(ByVal pTitulo As String, ByVal pRemetente As String, ByVal pCargoRemetente As String,
ByVal pDestinatario As String, ByVal pCargoDestinatario As String, ByVal pNumDoc As String,
ByVal pDataAtual As String, ByVal pAssunto As String, ByVal pConteudo As String)
Dim pCaminho As String
Dim pUsuario As String = Usuario()
pCaminho = Server.MapPath("pdfs")
If File.Exists(pCaminho + "/" & Usuario() & ".pdf") Then
File.Delete(pCaminho + "/" & Usuario() & ".pdf")
End If
Dim pDoc As New Document(iTextSharp.text.PageSize.A4, 20, 20, 20, 20)
PdfWriter.GetInstance(pDoc, New FileStream(pCaminho + "/" & pUsuario & ".pdf", FileMode.Create))
pDoc.Open()
'DEFINIR FONTE
Dim pFonte1 As Font = FontFactory.GetFont("Arial", 9.0F, 0, Color.BLACK)
Dim pFonte2 As Font = FontFactory.GetFont("Arial", 9.0F, 0, Color.BLACK)
'Identificando o arquivo através de suas propriedades
pDoc.AddAuthor("Centec")
pDoc.AddCreator(Usuario)
'Definir imagem
Dim scale As Single = 0
Dim pImagem As String = Session("sDominio") & "/documentos/imagens/logo_centec.png"
Dim pPNG As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(pImagem)
pPNG.ScaleAbsolute(120.0F, 50.0F)
pDoc.Add(pPNG)
'CRIA HEADER
Dim pHeader As New HeaderFooter(New Phrase(""), True)
pHeader.Border = iTextSharp.text.Rectangle.NO_BORDER
pHeader.Border = Element.ALIGN_RIGHT
pDoc.Header = pHeader
'CRIA O PRIMEIRO PARÁGRAFO
Dim pParagrafo1 As New iTextSharp.text.Paragraph(pTitulo, pFonte1)
pParagrafo1.Alignment = Element.ALIGN_CENTER
' ADICIONAR TABELA 1
Dim pTabela As New PdfPTable(2)
Dim pCell As New PdfPCell(New iTextSharp.text.Phrase(pTitulo))
pCell.Colspan = 2
pCell.HorizontalAlignment = Element.ALIGN_CENTER
pCell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE
pCell.FixedHeight = 20
pTabela.AddCell(pCell)
pTabela.AddCell(New Phrase("De: " & pRemetente))
pTabela.AddCell(New Phrase("Para: " & pDestinatario))
pCell.AddElement(New Phrase(pCargoDestinatario))
'pTabela.AddCell("Assunto: " & pAssunto)
pCell = New PdfPCell(New iTextSharp.text.Phrase("Assunto: " & pAssunto))
pCell.Colspan = 2
pCell.HorizontalAlignment = Element.ALIGN_LEFT
pTabela.AddCell(pCell)
'ADICIONA NÚMERO DO DOCUMENTO
pCell = New PdfPCell(New iTextSharp.text.Phrase("C.I Nº: " & pNumDoc))
pCell.Colspan = 2
pCell.HorizontalAlignment = Element.ALIGN_RIGHT
pCell.VerticalAlignment = PdfPCell.ALIGN_BOTTOM
pCell.Border = iTextSharp.text.Rectangle.NO_BORDER
pCell.FixedHeight = 20
pTabela.AddCell(pCell)
'ADICIONA DATA ATUAL
pCell = New PdfPCell(New iTextSharp.text.Phrase(pDataAtual))
pCell.Colspan = 2
pCell.HorizontalAlignment = Element.ALIGN_RIGHT
pCell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE
pCell.Border = iTextSharp.text.Rectangle.NO_BORDER
pCell.FixedHeight = 20
pTabela.AddCell(pCell)
'ADICIONA CONTEÚDO
Dim vlConteudo As String = Server.HtmlDecode(pConteudo)
Dim phraseDados As Phrase = Nothing 'Texto
Dim htmlarraylist As ArrayList = HTMLWorker.ParseToList(New StringReader(vlConteudo), Nothing)
phraseDados = New Phrase()
For k As Integer = 0 To htmlarraylist.Count - 1
phraseDados.Add(CType(htmlarraylist(k), IElement))
Next
pDoc.Add(New Paragraph(" "))
' INICIA MONTAGEM DO DOCUMENTO
pDoc.Add(pTabela)
pDoc.Add(New Paragraph(phraseDados))
pDoc.Close()
If File.Exists(pCaminho + "/" & pUsuario & ".pdf") Then
hlDownload.NavigateUrl = "pdfs/" & Usuario() & ".pdf"
hlDownload.Visible = True
End If
End Sub
Private Sub MeuPDF()
Dim pCaminho As String = Server.MapPath("pdfs")
Dim pDoc As New Document(iTextSharp.text.PageSize.A4, 20, 20, 20, 20)
Dim pFonte1 As Font = FontFactory.GetFont("Arial", 9.0F, 0, Color.BLACK)
If File.Exists(pCaminho + "/" & Session("sUsuario") & ".pdf") Then
File.Delete(pCaminho + "/" & Session("sUsuario") & ".pdf")
End If
PdfWriter.GetInstance(pDoc, New FileStream(pCaminho + "/" & Session("sUsuario") & ".pdf", FileMode.Create))
pDoc.Open()
'CRIAR IMAGEM
Dim scale As Single = 0
Dim pImagem As String = Session("sDominio") & "/documentos/imagens/logo_centec.png"
Dim pPNG As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(pImagem)
pPNG.ScaleAbsolute(120.0F, 50.0F)
pDoc.Add(pPNG)
'CRIAR TABELA
Dim pTabela As New PdfPTable(3)
Dim pCell As New PdfPCell(New Phrase(""))
pDoc.Close()
If File.Exists(pCaminho + "/" & Session("sUsuario") & ".pdf") Then
hlDownload.NavigateUrl = "pdfs/" & Usuario() & ".pdf"
hlDownload.Visible = True
End If
End Sub
Private Sub ci_relatorios_relDocPDF_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
RecebeParametros()
Consulta()
End If
End Sub
Protected Sub BtnVoltar_Click(sender As Object, e As EventArgs) Handles BtnVoltar.Click
Response.Redirect(Session("sDominio") & "/ci/consDocumento.aspx")
End Sub
End Class
Grateful, Ilano