0

私はiTextSharp自分のプロジェクトで使用しており、このメソッドを使用してhtml文字列をpdfに解析しています

public static MemoryStream MakePdf(string htmlCode)
        {
            MemoryStream msOutput = new MemoryStream();
            TextReader reader = new StringReader(htmlCode);

            Document document = new Document(PageSize.A4, 30, 30, 30, 30);
            PdfWriter writer = PdfWriter.GetInstance(document, msOutput);
            HTMLWorker worker = new HTMLWorker(document);
            document.Open();
            worker.StartDocument();
            worker.Parse(reader); // EXCEPTION IN THIS LINE!!!!
            worker.EndDocument();
            worker.Close();
            document.Close();

            return msOutput;
        }

そして、それは私にはうまくいきません。選択した行で例外をスローします

URI 形式はサポートされていません。

どうすればこの問題を解決できますか?

PSこれが解析する必要がある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" />
    <meta name="author" content="Boomer" />
    <style type="text/css">
        body 
        {
           font: 12px/18px Arial, Tahoma, Verdana, sans-serif;
           width: 100%;
           font-family: Myriad Pro;
           font-style: italic;
           background: #f3f3f3;
        }

        @font-face 
        {
            font-family: 'Myriad Pro';
            src: url('../fonts/myriadpro.eot');
            src: url('../fonts/myriadpro.eot?#iefix') format('embedded-opentype'),
                 url('../fonts/myriadpro.woff') format('woff'),
                 url('../fonts/myriadpro.ttf') format('truetype');
            font-weight: normal;
            font-style: normal;
        }

        .clear
        {
            clear: both;
        }

        a,.blue_text
        {
            color:#2aa2f6;
            text-decoration: none;
        }

        a:hover
        {
            text-decoration: underline;
        }

        .simple_title
        {
           font-size: 22px; 
        }

        .text750
        {
            width: 750px;
            margin: 20px 0;
        }

        .full_width_text
        {
            margin-right: 35px;
            text-align: justify;
        }

        .grey_text 
        {
            color:#afafaf;
        }

        .email_page
        {
            width: 1025px;
            margin: 20px auto;
            background: #fff;
            padding: 20px 0;
        }

        .email_page p
        {
            font-size: 18px;
        }

        .email_div
        {
            margin: 0 auto;
            background: #fff;
            width: 998px;
            border: 1px solid #ebebeb;
            border-radius: 10px;
           -webkit-border-radius:10px;
           -border-radius:10px;
           -moz-border-radius:10px;
           -o-border-radius:10px;
            display: table;
        }

        .email_content 
        {
            margin-left: 60px;
            margin-top: 40px;
        }

        .email_logo
        {
            float: left;
        }

        .email_title
        {
            float: left;
            margin-left: 130px;
            font-size: 22px;
            color:#f7941d;
            margin-top: 50px;
        }

        .email_img_container
        {
            float: right;
            width: 270px;
        }

        .email_img_container .text_description
        {
            font-size: 18px;
            color:#000000;
            margin-bottom: 15px;
        }

        .email_img_container img
        {
            border: 1px solid #ebebeb;
            border-radius: 10px;
           -webkit-border-radius:10px;
           -border-radius:10px;
           -moz-border-radius:10px;
           -o-border-radius:10px;
        }

        .text600
        {
            width: 600px;
            float: left;
        }
    </style>
    <title>Voucher</title>
</head>

<body>
    <div class="email_page">

    <div class="email_div">
        <div class="email_content">
           <div class="text600">
                <div class="email_logo">
                    <a href="#"> 
                        <img src="{EmailLogo}"  />
                    </a>

                </div>
                <div class="email_title">
                    Instant Gift Certificate
                </div>  
                <div class="clear"></div>
                <div>
                    <p>To: <span class="blue_text">{RecipientName}</span></p>
                    <p>
                    <div>{GiftVoucherName}</div>
                    <div>{GiftVoucherDescription}</div>
                    </p>
                    <p>
                        This gift is from:  <span class="blue_text">{SenderName}</span>
                    </p>
                </div>

            </div>

            <div class="email_img_container">
                <div class="text_description">
                    <div>SG Code: {SGCode}</div>
                    <div>Purchased on: {PurchaseDate}</div>
                </div>
                <img src="{MerchantImage}" alt="" />
            </div>
         <div class="clear"></div>
         <div class="text750">
            <p>
                This gift must be redeemed by: <span class="blue_text">{Date}</span><br />
                Redeemable at the following locations: <span class="blue_text">{Locations}</span><br />
                For other details and terms and conditions, please see the other attachment. 



            </p>

            <div class="simple_title">
                Disclaimer
            </div>
            <p>
                Test.com is not responsible for the content of the message or the selection of the gift by the sender.
                Once the sender enters the information, the instant gift is automatically generated and sent to
                the recipient.
            </p>
            <p>
                Visit us at: <a href="http://www.Test.com">www.Test.com</a>
            </p>
         </div>


        </div>


    </div>
</div>

</body>
</html>
4

1 に答える 1

1

この問題を解決するにSetProvidersは、HTMLWorker クラスに文字列キーの定数がある IDictionary を受け取るメソッドを使用して、プロバイダーを HTMLWorker に追加します。あなたの場合、ILinkProvider および/または IImageProvider を見てください。これらは、HTML 内の画像と URL を自分で処理し、それらを PDF で使用可能な部分に変換する方法を提供します。

于 2013-02-13T09:35:37.063 に答える