I know how to target any text of any PDF page using code:

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

My question is how to target a page based on its number. For example if targeted page number is 6, clicking on the Anchor text should take to 6th page.

有帮助吗?

解决方案

Instead of an Anchor, you need a Chunk. To this Chunk you need to add a PdfAction. The action needs to be a gotoLocalPage() action.

For instance:

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

其他提示

        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();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top