当我创建一个带&符号的链接时。
在生成的pdf而不是&
中,我有&
因此,链接被破坏
我使用itextsharp和xmlworker处理ASP.NET项目。
我也在演示http://demo.itextsupport.com/xmlworker/中测试过,我也看到了同样的问题。
适用于我的解决方案:
// we create the reader
var reader = new PdfReader(new FileStream(path, FileMode.Open));
// we retrieve the total number of pages
var n = reader.NumberOfPages;
for (var page = 1; page <= n; page++)
{
//Get the current page
var pageDictionary = reader.GetPageN(page);
//Get all of the annotations for the current page
var annots = pageDictionary.GetAsArray(PdfName.ANNOTS);
//Loop through each annotation
if ((annots != null) && (annots.Length != 0))
foreach (var a in annots.ArrayList)
{
//Convert the itext-specific object as a generic PDF object
var annotationDictionary = (PdfDictionary)PdfReader.GetPdfObject(a);
//Make sure this annotation has a link
if (!annotationDictionary.Get(PdfName.SUBTYPE).Equals(PdfName.LINK))
continue;
//Make sure this annotation has an ACTION
if (annotationDictionary.Get(PdfName.A) == null)
continue;
//Get the ACTION for the current annotation
var annotationAction = (PdfDictionary)annotationDictionary.Get(PdfName.A);
//Test if it is a URI action (There are tons of other types of actions, some of which might mimic URI, such as JavaScript, but those need to be handled seperately)
if (!annotationAction.Get(PdfName.S).Equals(PdfName.URI)) continue;
var destination = annotationAction.GetAsString(PdfName.URI).ToString();
destination = destination.Replace("&", "&");
annotationAction.Put(PdfName.URI, new PdfString(destination));
}
}
答案 0 :(得分:1)
您应该对这些特殊的ASCII字符使用URL编码。例如,&#39;&amp;&#39;应替换为&#39;%26&#39;。您可以在此处找到这些代码的完整列表http://www.w3schools.com/tags/ref_urlencode.asp