1

I am just trying to add an image to my pdf document using XMLWorker. but for some reason its wanting a close img tag. but even when i put one in it still fails.

C# CODE

        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");

I've never used closing img tags, so I'm curious if I'm doing something wrong here?

NeoSketo
  • 525
  • 1
  • 7
  • 26
  • 1
    XMLWorker works with XML. While HTML supports unclosed tags (like the 'img' tag), this is not valid XML. You have to be sure to use methods to ensure your "HTML" is valid. It would be no different that your "br" tag, while it probably works in HTML as
    , it would break as not valid XML.
    – Kevin Brown May 31 '16 at 18:59

2 Answers2

1

As far as I know img tag be like this < img src="" />

but you wrote it like this < img src="" >< img>

Blubberguy22
  • 1,344
  • 1
  • 17
  • 29
1

Your image tag is not being closed correctly. To do it properly, you need to change <img src='...'><img> to either <img src='...'/> or <img src='...'></img>.

See How to close tag properly answer by M. Rabe

Community
  • 1
  • 1
Blubberguy22
  • 1,344
  • 1
  • 17
  • 29