2014-08-27 9 views
1

Когда я создаю ссылку с амперсандами. В сгенерированном PDF вместо &, у меня есть &Ссылка с амперсандами в формате pdf, созданная xmlworker

Из-за этого, связь нарушается

Я работаю над проектом ASP.NET с itextsharp и xmlworker.

Я тестировал также в демо 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("&amp;", "&"); 
      annotationAction.Put(PdfName.URI, new PdfString(destination)); 
     } 
} 

ответ

1

Вы должны использовать кодировку URL для этих специальных символов ASCII. Например, «&» следует заменить на «% 26». Здесь вы можете найти полный список этих кодов. http://www.w3schools.com/tags/ref_urlencode.asp

+0

Возможно, было бы полезно добавить фрагмент кода, показывающий, как вы можете кодировать определенные символы или строки :) – Stormie

+0

Я обновил свой вопрос, я поставил решение, которое работает для меня – sadiqmrd

Смежные вопросы