You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用iText为PDF添加可跳页点击索引时链接失效问题求解

问题原因

你当前跳转失效的核心问题是使用了错误的跳转目标类型:

  • PdfExplicitRemoteGoToDestination 是用于跳转至外部PDF文件的目标类,不适用当前文档内部跳转
  • 另外常量类存在笔误:第二个索引页循环中调用的Constants.abajoInicial没有定义,会导致编译报错或坐标错误,文字渲染异常也会让你误以为跳转失效

解决方案

1. 修改跳转方法

将PDF类中的CreateClicableParagraph方法替换为以下代码,使用适用于当前文档跳转的PdfExplicitDestination:

public static void CreateClicableParagraph(ref Paragraph p, int page)
{
    // 替换为当前文档内部跳转的目标类型
    p.SetAction(iText.Kernel.Pdf.Action.PdfAction.CreateGoTo(iText.Kernel.Pdf.Navigation.PdfExplicitDestination.CreateFitB(page)));
}

2. 修正常量笔误

将第二个索引页循环的bottom赋值行修改为你已定义的常量:

// 原错误代码 bottom = Constants.abajoInicial;
bottom = Constants.initialBottom;

补充说明

iText的页码从1开始计数,你传入的跳转页码需要和PDF实际页码保持一致即可正常跳转。

内容的提问来源于stack exchange,提问作者negu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.15 05:45:00