IText学习手册——Anchor

Anchor,相当于html中的超链接,主要实现2个功能:

1.跳转到外部站点。

2.跳转到文档的特定位置。

跳转到特定网站,只需要设置Anchor对象的Reference属性

调转到文档的特定位置,先要创建一个Anchor对象到特定位置,并为其命名,如“China”,然后,在创建一个Anchor至于需要点击的位置,设置其Reference属性为“#China”

/// <summary>
        /// Anchor
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button22_Click(object sender, EventArgs e)
        {
            Document doc = new Document();
            PdfWriter.GetInstance(doc, File.Open(path, FileMode.Create));
            doc.Open();
            //设置一个坐标点,用于goTop定位
            Anchor topLine = new iTextSharp.text.Anchor("This name is US");
            topLine.Name = "US";
            doc.Add(topLine);

            doc.Add(Chunk.NEWLINE);
            //链接到百度
            Anchor anchor = new iTextSharp.text.Anchor("This is Anchor");
            anchor.Reference = "http://www.baidu.com";
            doc.Add(anchor);



            for (var i = 0; i < 100; i++)
            {
                doc.Add(new Paragraph("This is " + (i + 1) + "line"));
            }


            //点击后,跳到topLine的位置
            Anchor goTop = new iTextSharp.text.Anchor("Go Top");
            goTop.Reference = "#US";
            doc.Add(goTop);

            doc.Close();
        }

显示效果:

image

posted @ 2014-02-27 14:56  争世不悔  阅读(936)  评论(0编辑  收藏  举报