1

我需要在现有 pdf 中添加一个锚点,以便在单击时将用户返回到页面顶部。我试过这个:

        public void Bookmark() {
        PdfReader reader = new PdfReader(new RandomAccessFileOrArray(_fileName), null);
        DeleteFile(); //delete old pdf (_filename)

        _fileName = Setting.Temp + Guid.NewGuid() + ".pdf";

        Document doc = new Document(PageSize.A4, 0, 0, 50, 50);
        PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(_fileName, FileMode.Create));

        iTextSharp.text.Font link = FontFactory.GetFont("Arial", 12, BaseColor.BLUE);

        doc.Open();

        PdfContentByte pdfContentByte = writer.DirectContent;
        Anchor topAnchor = null;
        PdfImportedPage page = null;
        for (int i = 1; i < reader.NumberOfPages; i++) {
            if (i == 1) {
                topAnchor = new Anchor("");
                topAnchor.Name = "TopAnchor";
                page = writer.GetImportedPage(reader, i);
                doc.NewPage();
                pdfContentByte.AddTemplate(page, 0, 0);
                doc.Add(topAnchor);
            } else {
                page = writer.GetImportedPage(reader, i);
                doc.NewPage();
                pdfContentByte.AddTemplate(page, 0, 0);

                Anchor anchor = new Anchor("Back to Top", link);
                anchor.Reference = "#TopAnchor";
                doc.Add(anchor);
            }

        }


        doc.Close();
    }

该链接将添加到除第一个页面之外的所有页面。但是,当我单击链接时,它什么也不做。

有谁知道如何解决这个问题或更好的方法来实现我的目标?

4

1 回答 1

1

Hi you can use the exact way followed here.

http://www.mikesdotnetting.com/Article/84/iTextSharp-Links-and-Bookmarks

于 2012-12-19T10:20:44.320 回答