2016-05-31 13 views
0

XMLWorkerを使用してPDF文書に画像を追加しようとしています。何らかの理由で、近くのimgタグを望んでいます。しかし、私がそれに1つ入れても、まだ失敗します。XMLWorker:無効なネストされたタグdivが見つかりました。終了タグが必要です。img

C#コード

 Byte[] bytes; 
     using (var ms = new MemoryStream()) 
     { 
      using (var doc = new Document(PageSize.A4.Rotate())) 
      { 
       using (var writer = PdfWriter.GetInstance(doc, ms)) 
       { 

        //Open the document for writing 
        doc.Open(); 

        //Our HTML and CSS 

        string exampleHtml = @"<div style='position: relative; width: 100%'>" + 

               "<img src='/Content/images/certificate.jpg'><img>" + 

               "<div style='position: absolute; top: 100px; left: 100px; width: 800px; height: 550px; text-align: center;'>" + 
               "<h1>" + 
               "<img src='/Content/images/CertTitle.png' alt='CERTIFICATE PDF' style='width: 800px;'/>" + 
               "</h1>" + 


               "<p>" + 
               "The School certifies that<br/>" + 

               "<h2>FIRSTNAME LAST NAME TITLE</h2>" + 

               "has participated in the class titled<br />" + 

               "<h2>COURSCODE - COURSENAME</h2>" + 


               "This activity was designated for # Credit(s)&trade;" + 

               "</p>" + 




               "<div style='float: left; position: absolute; bottom: 50px;'>" + 
               "<i>Date issued: DATE<br/></i><img src='Content/images/ceo-signature.jpg' style='border-bottom: solid #000 1px;'>" + 
               "</div>" + 
               "</div>" + 

               "</div>"; 

        using (var srHtml = new StringReader(exampleHtml)) 
        { 

         //Parse the HTML 
         iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml); //right here is where it crashes ({"Invalid nested tag div found, expected closing tag img."} {"The document has no pages."} 
        } 


        doc.Close(); 
       } 
      } 

      bytes = ms.ToArray(); 
     } 

     var testFile = HttpContext.Server.MapPath("~/Content/documents/UserCertificates/test.pdf"); ; 
     System.IO.File.WriteAllBytes(testFile, bytes); 

     Response.AddHeader("Content-Disposition", "inline; filename=test.pdf"); 
     return File(testFile, "application/pdf"); 

私はIMGタグを閉じて使ったことがないので、私はここで何か間違ったことをやっている場合、私は興味が?

+1

XMLWorkerはXMLで動作します。 HTMLは閉じられていないタグ( 'img'タグなど)をサポートしていますが、これは有効なXMLではありません。 "HTML"が有効であることを確認する方法を必ず使用する必要があります。あなたの "br"タグは、おそらくHTMLで
として動作しますが、有効なXMLとして破損することはありません。 –

答えて

1

は、私の知る限りではimgタグは、この< img src="" />

のようなものがありますが、正しく閉じられていません。この< img src="" >< img>

関連する問題