1

I'm adding a link to a file from a pdf document (created with itext) this way:

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

Link works great if I open document in Google Chrome or Adobe reader. But it doesn't work if I open my PDF in Microsoft Edge.

Is it even possible to create a file link inside pdf with itext that will work in Microsoft Edge? If yes, then how?

Olja Muravjova
  • 117
  • 1
  • 7
  • I just tested around a bit. It appears that Edge does not accept relative URIs here, only complete, absolute ones including the protocol, e.g. "file:///C:/Users/mkl/Documents/test.png" – mkl Nov 19 '18 at 17:46

1 Answers1

2

Is it even possible to create a file link inside pdf with itext that will work in Microsoft Edge?

If yes, then how?

Having done some tests it appears that Edge does not support relative links in PDF documents.

It does support absolute links, though, given the full URI, e.g.

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

In contrast to other PDF viewers (Adobe Reader, Chrome, cf. your previous question in this context) it does not support URL encoding of special characters like Cyrillic ones:

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

But it does support the special characters in UTF-8 encoding. As UTF-8 PdfString encoding is a PDF-2.0 feature and iText 5 does not support PDF-2.0, one has to cheat a bit to inject strings in UTF-8 encoding here:

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

Tested with Edge 41.16299.666.0

Community
  • 1
  • 1
mkl
  • 90,588
  • 15
  • 125
  • 265