Steven Yong
Steven Yong

Reputation: 5446

Hyperlink at footer using itextSharp

I need to put a hyperlink at the footer of my PDF generated using iTextSharp.

I know how to use PdfPageEventHelper to print some text in the footer but not putting a hyperlink.

    public class PdfHandlerEvents: PdfPageEventHelper
    {
        private PdfContentByte _cb;
        private BaseFont _bf;

        public override void OnOpenDocument(PdfWriter writer, Document document)
        {
            _cb = writer.DirectContent;
        }

        public override void OnEndPage(PdfWriter writer, Document document)
        {
            base.OnEndPage(writer, document);

            _bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            Rectangle pageSize = document.PageSize;

            _cb.SetRGBColorFill(100, 100, 100);

            _cb.BeginText();
            _cb.SetFontAndSize(_bf, 10);
            _cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "More information", pageSize.GetRight(200), pageSize.GetBottom(30), 0);
            _cb.EndText();
        }
    } 

How do I make the text "More information" a hyperlink?

Edited:

After the answer from Chris below, I have also figure out how to print image at the footer, here is the code:

            Image pic = Image.GetInstance(@"C:\someimage.jpg");
            pic.SetAbsolutePosition(0, 0);
            pic.ScalePercent(25);

            PdfTemplate tpl = _cb.CreateTemplate(pic.Width, pic.Height);
            tpl.AddImage(pic);
            _cb.AddTemplate(tpl, 0, 0);

Upvotes: 0

Views: 1541

Answers (1)

Chris Haas
Chris Haas

Reputation: 55447

The Document object generally lets you work with abstract things like Paragraph and Chunk but in doing so you lose absolute positioning. The PdfWriter and PdfContentByte objects give you absolute positioning but you need to work with lower level objects like raw text.

Luckily there is a happy middle-ground object called ColumnText that should do what you're looking for. You can think of the ColumnText as basically a table and most people use it as a single column table so you can actually just think of it as a rectangle that you add objects to. See the comments in the code below for any questions.

public class PdfHandlerEvents : PdfPageEventHelper {
    private PdfContentByte _cb;
    private BaseFont _bf;

    public override void OnOpenDocument(PdfWriter writer, Document document) {
        _cb = writer.DirectContent;
    }

    public override void OnEndPage(PdfWriter writer, Document document) {
        base.OnEndPage(writer, document);

        _bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        iTextSharp.text.Rectangle pageSize = document.PageSize;

        //Create our ColumnText bound to the canvas
        var ct = new ColumnText(_cb);
        //Set the dimensions of our "box"
        ct.SetSimpleColumn(pageSize.GetRight(200), pageSize.GetBottom(30), pageSize.Right, pageSize.Bottom);
        //Create a new chunk with our text and font
        var c = new Chunk("More Information", new iTextSharp.text.Font(_bf, 10));
        //Set the chunk's action to a remote URL
        c.SetAction(new PdfAction("http://www.aol.com"));
        //Add the chunk to the ColumnText
        ct.AddElement(c);
        //Tell the ColumnText to draw itself
        ct.Go();

    }
}

Upvotes: 2

Related Questions