使用itextSharp在页脚处进行超链接 [英] Hyperlink at footer using itextSharp

查看:78
本文介绍了使用itextSharp在页脚处进行超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在使用iTextSharp生成的PDF的页脚处放置一个超链接.

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

我知道如何使用PdfPageEventHelper在页脚中打印一些文本,但不放置超链接.

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?

在下面克里斯的回答之后,我还想出了如何在页脚处打印图像,这是代码:

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

推荐答案

Document对象通常使您可以处理诸如ParagraphChunk之类的抽象内容,但是这样做会失去绝对定位. PdfWriterPdfContentByte对象为您提供了绝对的定位,但是您需要使用诸如原始文本之类的较低级别的对象.

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.

幸运的是,有一个名为ColumnText的快乐的地面物体可以完成您要寻找的东西.您可以将ColumnText基本上视为一个表,并且大多数人将其用作单列表,因此实际上您可以将其视为将对象添加到的矩形.如有任何疑问,请参见下面代码中的注释.

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

    }
}

这篇关于使用itextSharp在页脚处进行超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆