SST
SST

Reputation: 459

PDF header doesnt get displayed Itextsharp

Hi I am writng some pdf by parsing html to it using itextsharp and it is working fine but I want to add page number on each every page of the pdf. for this I am adding header with dummy text in it(later I will replace it with page count) and writing dome content but the content is not displayed ..

try
{
     Document oNewDocument = new Document(PageSize.A4, 20f, 20f, 30f, 10f);
     PdfWriter.GetInstance(oNewDocument, new FileStream(pdfpath + "/" + sSaleInvoicePdf, FileMode.Create)); 
     string content = "Some HTML Content";
     List<IElement> parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), styles);
     oNewDocument.AddHeader("text","text");

     foreach (var htmlElement in parsedHtmlElements)
     {
          oNewDocument.Add(htmlElement as IElement);
     }
}
catch (Exception ex)
{
     Response.Write(ex.Message);
}
finally
{
     oNewDocument.Close();
}

where I am worng. this code generate all the html content but not header text..??

Upvotes: 1

Views: 2452

Answers (2)

Peter
Peter

Reputation: 27944

Version 5+ you have to do this with a page event:

Handling header footer page event, itextsharp 5+

Before version 5 it works like:

Document oNewDocument = new Document(PageSize.A4, 20f, 20f, 30f, 10f);
PdfWriter.GetInstance(oNewDocument, new FileStream(pdfpath + "/" + sSaleInvoicePdf, FileMode.Create));  

//Create some text to add to the header
Chunk text= new Chunk("my text");
Phrase phHeader = new Phrase();
phHeader.Add(text);

//Assign the Phrase to PDF Header
HeaderFooter header = new HeaderFooter(phHeader, false);

//Add the header to the document
oNewDocument.Header = header;

Upvotes: 1

Karthik
Karthik

Reputation: 2399

  HeaderFooter hdr = new HeaderFooter(stringvalue, false);
  hdr.Border = Rectangle.NO_BORDER;
  hdr.Alignment = Element.ALIGN_LEFT;
  doc.Header = hdr;

try this not sure whether your version supports this..give it a try

Upvotes: 0

Related Questions