简体   繁体   中英

html tags not converting when html to pdf convert using itextSharp

I am trying to create a pdf using iTextSharp and it did it. But it is also printing html tags in the pdf instead of making it as a design around text

            Document pdfDoc = new Document(PageSize.A4, 25, 10, 25, 10);
            PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            Paragraph Text = new Paragraph("<b>Hiii</b>");
            pdfDoc.Add(Text);
            pdfWriter.CloseStream = false;
            pdfDoc.Close();
            Response.Buffer = true;
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=Example.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Write(pdfDoc);
            Response.End();
        }

Output of this is Hiii with bold tags the way it is looking in the code, but I want Hiii

You expect iText to parse HTML tags in the text of its layout elements ( Paragraph , ...) but iText does not do so. To get " Hiii " instead of "<b>Hiii</b>" in iText you essentially have two options:

  • Explicitly use a bold font for bold text and a normal font for regular text.

     var phrase = new Phrase(); phrase.Add(new Chunk("REASON(S) FOR CANCELLATION:", boldFont)); phrase.Add(new Chunk(" See Statutoryreason(s) designated by Code No(s) 1 on the reverse side hereof", normalFont));

    This is explained in detail in the answers to the question @Ratheesh pointed to in a comment. In particular you can get a bold font either by explicitly naming a bold font, eg

    var normalFont = FontFactory.GetFont(FontFactory.HELVETICA, 12); var boldFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12);

    Or you can ask iText to find a bold font of a font family; if iText cannot find it, it will use create a poor-man's-bold version of the regular font by drawing a line along the outlines of the font glyphs:

     Font verdanaBold = FontFactory.GetFont("Verdana", 7f, Font.BOLD);
  • Use the XMLWorker to parse XHTML and generate accordingly styled layout elements from it.

    This also has been explained in many answers on stack overflow, see for example the CreateSimpleHtmlParagraph method in this answer :

     private static Phrase CreateSimpleHtmlParagraph(String text) { var p = new Phrase(); var mh = new MyElementHandler(); using (TextReader sr = new StringReader("<html><body><p>" + text + "</p></body></html>")) { XMLWorkerHelper.GetInstance().ParseXHtml(mh, sr); } foreach (var element in mh.elements) { foreach (var chunk in element.Chunks) { p.Add(chunk); } } return p; }

    (The helper class MyElementHandler is shown in the referenced answer.)

iTextSharp cannot do that for you. You need to set a bold font to achieve that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM