Hi Ilanocf,
Please refer below sample.
Package
Install-Package iTextSharp-4.1.2.0 -Version 4.1.2
HTML
<asp:Label ID="LbMensagem" runat="server" />
<asp:HiddenField ID="hfArquivo" runat="server" />
Namespaces
C#
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.Collections;
using System.Data;
using System.IO;
VB.Net
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.html.simpleparser
Imports System.Collections
Imports System.Data
Imports System.IO
Code
C#
protected void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
PovoaDados();
}
private void PovoaDados()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("ID"),
new DataColumn("PESSOAID"),
new DataColumn("EDITALITEMID"),
new DataColumn("CURSOID"),
new DataColumn("CPFCNPJ"),
new DataColumn("EMENTA")
});
dt.Rows.Add("1", "1", "IT", ".Net", "ABC", "123");
DataSet Ds = new DataSet();
Ds.Tables.Add(dt);
if (Ds != null)
{
string html = " <div style="text-align: justify;"> <span style="font-family: Arial;color: red;font-weight:bold;" > Food Education in Self Care; Macronutrients and their Functions;";
html += "Chrononutrition;Food Behavior;Functional Foods;Foods in Disease Prevention;Learning to Read Labels;Preservation of Food through Bleaching.</span></div>";
if (Ds.Tables[0].Rows.Count > 0)
{
CriarRelatorio(Ds.Tables[0].Rows[0]["CPFCNPJ"].ToString(), Ds.Tables[0].Rows[0]["EMENTA"].ToString() + Server.HtmlDecode(html));
}
else if (Ds.Tables[0].Rows.Count == 0)
{
LbMensagem.Text = "<div class='alert alert-danger'>Nenhuma informação foi encontrada.</div>";
}
}
else if (Ds == null)
LbMensagem.Text = "<div class='alert alert-danger'>Nenhuma informação foi encontrada.</div>";
}
private void CriarRelatorio(string pCPF, string pDados)
{
string pCaminho;
pCaminho = Server.MapPath("pdfs");
if (File.Exists(pCaminho + "/" + pCPF + ".pdf"))
File.Delete(pCaminho + "/" + pCPF + ".pdf");
// Atribuir fontes do servidor na aplicação
FontFactory.RegisterDirectory(@"C:\WINDOWS\Fonts");
Font cellFont = FontFactory.GetFont("Arial", 16.0F, Font.NORMAL);
Font cellFont3 = FontFactory.GetFont("Arial", 9.0F, 0, Color.BLACK);
// Criar uma instância de document
Document doc = new Document(PageSize.A4, 0, 0, 0, 0);
PdfWriter.GetInstance(doc, new FileStream(pCaminho + "/" + pCPF + ".pdf", FileMode.Create));
doc.Open();
// Convert the html and add to document.
ArrayList htmlarraylist = HTMLWorker.ParseToList(new StringReader(pDados), null);
for (int k = 0; k < htmlarraylist.Count; k++)
{
doc.Add((IElement)htmlarraylist[k]);
}
doc.Close();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then PovoaDados()
End Sub
Private Sub PovoaDados()
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("ID"), New DataColumn("PESSOAID"), New DataColumn("EDITALITEMID"), New DataColumn("CURSOID"), New DataColumn("CPFCNPJ"), New DataColumn("EMENTA")})
dt.Rows.Add("1", "1", "IT", ".Net", "ABC", "123")
Dim Ds As DataSet = New DataSet()
Ds.Tables.Add(dt)
If Ds IsNot Nothing Then
Dim html As String = " <div style="text-align: justify;"> <span style="font-family: Arial;color: red;font-weight:bold;" > Food Education in Self Care; Macronutrients and their Functions;"
html += "Chrononutrition;Food Behavior;Functional Foods;Foods in Disease Prevention;Learning to Read Labels;Preservation of Food through Bleaching.</span></div>"
If Ds.Tables(0).Rows.Count > 0 Then
CriarRelatorio(Ds.Tables(0).Rows(0)("CPFCNPJ").ToString(), Ds.Tables(0).Rows(0)("EMENTA").ToString() + Server.HtmlDecode(html))
ElseIf Ds.Tables(0).Rows.Count = 0 Then
LbMensagem.Text = "<div class='alert alert-danger'>Nenhuma informação foi encontrada.</div>"
End If
ElseIf Ds Is Nothing Then
LbMensagem.Text = "<div class='alert alert-danger'>Nenhuma informação foi encontrada.</div>"
End If
End Sub
Private Sub CriarRelatorio(ByVal pCPF As String, ByVal pDados As String)
Dim pCaminho As String
pCaminho = Server.MapPath("pdfs")
If File.Exists(pCaminho & "/" & pCPF & ".pdf") Then File.Delete(pCaminho & "/" & pCPF & ".pdf")
FontFactory.RegisterDirectory("C:\WINDOWS\Fonts")
Dim cellFont As Font = FontFactory.GetFont("Arial", 16.0F, Font.NORMAL)
Dim cellFont3 As Font = FontFactory.GetFont("Arial", 9.0F, 0, Color.BLACK)
Dim doc As Document = New Document(PageSize.A4, 0, 0, 0, 0)
PdfWriter.GetInstance(doc, New FileStream(pCaminho & "/" & pCPF & ".pdf", FileMode.Create))
doc.Open()
Dim htmlarraylist As ArrayList = HTMLWorker.ParseToList(New StringReader(pDados), Nothing)
For k As Integer = 0 To htmlarraylist.Count - 1
doc.Add(CType(htmlarraylist(k), IElement))
Next
doc.Close()
End Sub
Screenshot