Unicode symbols in iTextSharp relative link

◇◆丶佛笑我妖孽 提交于 2019-12-25 03:29:32

问题


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

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

This works fine:

Chunk chunk = new Chunk(text, font);
chunk.SetAnchor("./Attachments/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");

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


回答1:


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

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

(ISO 32000-1, Table 206 – Additional entries specific to a URI action)

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

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!

Thus, simply replace

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

by

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

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


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: The situation changes a bit in the newer PDF specification:

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

(ISO 32000-2, Table 210 — Additional entries specific to a URI action)

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

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...



来源:https://stackoverflow.com/questions/53264518/unicode-symbols-in-itextsharp-relative-link

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!