Hyperlink at footer using itextSharp

前提是你 提交于 2019-12-25 02:57:48

问题


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);

回答1:


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();

    }
}


来源:https://stackoverflow.com/questions/15943417/hyperlink-at-footer-using-itextsharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!