2016-11-08 96 views
0

我試着找到這個答案,但是。傳遞一個字符串給出了一個不同的方法來傳遞一個字符串變量

我有這個功能是應該創建一個格式化的段落。 當我將它傳遞給像"<b>Test</b>"這樣的html字符串時,我按照預期在pdf中獲得了粗體文本。

但是,當我傳遞一個具有相同值的字符串變量時,我沒有得到一個格式化文本,而是我只是得到了pdf中的原始字符串。

private Paragraph CreateSimpleHtmlParagraph(string text) 
    { 
     //Our return object 
     Paragraph p = new Paragraph(); 

     //ParseToList requires a StreamReader instead of just text 
     using (StringReader sr = new StringReader(text)) 
     { 
      //Parse and get a collection of elements 
      List<IElement> elements = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr, null); 
      foreach (IElement e in elements) 
      { 
       //Add those elements to the paragraph 
       p.Add(e); 
      } 
     } 
     //Return the paragraph 
     return p; 
    } 
+1

查找作爲「文本」傳遞的字符串之間的差異,它必須在那裏。 – dasblinkenlight

+0

這兩個字符串沒有被插入文檔中完全相同的位置。沒有看到所有的標籤,我無法給出更好的答案。 – jdweng

+0

試試這個:「<b>測試</b >」。你明白了嗎? – heringer

回答

0

非常感謝這麼多人。我在運行時檢查了變量,它是HTML格式(例如:&lt而不是<)。我不得不在變量上使用HttpUtility.HtmlDecode函數,並且完美地解決了這個問題。

相關問題