below is my method to generate pdf,
protected void startpdfgeneration()
{
try
{
pdfDoc = new Document(PageSize.A4, 10f, 10f, 45f, 25f);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);
writer.SetLinearPageMode();
ITextEvents evento = new ITextEvents();
writer.PageEvent = evento;
PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, pdfDoc.PageSize.Height, 1f);
pdfDoc.Open();
PdfAction action = PdfAction.GotoLocalPage(1, pdfDest, writer);
writer.SetOpenAction(action);
pdfDoc.NewPage();
PdfPTable table = new PdfPTable(1);
PdfPCell cell = new PdfPCell();
cell.BorderColor = BaseColor.WHITE;
Paragraph toc = new Paragraph("Table Of Contents", tocFont);
toc.Alignment = Element.ALIGN_CENTER;
cell.AddElement(toc);
table.AddCell(cell);
pdfDoc.Add(table);
Chunk dottedLine = new Chunk(new DottedLineSeparator());
List<PageIndex> entries = evento.getTOC();
Paragraph p;
foreach (PageIndex pageIndex in entries)
{
Chunk chunk = new Chunk(pageIndex.Text);
chunk.SetAction(PdfAction.GotoLocalPage(pageIndex.Name, false));
p = new Paragraph(chunk);
p.Add(dottedLine);
chunk = new Chunk(pageIndex.Page.ToString());
chunk.SetAction(PdfAction.GotoLocalPage(pageIndex.Name, false));
p.Add(chunk);
pdfDoc.Add(p);
}
pdfDoc.Close();
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename="+txtNumber.Text.Trim()+".pdf");
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Write(pdfDoc);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.SuppressContent = true;
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
catch (Exception ex)
{
}
}
Below is my pageeventhandler class for header, footer and toc
public class ITextEvents : PdfPageEventHelper
{
protected int counter = 0;
protected List<PageIndex> toc = new List<PageIndex>();
PdfContentByte cb;
PdfTemplate headerTemplate, footerTemplate;
BaseFont bf = null;
#region Fields
private string _header;
#endregion
#region Properties
public string Header
{
get { return _header; }
set { _header = value; }
}
#endregion
public override void OnGenericTag(PdfWriter writer, Document document, Rectangle rect, string text)
{
String name = "dest" + (counter++);
int page = writer.PageNumber;
toc.Add(new PageIndex() { Text = text, Name = name, Page = page });
writer.DirectContent.LocalDestination(name, new PdfDestination(PdfDestination.FITH, rect.GetTop(0)));
}
public List<PageIndex> getTOC()
{
return toc;
}
public override void OnOpenDocument(PdfWriter writer, Document document)
{
try
{
bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb = writer.DirectContent;
headerTemplate = cb.CreateTemplate(100, 100);
footerTemplate = cb.CreateTemplate(50, 50);
}
catch (DocumentException de)
{
}
catch (System.IO.IOException ioe)
{
}
}
public override void OnEndPage(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document)
{
base.OnEndPage(writer, document);
iTextSharp.text.Font baseFontNormal = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 8f, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK);
iTextSharp.text.Font baseFontBig = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12f, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.BLACK);
PdfPTable pdfTab = new PdfPTable(2);
PdfPCell pdfCell1 = new PdfPCell(new Phrase(string.Empty, baseFontNormal));
Image logoimg = Image.GetInstance(System.Web.HttpContext.Current.Server.MapPath("logo.png"));
logoimg.ScaleToFit(105f, 33f);
PdfPCell pdfCell2 = new PdfPCell(logoimg);
String text = "Page " + writer.PageNumber + " of ";
{
cb.BeginText();
cb.SetFontAndSize(bf, 8);
cb.SetTextMatrix(document.PageSize.GetRight(100), document.PageSize.GetBottom(10));
cb.ShowText(text);
cb.EndText();
float len = bf.GetWidthPoint(text, 8);
cb.AddTemplate(footerTemplate, document.PageSize.GetRight(100) + len, document.PageSize.GetBottom(10));
}
pdfCell1.HorizontalAlignment = Element.ALIGN_LEFT;
pdfCell2.HorizontalAlignment = Element.ALIGN_RIGHT;
pdfCell1.Border = 0;
pdfCell2.Border = 0;
pdfTab.AddCell(pdfCell1);
pdfTab.AddCell(pdfCell2);
pdfTab.TotalWidth = document.PageSize.Width-8;
pdfTab.WriteSelectedRows(0, -1, 5, document.PageSize.Height-5, writer.DirectContent);
}
public override void OnCloseDocument(PdfWriter writer, Document document)
{
base.OnCloseDocument(writer, document);
headerTemplate.BeginText();
headerTemplate.SetFontAndSize(bf, 8);
headerTemplate.SetTextMatrix(0, 0);
headerTemplate.ShowText((writer.PageNumber - 1).ToString());
headerTemplate.EndText();
footerTemplate.BeginText();
footerTemplate.SetFontAndSize(bf, 8);
footerTemplate.SetTextMatrix(0, 0);
footerTemplate.ShowText((writer.PageNumber).ToString());
footerTemplate.EndText();
}
}
public class PageIndex
{
public string Text { get; set; }
public string Name { get; set; }
public int Page { get; set; }
}
}
TOC is getting generated and coming in last page. I want to reorder it to first page.
What I have tried:
I have no idea to try. Everywhere i see is itext7 but i am using itextsharp.