2011-12-20 10 views
8

फेंकता है मैं HTML फ़ाइलों का पूरा समूह मर्ज करने के लिए iTextSharp v.4 का उपयोग कर रहा हूं। यह तब तक ठीक काम कर रहा था जब तक मुझे iTextSharp के v.5 में अपग्रेड करने की आवश्यकता नहीं थी।iTextSharp HTMLWorker.ParseToList() NullReferenceException

समस्या तब होती है जब मैं स्ट्रीमवर्कर (HTML फ़ाइल की सामग्री को पढ़ना) HTMLWorker ऑब्जेक्ट की ParseToList विधि में पास करता हूं। यह एक शून्य संदर्भ अपवाद फेंकता है। इसे डिबग करने पर मैं स्ट्रीम रीडर तक पहुंच सकता हूं और पुष्टि कर सकता हूं कि फ़ाइल की सही सामग्री पढ़ी जाती है।

List<IElement> objects; 
try 
{ 
    objects = HTMLWorker.ParseToList(new StringReader(htmlString), null); 
} 
catch (Exception e) 
{ 
    htmlString = "<html><head></head><body><br/><br/><h2 style='color:#FF0000'>ERROR READING FILE!</h2><h3>File Excluded From Stitched Document!</h3><br/><br/><p>There was an error while trying to read the following file:</p><p><span style='color:#FF0000'>" + fileName + "</span></p></body></html>"; 
    objects = HTMLWorker.ParseToList(new StringReader(htmlString), null); 
} 

कैच ब्लॉक में आप है कि मैं तो पीडीएफ के लिए पाठ जोड़ने के लिए एक समस्या थी कि कहने के लिए व्यावहारिक रूप से एक ही कोड का उपयोग देखेंगे:

यहाँ कोड है। यह कोड ठीक काम करता है। निश्चित रूप से यह मैं लगता है कि समस्या मूल html स्ट्रिंग की सामग्री में निहित है बनाता है, इसलिए यहाँ के रूप में यह पार्सर में पारित किया जा रहा से ठीक पहले है स्ट्रिंग की सामग्री है:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
    <meta http-equiv="Pragma" content="no-cache" /> 
    <meta http-equiv="cache-control" content="no-cache" /> 
</head> 
<body style="font-family: Arial, Helvetica, sans-serif; font-size: 1em; margin: 0; 
    padding: 0;"> 
    <div style="font-size: 1em; line-height: 1.25em; width: 190mm;"> 
     <h1 style="font-size: 1.5em; font-weight: bold; margin: 0 0 1.5em 0; text-align: center;"> 
      Advice Item 1</h1> 
     <table border="0" style="width: 190mm; border-collapse: collapse; margin: 0 0 1.5em 0; 
      width: 100%;"> 
      <tbody> 
       <tr> 
        <td style="width: 35mm; height: 1px; line-height: 1px; font-size: 1px;"> 
         &nbsp; 
        </td> 
        <td> 
        </td> 
        <td style="width: 30mm; height: 1px; line-height: 1px; font-size: 1px;"> 
         &nbsp; 
        </td> 
        <td> 
        </td> 
       </tr> 
       <tr> 
        <td colspan="4" style="font-weight: bold;"> 
         <span id="litPatchedToCC" style="text-align: right; font-weight: bold;"></span> 
        </td> 
       </tr> 
       <tr> 
        <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;"> 
         By: 
        </th> 
        <td style="font-weight: bold; padding: 2px 5px;"> 
         ABC 
        </td> 
        <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;"> 
         From: 
        </th> 
        <td style="font-weight: bold; padding: 2px 5px;"> 
         CC 
        </td> 
       </tr> 
       <tr> 
        <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;"> 
         Date: 
        </th> 
        <td style="font-weight: bold; padding: 2px 5px;"> 
         29/03/2011 13:35 
        </td> 
        <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;"> 
         To: 
        </th> 
        <td style="font-weight: bold; padding: 2px 5px;"> 
         Member Practice 
        </td> 
       </tr> 
       <tr> 
        <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;"> 
         Folder: 
        </th> 
        <td style="font-weight: bold; padding: 2px 5px;"> 
         A15-123456 
        </td> 
        <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;"> 
         Individual: 
        </th> 
        <td style="font-weight: bold; padding: 2px 5px;"> 
         Miss A B Test 
        </td> 
       </tr> 
       <tr> 
        <td colspan="2"> 
         <hr width="100%" /> 
        </td> 
        <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;"> 
         Of: 
        </th> 
        <td style="font-weight: bold; padding: 2px 5px;"> 
         Lorem &amp; Ipsum 
        </td> 
       </tr> 
       <tr> 
        <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;"> 
         Species: 
        </th> 
        <td style="font-weight: bold; padding: 2px 5px;"> 
         Bovine 
        </td> 
        <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;"> 
         Position: 
        </th> 
        <td style="font-weight: bold; padding: 2px 5px;"> 
         Member 
        </td> 
       </tr> 
       <tr> 
        <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;"> 
         Item Type: 
        </th> 
        <td style="font-weight: bold; padding: 2px 5px;"> 
        </td> 
        <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;"> 
         Tel: 
        </th> 
        <td style="font-weight: bold; padding: 2px 5px;"> 
        
        </td> 
       </tr> 
       <tr> 
        <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;"> 
        </th> 
        <td style="font-weight: bold; padding: 2px 5px;"> 
        </td> 
        <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;"> 
         Other Nos: 
        </th> 
        <td style="font-weight: bold; padding: 2px 5px;"> 
        </td> 
       </tr> 
       <tr> 
        <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;"> 
         Reason For Call: 
        </th> 
        <td colspan="3" style="font-weight: bold; padding: 2px 5px;"> 
         Some Reason 
        </td> 
       </tr> 
       <tr> 
        <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;"> 
         Subject: 
        </th> 
        <td colspan="3" style="font-weight: bold; padding: 2px 5px;"> 
         Some problem. 
        </td> 
       </tr> 
       <tr> 
        <th scope="row" style="text-align: right; font-weight: normal; padding: 2px 5px;"> 
        </th> 
        <td> 
        </td> 
        <th scope="row" colspan="2" style="text-align: right; font-weight: normal; padding: 2px 5px;"> 
        </th> 
        <td colspan="2"> 
        </td> 
       </tr> 
       <tr> 
        <td style="font-size: 1.5em; font-weight: bold; text-align: center;" colspan="4"> 
         Internal 
        </td> 
       </tr> 
       <tr> 
        <td colspan="4" style="text-align: center; padding: 2px 5px;"> 
         <hr width="100%" /> 
        </td> 
       </tr> 
      </tbody> 
     </table> 
     <div style="padding: 2px 5px;"> 
      <p> 
       Here we start the discussion.</p> 
      <br /> 
      <p> 
       Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> 
      <br /> 
      <p> 
       Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> 
     </div> 
    </div> 
</body> 
</html> 

किसी भी मदद के लिए धन्यवाद। hofnarwillie

उत्तर

8

ऐसा लगता है कि HTMLWorker दो <hr width="100%" /> पर चकित कर रहा है। चूंकि आपने कहा है कि आप V5.XX पर जा रहे हैं, तो अपने HTML को पार्स करने के लिए XMLWorker का उपयोग शुरू करना भी अच्छा हो सकता है - विकास टीम इसकी सिफारिश कर रही है।

अपने विस्तारित HTML के साथ परीक्षण किया गया (नवीनतम HTMLWorker स्रोत कोड भी इस ओर इशारा करते हुए एक छोटे संदर्भ में है), यह काम करता है, और लागू करने के लिए :)

using (Document document = new Document()) { 
    PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream); 
    document.Open(); 
    try { 
    StringReader sr = new StringReader(htmlString); 
    XMLWorkerHelper.GetInstance().ParseXHtml(
     writer, document, sr 
    );   
    } 
    catch (Exception e) { 
    throw; 
    } 
} 

एक वेब वातावरण में परीक्षण किया गया बहुत बुरा नहीं है , तो अपनी पसंद के Stream के साथ Response.OutputStream को प्रतिस्थापित करें।

+0

प्रतिक्रिया के लिए धन्यवाद। XMLWorkerHelper क्लास कहां परिभाषित किया गया है? मुझे नहीं लगता कि मेरे पास iTextSharp dll में है जिसे मैंने डाउनलोड किया है। वी 5.1.3 – hofnarwillie

+0

इसे धन्यवाद मिला। संदर्भ उद्देश्यों के लिए: http://sourceforge.net/projects/itextsharp/files/xmlworker/xmlworker-1.1.1/itextsharp.xmlworker-all-1.1.1.zip/download – hofnarwillie

+0

बस परीक्षण, – hofnarwillie