iTextSharp - 如何定位PDF页码

时间:2014-02-20 12:01:08

标签: c# pdf pdf-generation itextsharp

我知道如何使用代码定位任何PDF页面的任何文本:

    Anchor click = new Anchor("Click to go to Target");
    click.Reference = "#target";
    Paragraph p1 = new Paragraph();
    p1.Add(click);
    doc.Add(p1);

    Anchor target = new Anchor("Target");
    target.Name = "target";
    doc.Add(target);

我的问题是如何根据网页的编号定位网页。例如,如果目标页码为6,则单击“锚点”文本应该是第6页。

2 个答案:

答案 0 :(得分:2)

而不是Anchor,您需要Chunk。对于此Chunk,您需要添加PdfAction。该操作需要gotoLocalPage()操作。

例如:

Chunk chunk = New Chunk("Go to page 5");
PdfAction action = PdfAction.GotoLocalPage(5, New PdfDestination(0), writer);
chunk.SetAction(action);

答案 1 :(得分:0)

        iTextSharp.text.Document doc = new iTextSharp.text.Document();
        Chunk chunk = new Chunk("Go to page 5");
        var writer = PdfWriter.GetInstance(doc, new FileStream(highLightFile, FileMode.Create));
        var des = new PdfDestination(0,10f);
        PdfAction action = PdfAction.GotoLocalPage(5, des, writer);
        doc.Open();
        chunk.SetAction(action);
        Paragraph p1 = new Paragraph();
        p1.Add(chunk);
        doc.Add(p1);
        doc.Close();