我制作的程序应该使用 iTextsharp 将 Html 转换为 pdf。但是每次我运行程序时, HTMLWorker.parse(stringreader reader);
. 异常说它找不到网络路径。
public string Run(string html, string pdfName)
{
try
{
Document doc = new Document();
//PdfWriter writer2 = PdfWriter.GetInstance(doc, new FileStream(@"L:\" + "Test54" + ".pdf", FileMode.Create));
StyleSheet styles = new StyleSheet();
string filePath = @"Z:\Emon Stage\" + "test" + ".pdf";
using (FileStream pdfStream = new FileStream(filePath, FileMode.Create))
{
using (PdfWriter writer = PdfWriter.GetInstance(doc, pdfStream))
{
writer.CloseStream = false;
doc.Open();
doc.OpenDocument();
doc.NewPage();
if (doc.IsOpen() == true)
{
using (StringReader reader = new StringReader(html))
{
//XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, reader);
doc.Add(new Paragraph(" "));
using (HTMLWorker worker = new HTMLWorker(doc))
{
worker.Open();
worker.StartDocument();
worker.NewPage();
worker.Parse(reader);
worker.SetStyleSheet(styles);
List<IElement> ie = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(reader, null);
foreach (IElement element in ie)
{
doc.Add((IElement)element);
}
worker.EndDocument();
worker.Close();
}
}
}
writer.Close();
}
}
doc.CloseDocument();
doc.Close();
doc.Dispose();
return string.Empty;
}
catch (Exception ex)
{
return ex.Message;
}
}
根据stackoverflow的某人所说,我使用的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><title>
AutoScout24: Fahrzeugdaten ausdrucken
</title><meta http-equiv="X-UA-Compatible" content="IE=7" /><meta http-equiv="content-type" content="text/html; charset=utf-8" /><meta name="author" content="AutoScout24" /><meta name="robots" content="noindex,follow" /><meta name="description" content="Preis: 7.700 €, Baujahr: 1996, Leistung: 207 Kw (281 PS), Laufleistung: 169.000 km, Getriebe: Automatik, Kraftstoff: Benzin" /><meta name="keywords" content="AutoScout24" /><meta name="page-topic" content="AutoScout24" /><link rel="alternate" type="application/rss+xml" title="AutoScout24-Magazin RSS-Feed" href="http://ww2.autoscout24.de/magazine_rss.xml" /><script type="text/javascript" src="//s.autoscout24.net/combine/js?921b9c37a2e4ab133b1f14b8e13c02f073e017fd0bef1432f7d27433f4e9d289"></script><link rel="stylesheet" type="text/css" href="//s.autoscout24.net/cmsbox/99/2462008437" /><link rel="stylesheet" type="text/css" href="//s.autoscout24.net/combine/css?69a1274c6bb2e43c6fd649686b1910dcb88846da731f33577731763848fed5ef8e57233cfa31bdac608a787bc54dc3dc25b21bb0e21eec631ca1c316e050af2aee3a9b1a4cd63f84d9ece894f4eceaaf85a689c0c1177a62cb374a776b2fdc53" /><link rel="stylesheet" type="text/css" href="//s.autoscout24.net/combine/css?ef372438e68b2221" media="print" /><script type="text/javascript">$.as24={toggles:{'MyAreaB2BRedesignExport':1,'MyAreaInsertionITToggle':1,'MyAreaInsertionNLToggle':1,'RemoveStaticRoutes':1,'TestUsageRestServiceMessageTellAFriend':1,'LinkToWPOnDetailsPageDE':1,'SEOPageES':1,'NuggadATPixelToggle':1,'ProlongArticleInTwoSteps':1,'ArticleDeletionSurvey':1,'SealDataOnPrintOut':1,'AdvancedSearchTopArticlesRelocate':1,'CustomerExportJson':1}};</script><script type="text/javascript" src="//s.autoscout24.net/combine/js?70f8f19d6b1d1f718cd532e95f5d6b2bb41768327b71a504f9edb406e60007f0c5c46b74b1e9cc69872734b354a85ce1e0adbfe55e4efc75"></script><meta name="title" content="Corvette C4 - AutoScout24" /><link rel="image_src" href="http://pic3.autoscout24.net/images-small/305/233/0219233305001.jpg?70cec1b69010c9baa7b920ce66f714ff" />
<!-- Start Visual Website Optimizer Asynchronous Code -->
<script type='text/javascript'>
var _vwo_code=(function(){
var account_id=10842,
settings_tolerance=2000,
library_tolerance=1500,
use_existing_jquery=false,
// DO NOT EDIT BELOW THIS LINE
f=false,d=document;return{use_existing_jquery:function(){return use_existing_jquery;},library_tolerance:function(){return library_tolerance;},finish:function(){if(!f){f=true;var a=d.getElementById('_vis_opt_path_hides');if(a)a.parentNode.removeChild(a);}},finished:function(){return f;},load:function(a){var b=d.createElement('script');b.src=a;b.type='text/javascript';b.innerText;b.onerror=function(){_vwo_code.finish();};d.getElementsByTagName('head')[0].appendChild(b);},init:function(){settings_timer=setTimeout('_vwo_code.finish()',settings_tolerance);this.load('//dev.visualwebsiteoptimizer.com/j.php?a='+account_id+'&u='+encodeURIComponent(d.URL)+'&r='+Math.random());var a=d.createElement('style'),b='body{opacity:0 !important;filter:alpha(opacity=0) !important;background:none !important;}',h=d.getElementsByTagName('head')[0];a.setAttribute('id','_vis_opt_path_hides');a.setAttribute('type','text/css');if(a.styleSheet)a.styleSheet.cssText=b;else a.appendChild(d.createTextNode(b));h.appendChild(a);return settings_timer;}};}());_vwo_settings_timer=_vwo_code.init();
</script>
<!-- End Visual Website Optimizer Asynchronous Code -->
</head>
<!--[if IE]>
<body class="ie7">
<![endif]-->
<!--[if !IE]><!-->
<body>
<!--<![endif]-->
<div id="docu"><img id="ctl00_decoratedArea_myArticleDetailImages_bigImage" src="http://pic2.autoscout24.net/images/305/233/0219233305001.jpg?70cec1b69010c9baa7b920ce66f714ff" alt="" border="0" />
<br/><img id="ctl00_decoratedArea_myArticleDetailImages_smallImageRepeater_ctl00_smallImage" src="http://pic4.autoscout24.net/thumbnails-big/305/233/0219233305002.jpg?22b74f6292d681883fe242fc9f719a2a" alt="" border="0" /><img id="ctl00_decoratedArea_myArticleDetailImages_smallImageRepeater_ctl01_smallImage" src="http://pic4.autoscout24.net/thumbnails-big/305/233/0219233305003.jpg?27d71f459ce81983a794639f51f4e37f" alt="" border="0" /><img id="ctl00_decoratedArea_myArticleDetailImages_smallImageRepeater_ctl02_smallImage" src="http://pic4.autoscout24.net/thumbnails-big/305/233/0219233305004.jpg?780b74044093e5c80f68f23ea84db5c2" alt="" border="0" /><img id="ctl00_decoratedArea_myArticleDetailImages_smallImageRepeater_ctl03_smallImage" src="http://pic4.autoscout24.net/thumbnails-big/305/233/0219233305005.jpg?658b15994c9680a056140d2510783137" alt="" border="0" />
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/></div>
<div id="globalLayerMask" class="globalLayerMask"></div>
<div id="globalLayer">
<form id="layerForm" enctype="application/x-www-form-urlencoded" action="" method="post">
</form>
</div>
<form name="aspnetForm" method="post" action="PrintViewArticles.aspx?id=219233305" id="aspnetForm">
<input type="hidden" name="contactSource" id="contactSource" value="Form" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="" />
<script type="text/javascript">
var cval=getCookieValue('as24Visitor',null);
setCookieValue('as24Visitor',cval===null?'a5f12924-6fad-495f-b551-fde651c9e4dc':cval,true);
</script>
<script type="text/javascript">
var firstIvwImage = new Image();
function sendIvwReport()
{
firstIvwImage.src = "http://toi.ivwbox.de/cgi-bin/ivw/CP/07020801;/Autoscout24/Gebrauchtwagen-Suche.html?r=" + escape(document.referrer) + "&d=" + Date.parse(new Date());
}
sendIvwReport();
</script>
<div class="container">
<div id="ctl00_decoratedArea_omniturePixel" class="pixel">
<script type="text/javascript">
var s_account='autoscout24de';var s_pagename='Search-UC-Detail-Print';
</script>
<script type="text/javascript" src="//s.autoscout24.net/combine/js?b52b5d64cd0410a8"></script>
<script type="text/javascript">
try{s.dc=112;s.trackingServer="";s.trackingServerSecure="";s.server="www.autoscout24.de";s.pageName=s_pagename;s.channel="Search-UC";s.prop9="de-DE";s.prop1="16380";s.prop2="";s.prop11="";s.prop12="";s.prop13="";s.prop15="";s.prop16="";s.prop17="";s.prop18="";s.prop19="";s.prop20="";s.prop21="U";s.prop22="";s.prop25="";s.prop26="";s.prop27="";s.prop30="";s.prop42="";s.prop36="";s.prop4="Standard";s.prop29="S";s.eVar13="de-DE";s.eVar40="";s.eVar2="";s.eVar3="U";s.eVar4="";s.eVar5="";s.eVar6="";s.eVar8="";s.eVar9="";s.eVar14="";s.eVar15="";s.eVar17="";s.eVar18="";s.eVar31="";s.eVar49="";s.eVar58="";s.eVar12="";s.eVar23="P";s.eVar34="";s.eVar19="P";s.eVar20="S";s.eVar10="Standard";s.hier1="/16380";s.events="event7";s.products=";16380|19712|1996|B|P|2|8000"; var s_code=s.t();if(s_code)document.write(s_code); } catch(e) {} finally {}
</script>
</div>
<div class="info-top-area">
<div class="print-button-container">
<div class="orange-rounded-button">
<div id="ctl00_decoratedArea_articlePrintHeader_printButton1" onclick="window.print(); return false;" class="primary-action">
<span class="l"></span><span class="m">Jetzt drucken</span><span class="r"></span>
</div>
</div>
</div>
<div class="clear-both"></div>
<div class="print-header">
<div class="print-logo">
<img id="ctl00_decoratedArea_articlePrintHeader_as24PrintLogo" src="//s.autoscout24.net/Images/_cache/ad95aba9b57bb3e9.jpg" alt="" border="0" />
</div>
<div class="date-insertion">
<span id="ctl00_decoratedArea_articlePrintHeader_printFound">Inserat entdeckt am 03.09.2012</span>
</div>
</div>
</div>
<div class="clear-both"></div>
<div class="print-dealer">
<div id="ctl00_decoratedArea_customerInfo_privateCustomerInfo_contactAddressPanel" class="contact-address">
<p>D 97225 Zellingen</p>
</div>
<div id="ctl00_decoratedArea_customerInfo_privateCustomerInfo_contactPhonePanel" class="contact-phone no-margin">
<div id="ctl00_decoratedArea_customerInfo_privateCustomerInfo_contactPhoneNoPanel">
<p><b id="ctl00_decoratedArea_customerInfo_privateCustomerInfo_contactPhoneNoRepeater_ctl00_contactPhoneNoType">Tel.:</b>
+49 (0)177 / 7931437</p>
</div>
</div>
</div>
<div class="clear-both"></div>
<div class="left-content-area">
<div class="detail-inner">
<div class="caption-box">
<div class="article-caption">
<div id="ctl00_decoratedArea_articleCaption_lblHead" class="detail-head">
Corvette C4
</div>
<div id="ctl00_decoratedArea_articleCaption_lblSubHeadDown" class="detail-subhead">
</div>
</div>
</div>
</div>
<div class="detail-inner">
<div class="floatLeft">
<div class="detail-column-left">
<div class="car-big">
<div class="carpics">
</div>
</div>
</div>
<div class="detail-column-right">
<div class="detail-info">
<table class="i-detail">
<tr id="ctl00_decoratedArea_myArticleDataControl1_pricePublicRow">
<td class="c1">
<b id="ctl00_decoratedArea_myArticleDataControl1_pricePublicLabel">Preis:</b>
</td>
<td valign="bottom" class="price-fat">
<span id="ctl00_decoratedArea_myArticleDataControl1_pricePublicValue">€ 7.700,-</span>
</td>
</tr>
<tr id="ctl00_decoratedArea_myArticleDataControl1_emptyRow">
<td colspan="2" style="height:13px"></td>
</tr>
<tr>
<td class="c1">
<span id="ctl00_decoratedArea_myArticleDataControl1_offerTypeKey">Fahrzeugkategorie:</span>
</td>
<td class="c2">
<span id="ctl00_decoratedArea_myArticleDataControl1_offerTypeValue">Gebraucht</span>
</td>
</tr>
<tr id="ctl00_decoratedArea_myArticleDataControl1_bodyTypeRow">
<td class="c1">
<span id="ctl00_decoratedArea_myArticleDataControl1_bodyTypeKey">Aufbau:</span>
</td>
<td class="c2">
<span id="ctl00_decoratedArea_myArticleDataControl1_bodyTypeValue">Cabrio</span>
</td>
</tr>
<tr id="ctl00_decoratedArea_myArticleDataControl1_firstRegRow">
<td class="c1">
<span id="ctl00_decoratedArea_myArticleDataControl1_firstRegKey">Erstzulassung:</span>
</td>
<td class="c2">
<span id="ctl00_decoratedArea_myArticleDataControl1_firstRegValue">07/1996</span>
</td>
</tr>
<tr id="ctl00_decoratedArea_myArticleDataControl1_mileageRow">
<td class="c1">
<span id="ctl00_decoratedArea_myArticleDataControl1_mileageKey">Kilometerstand:</span>
</td>
<td class="c2">
<span id="ctl00_decoratedArea_myArticleDataControl1_mileageValue">169.000 km</span>
</td>
</tr>
<tr id="ctl00_decoratedArea_myArticleDataControl1_fuelRow">
<td class="c1">
<span id="ctl00_decoratedArea_myArticleDataControl1_fuelKey">Kraftstoff:</span>
</td>
<td class="c2">
<span id="ctl00_decoratedArea_myArticleDataControl1_fuelValue">Benzin</span>
</td>
</tr>
<tr id="ctl00_decoratedArea_myArticleDataControl1_powerRow">
<td class="c1">
<span id="ctl00_decoratedArea_myArticleDataControl1_powerKey">Leistung:</span>
</td>
<td class="c2">
207
<span id="ctl00_decoratedArea_myArticleDataControl1_kwUnit">kW</span>
(281
<span id="ctl00_decoratedArea_myArticleDataControl1_hpUnit">PS</span>)
</td>
</tr>
<tr id="ctl00_decoratedArea_myArticleDataControl1_gearingTypeRow">
<td class="c1">
<span id="ctl00_decoratedArea_myArticleDataControl1_gearingTypeKey">Getriebeart:</span>
</td>
<td class="c2">
<span id="ctl00_decoratedArea_myArticleDataControl1_gearingTypeValue">Automatik</span>
</td>
</tr>
<tr id="ctl00_decoratedArea_myArticleDataControl1_bodyColorRow">
<td class="c1">
<span id="ctl00_decoratedArea_myArticleDataControl1_bodyColorKey">Außenfarbe:</span>
</td>
<td class="c2">
<span id="ctl00_decoratedArea_myArticleDataControl1_bodyColorValue">Grün Metallic</span>
</td>
</tr>
</table>
</div>
</div>
</div>
<div class="detail-column-left">
<div id="articleDetailEnvkv" class="envkv_box">
</div>
</div>
<div class="detail-column-right">
<div class="dashed-line" style="margin-left:3px;"></div>
<div class="lower-right1">
<div id="ctl00_decoratedArea_myArticleEquipments_equipmentsHead" class="actions-box-caption">
Fahrzeugausstattung:
</div>
<div id="ctl00_decoratedArea_myArticleEquipments_equipmentList" class="equi-list-all">
Nichtraucherfahrzeug
</div>
</div>
<div class="clear-both"></div>
<div class="dashed-line">
</div>
<div class="lower-right3">
<div id="ctl00_decoratedArea_ArticleMoreDetailsData_detailsCaption" class="actions-box-caption">
Weitere Details zum Fahrzeug:
</div>
<div class="more-details">
<table class="i-detail" cellspacing="0" cellpadding="0">
<tr id="ctl00_decoratedArea_ArticleMoreDetailsData_fuelRow" valign="top">
<td class="c1">
<span>Alle Kraftstoffe:</Span>
</td>
<td class="c2">
<span class="c2-sub">Benzin</div>
</td>
</tr>
<tr id="ctl00_decoratedArea_ArticleMoreDetailsData_generalInspectionRow">
<td class="c1">
<span id="ctl00_decoratedArea_ArticleMoreDetailsData_generalInspectionKeyLabel">HU/AU:</span>
</td>
<td class="c2">
<span id="ctl00_decoratedArea_ArticleMoreDetailsData_generalInspectionValueLabel">02/2014</span>
</td>
</tr>
</table>
</div>
</div>
<div class="dashed-line"></div>
<div class="sep-short">
<img id="ctl00_decoratedArea_ArticlePrintFinancingData_seperatorImage3" src="//s.autoscout24.net/Images/_cache/275a32375231b176.gif" alt="" border="0" />
</div>
<div class="lower-right3">
<div id="ctl00_decoratedArea_ArticlePrintFinancingData_printFinancingHeader" class="actions-box-caption">
Finanzdienstleistungen:
</div>
<div id="ctl00_decoratedArea_ArticlePrintFinancingData_printFinancingDisclaimer" class="description-text">
Bitte wenden Sie sich für ein individuelles Finanzierungs-, Leasing- oder Versicherungs-Angebot an Ihren Partner.
</div>
</div>
<div id="ctl00_decoratedArea_AdServerArticleDetailItem" class="adbox">
</div>
</div>
</div>
<div class="clear-both"></div>
<div class="lower-content-bottom"></div>
<div class="footer-warranty">
<p id="ctl00_decoratedArea_articlePrintFooter_as24Warranty_disclaimerText" class="notice">AutoScout24 GmbH übernimmt für die Richtigkeit der Angaben keine Gewähr.</p>
</div>
<div class="print-button-container">
<div class="orange-rounded-button">
<div id="ctl00_decoratedArea_articlePrintFooter_printButton2" onclick="window.print(); return false;" class="primary-action">
<span class="l"></span><span class="m">Jetzt drucken</span><span class="r"></span>
</div>
</div>
</div>
</div>
<img src="http://tracking.autoscout24.com/parser.ashx?source=pr&url=http://www.autoscout24.de/PrintViewArticles.aspx?id=219233305&visitor=not_found&ticks=634822815476118951" width="2" height="2" style="position: absolute" />
</div>
<script type="text/javascript">
$(function() {
try {
var resCookie=$.cookie('ResolutionParams');
var domain=document.domain;
if(domain.indexOf('.') > -1) domain=domain.substring(domain.indexOf('.'));
if (!resCookie || true) {
resCookie = screen.width.toString();
$.cookie('ResolutionParams', resCookie, { expires: 365, domain: domain, path: '/'});
}
} catch(e) {}
});
</script>
<script type="text/javascript" src="http://ads.autoscout24.com/adserver/index4.aspx?slot=268&language=ger&site=1&elementid=ctl00_decoratedArea_AdServerArticleDetailItem&dealerid=12335432&price=7700&carfinancedby=0&make=16380&modell=19712&strModell=C4&strMake=Corvette&vat=0&body=2&hsn=&hp=281&kw=207&zip=97225&age=194&Year=1996&carid=219233305&adtype=P&lastInspect=02/01/2014&tsn=&Mileage=169000&carNo=&cartype_id=C&accident=U&Month=7&rnd=28&ipaddress=1400240550&ccm=0&bildurl=http://pic2.autoscout24.net/images/305/233/0219233305001.jpg?70cec1b69010c9baa7b920ce66f714ff&eurotaxid=0&offertype=U&fuel=B&inter=&cash=&durat=&rate=&frate=&tm=3"></script></form>
<script src='//js.revsci.net/gateway/gw.js?csid=J11265&auto=t'></script>
<!-- m-pathy tracking [ start ] -->
<script type="text/javascript">
var _mpathyHost = "http" + (document.location.protocol!="https:" ? "://m-pathy.gdlcdn.com/js/a" : "s://ne.wac.edgecastcdn.net/001262/m-pathy/js/as");
document.write(unescape("%3Cscript src='" + _mpathyHost + "2909.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<!-- m-pathy tracking [ end ] -->
<span id="ctl00_VisualWebsiteOptimizer1"></span>
</body>
</html>