Hi comunidadmexi...,
Refer below modified code.
Namespaces
C#
using System.Data;
using System.Linq;
using iText.Html2pdf;
using iText.Kernel.Colors;
using iText.Kernel.Events;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Xobject;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Layout;
using iText.Layout.Properties;
using iText.Layout.Renderer;
VB.Net
Imports System.Data
Imports System.Linq
Imports iText.Html2pdf
Imports iText.Kernel.Colors
Imports iText.Kernel.Events
Imports iText.Kernel.Geom
Imports iText.Kernel.Pdf
Imports iText.Kernel.Pdf.Canvas
Imports iText.Kernel.Pdf.Xobject
Imports iText.Layout
Imports iText.Layout.Element
Imports iText.Layout.Layout
Imports iText.Layout.Properties
Imports iText.Layout.Renderer
Code
C#
string filename;
string html;
string dest;
Paragraph contents;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("sID"),
new DataColumn("sUnity"),
new DataColumn("contents")
});
dt.Rows.Add(83, "Q400", "Set n.1");
dt.Rows.Add(84, "Q400", "- Par 1.1");
dt.Rows.Add(85, "Q400", "<b>bold text</b>");
dt.Rows.Add(86, "Q400", "- Par 1.2");
dt.Rows.Add(87, "Q400", "normal text");
dt.Rows.Add(88, "Q400", "Set n.2");
dt.Rows.Add(89, "Q400", "- Par 2.1");
dt.Rows.Add(90, "Q400", "<i>italic text</i>");
dt.Rows.Add(91, "Q400", "- Par 2.2");
dt.Rows.Add(92, "Q400", "<u>underline text</u>");
dt.Rows.Add(93, "Q400", "- Par 2.3");
dt.Rows.Add(94, "Q400", "Set n.3");
dt.Rows.Add(95, "Q400", "Set n.4");
dt.Rows.Add(96, "Q400", "Set n.5");
dt.Rows.Add(97, "Q400", "Set n.6");
dt.Rows.Add(98, "Q400", "Set n.7");
dt.Rows.Add(99, "Q400", "Set n.8");
dt.Rows.Add(100, "Q400", "Set n.9");
filename = @"C:\Users\developer4\Desktop\Management_" + Guid.NewGuid() + ".pdf";
PdfWriter writer = new PdfWriter(filename);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf, PageSize.A4, false);
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
contents = new Paragraph(dt.Rows[i]["contents"].ToString())
.SetTextAlignment(TextAlignment.JUSTIFIED).SetFontSize(12);
if (dt.Rows[i]["contents"].ToString().StartsWith("Set ") && i != 0)
{
document.Add(new AreaBreak(AreaBreakType.NEXT_PAGE));
}
IElement lst = HtmlConverter.ConvertToElements(dt.Rows[i]["contents"].ToString()).FirstOrDefault();
IBlockElement element = (IBlockElement)lst;
if (dt.Rows[i]["contents"].ToString().StartsWith("Set"))
{
contents.SetFontSize(12)
.SetBold()
.SetFontColor(ColorConstants.BLUE);
}
else if (dt.Rows[i]["contents"].ToString().StartsWith("- "))
{
contents.SetFontSize(11)
.SetBold()
.SetFontColor(ColorConstants.BLACK);
}
else
{
contents.SetTextAlignment(TextAlignment.JUSTIFIED_ALL)
.SetFontSize(10)
.SetFontColor(ColorConstants.BLACK);
}
document.Add(element);
}
dest = filename.ToString();
}
else
{
Console.WriteLine("No rows found.");
}
Footer footerHandler = new Footer();
pdf.AddEventHandler(PdfDocumentEvent.END_PAGE, footerHandler);
footerHandler.WriteTotal(pdf);
document.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + dest);
Response.TransmitFile(dest);
Response.End();
}
}
private class TableFooterEventHandler : IEventHandler
{
private iText.Layout.Element.Table table;
public TableFooterEventHandler(iText.Layout.Element.Table table)
{
this.table = table;
}
public void HandleEvent(Event currentEvent)
{
PdfDocumentEvent docEvent = (PdfDocumentEvent)currentEvent;
PdfDocument pdfDoc = docEvent.GetDocument();
PdfPage page = docEvent.GetPage();
int pageNumber = pdfDoc.GetPageNumber(page);
Rectangle pageSize = page.GetPageSize();
PdfCanvas canvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdfDoc);
new Canvas(canvas, new Rectangle(36, 20, page.GetPageSize().GetWidth() - 72, 50)).Add(table).Close();
}
}
protected class Footer : IEventHandler
{
protected PdfFormXObject placeholder;
protected float side = 20;
protected float x = 300;
protected float y = 25;
protected float space = 4.5f;
protected float descent = 3;
public Footer()
{
placeholder = new PdfFormXObject(new Rectangle(0, 0, side, side));
}
public virtual void HandleEvent(Event @event)
{
PdfDocumentEvent docEvent = (PdfDocumentEvent)@event;
PdfDocument pdf = docEvent.GetDocument();
PdfPage page = docEvent.GetPage();
int pageNumber = pdf.GetPageNumber(page);
Rectangle pageSize = page.GetPageSize();
PdfCanvas pdfCanvas = new PdfCanvas(page);
Canvas canvas = new Canvas(pdfCanvas, pageSize);
canvas.SetFontSize(10);
Paragraph p = new Paragraph().Add("Page ").Add(pageNumber.ToString()).Add(" of");
canvas.ShowTextAligned(p, x, y, TextAlignment.RIGHT);
canvas.Close();
pdfCanvas.AddXObjectAt(placeholder, x + space, y - descent);
pdfCanvas.Release();
}
public void WriteTotal(PdfDocument pdfDoc)
{
Canvas canvas = new Canvas(placeholder, pdfDoc);
canvas.SetFontSize(10);
canvas.ShowTextAligned(pdfDoc.GetNumberOfPages().ToString(), 0, descent, TextAlignment.LEFT);
canvas.Close();
}
}
private class TableHeaderEventHandler : IEventHandler
{
private iText.Layout.Element.Table table;
private float tableHeight;
private Document doc;
public TableHeaderEventHandler(Document doc)
{
this.doc = doc;
InitTable();
TableRenderer renderer = (TableRenderer)table.CreateRendererSubTree();
renderer.SetParent(new DocumentRenderer(doc));
LayoutResult result = renderer.Layout(new LayoutContext(new LayoutArea(0, PageSize.A4)));
tableHeight = result.GetOccupiedArea().GetBBox().GetHeight();
}
public void HandleEvent(Event currentEvent)
{
PdfDocumentEvent docEvent = (PdfDocumentEvent)currentEvent;
PdfDocument pdfDoc = docEvent.GetDocument();
PdfPage page = docEvent.GetPage();
int pageNum = docEvent.GetDocument().GetPageNumber(page);
PdfCanvas canvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdfDoc);
PageSize pageSize = pdfDoc.GetDefaultPageSize();
float coordX = pageSize.GetX() + doc.GetLeftMargin();
float coordY = pageSize.GetTop() - doc.GetTopMargin();
float width = pageSize.GetWidth() - doc.GetRightMargin() - doc.GetLeftMargin();
float height = GetTableHeight();
Rectangle rect = new Rectangle(coordX, coordY, width, height);
new Canvas(canvas, rect).Add(table).Close();
}
public float GetTableHeight()
{
return tableHeight;
}
private void InitTable()
{
}
}
VB.Net
Private filename As String
Private html As String
Private dest As String
Private contents As Paragraph
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("sID"), New DataColumn("sUnity"), New DataColumn("contents")})
dt.Rows.Add(83, "Q400", "Set n.1")
dt.Rows.Add(84, "Q400", "- Par 1.1")
dt.Rows.Add(85, "Q400", "<b>bold text</b>")
dt.Rows.Add(86, "Q400", "- Par 1.2")
dt.Rows.Add(87, "Q400", "normal text")
dt.Rows.Add(88, "Q400", "Set n.2")
dt.Rows.Add(89, "Q400", "- Par 2.1")
dt.Rows.Add(90, "Q400", "<i>italic text</i>")
dt.Rows.Add(91, "Q400", "- Par 2.2")
dt.Rows.Add(92, "Q400", "<u>underline text</u>")
dt.Rows.Add(93, "Q400", "- Par 2.3")
dt.Rows.Add(94, "Q400", "Set n.3")
dt.Rows.Add(95, "Q400", "Set n.4")
dt.Rows.Add(96, "Q400", "Set n.5")
dt.Rows.Add(97, "Q400", "Set n.6")
dt.Rows.Add(98, "Q400", "Set n.7")
dt.Rows.Add(99, "Q400", "Set n.8")
dt.Rows.Add(100, "Q400", "Set n.9")
filename = "C:\Users\developer4\Desktop\Management_" & Guid.NewGuid() & ".pdf"
Dim writer As PdfWriter = New PdfWriter(filename)
Dim pdf As PdfDocument = New PdfDocument(writer)
Dim document As Document = New Document(pdf, PageSize.A4, False)
If dt.Rows.Count > 0 Then
For i As Integer = 0 To dt.Rows.Count - 1
contents = New Paragraph(dt.Rows(i)("contents").ToString()).SetTextAlignment(TextAlignment.JUSTIFIED).SetFontSize(12)
If dt.Rows(i)("contents").ToString().StartsWith("Set ") AndAlso i <> 0 Then
document.Add(New AreaBreak(AreaBreakType.NEXT_PAGE))
End If
Dim lst As IElement = HtmlConverter.ConvertToElements(dt.Rows(i)("contents").ToString()).FirstOrDefault()
Dim element As IBlockElement = CType(lst, IBlockElement)
If dt.Rows(i)("contents").ToString().StartsWith("Set") Then
contents.SetFontSize(12).SetBold().SetFontColor(ColorConstants.BLUE)
ElseIf dt.Rows(i)("contents").ToString().StartsWith("- ") Then
contents.SetFontSize(11).SetBold().SetFontColor(ColorConstants.BLACK)
Else
contents.SetTextAlignment(TextAlignment.JUSTIFIED_ALL).SetFontSize(10).SetFontColor(ColorConstants.BLACK)
End If
document.Add(element)
Next
dest = filename.ToString()
Else
Console.WriteLine("No rows found.")
End If
Dim footerHandler As Footer = New Footer()
pdf.AddEventHandler(PdfDocumentEvent.END_PAGE, footerHandler)
footerHandler.WriteTotal(pdf)
document.Close()
Response.Clear()
Response.ContentType = "application/pdf"
Response.AppendHeader("Content-Disposition", "attachment; filename=" & dest)
Response.TransmitFile(dest)
Response.End()
End If
End Sub
Private Class TableFooterEventHandler
Inherits IEventHandler
Private table As iText.Layout.Element.Table
Public Sub New(ByVal table As iText.Layout.Element.Table)
Me.table = table
End Sub
Public Sub HandleEvent(ByVal currentEvent As [Event])
Dim docEvent As PdfDocumentEvent = CType(currentEvent, PdfDocumentEvent)
Dim pdfDoc As PdfDocument = docEvent.GetDocument()
Dim page As PdfPage = docEvent.GetPage()
Dim pageNumber As Integer = pdfDoc.GetPageNumber(page)
Dim pageSize As Rectangle = page.GetPageSize()
Dim canvas As PdfCanvas = New PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdfDoc)
New Canvas(canvas, New Rectangle(36, 20, page.GetPageSize().GetWidth() - 72, 50)).Add(table).Close()
End Sub
End Class
Protected Class Footer
Inherits IEventHandler
Protected placeholder As PdfFormXObject
Protected side As Single = 20
Protected x As Single = 300
Protected y As Single = 25
Protected space As Single = 4.5F
Protected descent As Single = 3
Public Sub New()
placeholder = New PdfFormXObject(New Rectangle(0, 0, side, side))
End Sub
Public Overridable Sub HandleEvent(ByVal [event] As [Event])
Dim docEvent As PdfDocumentEvent = CType([event], PdfDocumentEvent)
Dim pdf As PdfDocument = docEvent.GetDocument()
Dim page As PdfPage = docEvent.GetPage()
Dim pageNumber As Integer = pdf.GetPageNumber(page)
Dim pageSize As Rectangle = page.GetPageSize()
Dim pdfCanvas As PdfCanvas = New PdfCanvas(page)
Dim canvas As Canvas = New Canvas(pdfCanvas, pageSize)
canvas.SetFontSize(10)
Dim p As Paragraph = New Paragraph().Add("Page ").Add(pageNumber.ToString()).Add(" of")
canvas.ShowTextAligned(p, x, y, TextAlignment.RIGHT)
canvas.Close()
pdfCanvas.AddXObjectAt(placeholder, x + space, y - descent)
pdfCanvas.Release()
End Sub
Public Sub WriteTotal(ByVal pdfDoc As PdfDocument)
Dim canvas As Canvas = New Canvas(placeholder, pdfDoc)
canvas.SetFontSize(10)
canvas.ShowTextAligned(pdfDoc.GetNumberOfPages().ToString(), 0, descent, TextAlignment.LEFT)
canvas.Close()
End Sub
End Class
Private Class TableHeaderEventHandler
Inherits IEventHandler
Private table As iText.Layout.Element.Table
Private tableHeight As Single
Private doc As Document
Public Sub New(ByVal doc As Document)
Me.doc = doc
InitTable()
Dim renderer As TableRenderer = CType(table.CreateRendererSubTree(), TableRenderer)
renderer.SetParent(New DocumentRenderer(doc))
Dim result As LayoutResult = renderer.Layout(New LayoutContext(New LayoutArea(0, PageSize.A4)))
tableHeight = result.GetOccupiedArea().GetBBox().GetHeight()
End Sub
Public Sub HandleEvent(ByVal currentEvent As [Event])
Dim docEvent As PdfDocumentEvent = CType(currentEvent, PdfDocumentEvent)
Dim pdfDoc As PdfDocument = docEvent.GetDocument()
Dim page As PdfPage = docEvent.GetPage()
Dim pageNum As Integer = docEvent.GetDocument().GetPageNumber(page)
Dim canvas As PdfCanvas = New PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdfDoc)
Dim pageSize As PageSize = pdfDoc.GetDefaultPageSize()
Dim coordX As Single = pageSize.GetX() + doc.GetLeftMargin()
Dim coordY As Single = pageSize.GetTop() - doc.GetTopMargin()
Dim width As Single = pageSize.GetWidth() - doc.GetRightMargin() - doc.GetLeftMargin()
Dim height As Single = GetTableHeight()
Dim rect As Rectangle = New Rectangle(coordX, coordY, width, height)
New Canvas(canvas, rect).Add(table).Close()
End Sub
Public Function GetTableHeight() As Single
Return tableHeight
End Function
Private Sub InitTable()
End Sub
End Class