我需要向现有的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();
}
链接将添加到除第一个页面之外的所有页面。但是,当我点击链接时,它什么也没做。
有谁知道如何解决这个或更好的方法来实现我的目标?
答案 0 :(得分:1)
您好,您可以按照此处的确切方式使用。
http://www.mikesdotnetting.com/Article/84/iTextSharp-Links-and-Bookmarks