iTextSharp相对链接中的Unicode符号 [英] Unicode symbols in iTextSharp relative link

查看:132
本文介绍了iTextSharp相对链接中的Unicode符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在使用iTextSharp创建的pdf文件中创建相对链接

I'm trying to create a relative link in pdf file created with iTextSharp

一切都可以与ASCII字母一起使用,但是如果我在路径中添加其他unicode符号,它们将被跳过

everything works good with ASCII letters, but if I add other unicode symbols in path they are just skipped

这很好:

Chunk chunk = new Chunk(text, font);
chunk.SetAnchor("./Attachments/1.jpg");

这会创建错误的链接(链接创建如下://1.jpg,Вложения-缺少一部分):

This creates incorrect link (link is created like this: //1.jpg, Вложения - part is missing):

Chunk chunk = new Chunk(text, font);
chunk.SetAnchor("./Вложения/1.jpg");

有什么方法可以用Unicode符号创建正确的链接? 谢谢

Is there any way to create correct link with unicode symbols? Thanks

推荐答案

通过在iText 5中使用Chunk.SetAnchor,您可以有效地生成 URI操作.其URI参数指定为

By using Chunk.SetAnchor in iText 5 you effectively generate an URI Action. The URI parameter thereof is specified as

URI ASCII字符串 (必需)要解析的统一资源标识符,以7位ASCII编码.

URI ASCII string (Required) The uniform resource identifier to resolve, encoded in 7-bit ASCII.

(ISO 32000-1,表206 –特定于URI操作的其他条目)

因此,可以认为,Chunk.SetAnchor不接受像您的西里尔字母这样的非ASCII字符. (不过,将它们简单地删除是不行的;如果该方法不接受其输入,则应抛出异常.)

Thus, it can be considered ok that non-ASCII characters like your Cyrillic ones are not accepted by Chunk.SetAnchor. (It is not ok, though, that they are simply dropped; if the method does not accept its input, it should throw an exception.)

但这绝不是意味着您不能在使用某些非ASCII字符的路径中引用文件.相反,您可以利用将路径视为URI的事实:这特别意味着您可以将URL编码方案应用于特殊字符!

But by no means does that mean you cannot reference a file in a path that is using some non-ASCII characters. Instead you can make use of the fact that the path is considered an URI: This in particular means that you can apply the URL encoding scheme for special characters!

因此,只需替换

chunk.SetAnchor("./Вложения/1.jpg");

作者

chunk.SetAnchor(WebUtility.UrlEncode("./Вложения/1.jpg"));

,您的链接将再次起作用! (至少在我的测试中是这样.)

and your link works again! (At least it did in my tests.)

PS:在.Net中,实际上您可以选择很多类来进行URL编码,请参见.例如此答案. WebUtility.UrlEncode在我手头的案例中为我工作,但根据您的用例,可能更适合其他情况.

PS: In .Net you actually have quite a choice of classes to do the URL encoding, cf. for example this answer. WebUtility.UrlEncode worked for me in the case at hand but depending on your use case one of the others might be more appropriate.

PPS:在新的PDF规范中情况有所变化:

PPS: The situation changes a bit in the newer PDF specification:

URI ASCII字符串 (必需)要解析的统一资源标识符,以UTF8编码.

URI ASCII string (Required) The uniform resource identifier to resolve, encoded in UTF8.

(ISO 32000-2,表210-特定于URI操作的其他条目)

(我认为 type 列中的"ASCII"是一种规范错误,应认真对待 value 列中的UTF8.)

(I think the "ASCII" in the type column is a specification error and the UTF8 in the value column is to be taken seriously.)

但是iText 5不支持PDF 2.0,因此此处不支持UTF8编码.可能应该使用声称支持PDF 2.0的iText 7进行测试...

But iText 5 has no PDF 2.0 support and, therefore, does not support UTF8 encoding here. One should probably test with iText 7 which claims PDF 2.0 support...

这篇关于iTextSharp相对链接中的Unicode符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆