27317

Unicode symbols in iTextSharp relative link

Question:

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, <strong>Вложения</strong> - 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

Answer1:

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

<blockquote>

<strong>URI</strong> ASCII string <em>(Required)</em> The uniform resource identifier to resolve, encoded in 7-bit ASCII.

</blockquote>

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

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

<hr />

PS: In .Net you actually have quite a choice of classes to do the URL encoding, cf. for example <a href="https://stackoverflow.com/a/8451941/1729265" rel="nofollow">this answer</a>. WebUtility.UrlEncode worked for me in the case at hand but depending on your use case one of the others might be more appropriate.

<hr />

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

<blockquote>

<strong>URI</strong> ASCII string <em>(Required)</em> The uniform resource identifier to resolve, encoded in UTF8.

</blockquote>

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

(I think the "ASCII" in the <em>type</em> column is a specification error and the UTF8 in the <em>value</em> 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...

Recommend

  • How can I do basic maths in bash?
  • Difference between calling a non-static method statically and not statically
  • Bash backtick escaping
  • Jsoup: How to convert a String containing HTML to a XHTML document?
  • Adding listchoices to Pdfform with c# code
  • View PDF through C# .Net desktop App
  • Scala Slick Database Views
  • How can I ensure Realm schema is identical across Android and iOS?
  • numpy 64bit support in PTVS and numpy System.Int64 casting
  • ng-repeat not working with table but works with list
  • Cordova Apache wrong module path
  • Is there a way to call library thread-local init/cleanup on thread creation/destruction?
  • Correct implementation of List Iterator methods
  • iText RadioGroup/RadioButtons across multiple PdfPCells
  • What Makes These Two Array Adds Different?
  • Two Tables Serving as one Model in Rails
  • Bootstrap (v3.3.4) glyphicons not displayed in IE when refresh page (F5)
  • Bigquery event streaming and table creation
  • Implementing “partial void” in VB
  • SetWindowsHookEx does not react on media keys
  • Sending HTML Form Data to Spring REST Web Service
  • Cannot upload to OneDrive using the new SDK
  • Android Google Maps API v2 start navigation
  • System.InvalidCastException: Specified cast is not valid
  • Unable to decode certificate at client new X509Certificate2()
  • Needing to do .toArray() to get output of mongodb .find() on key name not value
  • Dialing with Intent.ACTION_CALL stopps at # in phone number
  • Change Inet root folder for iis 7
  • What is Eclipse's Declaration View used for?
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • Symfony2: How to get request parameter
  • what is the difference between the asp.net mvc application and asp.net web application
  • ORA-29908: missing primary invocation for ancillary operator
  • Jquery - Jquery Wysiwyg return html as a string
  • SVN: Merging two branches together
  • SetUp method failed while running tests from teamcity
  • log4net write single file for each call to log.info
  • Why joiner is not used after Sequence generator or Update statergy
  • Append folder name and increment by 1 using batch script
  • Android Heatmap on canvas or ImageView