views:

1267

answers:

1

Hello!

I'm wondering how can I put a header into my pdf file, cause i've tried the tutorials from here:

http://itextsharp.sourceforge.net/tutorial/ch04.html

And it have not worked.

Ive done this:

Dim head As New HeaderFooter(New Phrase("This is page: "), False) head.Border = Rectangle.NO_BORDER document.Header = head

But VS2008 says that HeaderFooter it's not defined (line 1), and Footer it's not a member of "iTextSharp.text.document" (line 3).

I've already included the imports at the begining of my code and i don't have any other problems with the iTextsharps (i mean that it is working apart of the header problem):

Imports iTextSharp.text Imports iTextSharp.text.pdf Imports System.Data.SQLite Imports System.IO

So please, can anyone explain to me how can i set a header for my pages?

Regards

+1  A: 

Hi,

The answer to this question depends on which version of the iTextSharp dll you are using.

If you are using a version lower than 5, this should work

Imports iTextSharp.text.pdf
Imports iTextSharp.text
Module Module1
    Sub Main()
        Dim pdfWrite As PdfWriter
        Dim pdfDoc As New Document()
        Dim pdfMemoryStream As New IO.FileStream("tryme.pdf", IO.FileMode.Create)
        pdfWrite = PdfWriter.GetInstance(pdfDoc, pdfMemoryStream)
        Dim pdfHeader As New HeaderFooter(New Phrase("Im at the head: "), False)
        pdfHeader.Border = Rectangle.NO_BORDER
        pdfDoc.Header = pdfHeader
        pdfDoc.Open()
        pdfDoc.Add(New Paragraph("Hello World"))
        pdfDoc.NewPage()
        pdfDoc.Add(New Paragraph("Hello World Again"))
        pdfDoc.Close()
    End Sub
End Module

Update

For version 5+ of iTextSharp the HeaderFooter property has been removed from iTextSharp. To add Headers/Footers now you must use PageEvents. The following code demonstrates how to do this (very simply!)

Imports iTextSharp.text.pdf
Imports iTextSharp.text
Imports System.IO
Module Module1
    Sub Main()
        Dim pdfDoc As New Document()
        Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(pdfDoc, New FileStream("tryme2.pdf", FileMode.Create))
        Dim ev As New itsEvents
        pdfWrite.PageEvent = ev
        pdfDoc.Open()
        pdfDoc.Add(New Paragraph("Hello World"))
        pdfDoc.NewPage()
        pdfDoc.Add(New Paragraph("Hello World Again"))
        pdfDoc.Close()
    End Sub
End Module

Public Class itsEvents
    Inherits PdfPageEventHelper
    Public Overrides Sub OnStartPage(ByVal writer As iTextSharp.text.pdf.PdfWriter, ByVal document As iTextSharp.text.Document)
        Dim ch As New Chunk("This is my Stack Overflow Header on page " & writer.PageNumber)
        document.Add(ch)
    End Sub
End Class
CResults
HiI've tried your code and it hasn't worked for me. It can't find the HeaderFooter class wich I think it was removed from the current version of itextsharp (5.0.1)Any other chance to get this to work?Thank you
bpSz
That updated code worked perfect for me, btw do you know how to add a table at the Header/Footer?Thank you very much for this!!
bpSz
I think they removed header/footers in v5 as they were restrictive in that you couldn't add much more than basic text chunks to them. Using the OnStartPage event you should be able to add anything you want to the document.
CResults
ok! Thank you again, sir! that was very useful!
bpSz