【问题标题】:itext PDF link doesn't work in Microsoft Edgeitext PDF 链接在 Microsoft Edge 中不起作用
【发布时间】:2019-04-18 17:20:05
【问题描述】:

我正在从 pdf 文档(使用 itext 创建)中添加指向文件的链接 这样:

Chunk chunk = new Chunk(fileName, font);
chunk.SetAnchor("./relative/path/to/file"); 

如果我在 Google Chrome 或 Adob​​e 阅读器中打开文档,链接效果很好。 但如果我在 Microsoft Edge 中打开我的 PDF,它就不起作用。

是否甚至可以在 pdf 中创建一个在 Microsoft Edge 中可以使用的带有 itext 的文件链接?如果是,那么如何?

【问题讨论】:

  • 我刚刚测试了一下。似乎 Edge 在这里不接受相对 URI,只接受完整的绝对 URI,包括协议,例如"file:///C:/Users/mkl/Documents/test.png"

标签: c# pdf hyperlink itext microsoft-edge


【解决方案1】:

是否可以在 pdf 中创建一个在 Microsoft Edge 中可以使用的带有 itext 的文件链接?

如果是,那怎么办?

经过一些测试,Edge 似乎不支持 PDF 文档中的相对链接。

它确实支持绝对链接,但是,给定完整的 URI,例如

chunk = new Chunk("Only ASCII chars in target. Full path.");
chunk.SetAnchor("file:///C:/Repo/GitHub/testarea/itext5/target/test-outputs/annotate/Attachments/1.png");
doc.Add(new Paragraph(chunk));

与其他 PDF 查看器(Adobe Reader、Chrome,在此上下文中参见 your previous question)相比,它确实不支持像西里尔字母这样的特殊字符的 URL 编码:

chunk = new Chunk("Cyrillic chars in target. URL-encoded. Full path. NOT WORKING");
chunk.SetAnchor("file:///C:/Repo/GitHub/testarea/itext5/target/test-outputs/annotate/" + WebUtility.UrlEncode("Вложения") + "/1.png");
doc.Add(new Paragraph(chunk));

但它确实支持 UTF-8 编码中的特殊字符。由于 UTF-8 PdfString 编码是一种 PDF-2.0 功能,而 iText 5 不支持 PDF-2.0,因此必须在此处以 UTF-8 编码注入字符串:

chunk = new Chunk("Cyrillic chars in target. Action manipulated. Full path.");
chunk.SetAnchor("XXX");
action = (PdfAction)chunk.Attributes[Chunk.ACTION];
action.Put(PdfName.URI, new PdfString(new UTF8Encoding().GetBytes("file:///C:/Repo/GitHub/testarea/itext5/target/test-outputs/annotate/Вложения/1.png")));
doc.Add(new Paragraph(chunk));

使用 Edge 41.16299.666.0 测试

【讨论】:

    猜你喜欢
    • 2016-01-14
    • 2016-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-15
    • 2015-11-30
    相关资源
    最近更新 更多