0

Using c# and nuget packages iTextSharp, itextsharp.pdfa and itextsharp.xmlworker, version 5.5.13, I generate a PDF document of the PDF/A standard. My problem is that some text has html tags, and should be converted to the PDF also. As my example show the text contains a html tag, and should be converted to bold. How to do that? Maybe it is a possibility to use itext XMLWorkerHelper, if so, how to use it?

using System;
using System.IO;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;

namespace itext_html
{
    class Program
    {
        static void Main(string[] args)
        {
            var baseFontArial = BaseFont.CreateFont(Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\arial.ttf",
                                                BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            var arial7 = new Font(baseFontArial, 7);
            var stream = new FileStream(@"c:\temp\itext\itext.pdf", FileMode.Create);

            var doc = new Document(PageSize.A4);
            var pdfAWriter = PdfAWriter.GetInstance(doc, stream, PdfAConformanceLevel.PDF_A_1B);
            doc.Open();

            var outi = new PdfDictionary(PdfName.OUTPUTINTENT);
            outi.Put(PdfName.OUTPUTCONDITIONIDENTIFIER, new PdfString("sRGB IEC61966-2.1"));
            outi.Put(PdfName.INFO, new PdfString("sRGB IEC61966-2.1"));
            outi.Put(PdfName.S, PdfName.GTS_PDFA1);
            var icc = ICC_Profile.GetInstance(@"C:\Temp\itext\srgb.profile");

            pdfAWriter.SetOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);

            var table = new PdfPTable(2);
            table.SetWidths(new int[] { 65, 35 });
            var text1 = new Phrase("Hello World <b>bold text</b>", arial7);
            var cell = new PdfPCell(text1);
            table.AddCell(cell);
            var text2 = new Phrase("Another text <i>italic text</i>", arial7);
            cell = new PdfPCell(text2);
            table.AddCell(cell);

            doc.Add(table);

            //Add text with html elements
            doc.Add(new Phrase("Hello World <b>bold text</b>", arial7));
            pdfAWriter.CreateXmpMetadata();
            doc.Close();
            pdfAWriter.Close();
        }
    }
}
Batar
  • 19
  • 1
  • 5
  • Did you see this SO [answer](https://stackoverflow.com/questions/25164257/how-to-convert-html-to-pdf-using-itextsharp)? – Alexander I. Feb 27 '19 at 12:24
  • just a friendly FYI, iTextSharp has been deprecated, you should use iText 7 (also on nugget). The second answer from @AlexanderI. link mentions it – André Lemos Feb 27 '19 at 12:38
  • I have edited my code. There are many chunks of text with html elements, to place in tables, or directly into the PDF document. – Batar Feb 27 '19 at 13:20
  • @AlexanderI, I have seen the other answer, but not sure if I can use the XMLWorkerHelper and add text in table cells. – Batar Feb 27 '19 at 19:43

1 Answers1

0

Where sb is the string builder with html content.

    private bool SavePDFFileFromStringHtml(StringBuilder sb, string strPdfFilePath)
    {
        if (File.Exists(strPdfFilePath))
            File.Delete(strPdfFilePath);

        StringReader sr = new StringReader(sb.ToString());

        using (FileStream msReport = new FileStream(strPdfFilePath, FileMode.Create))
        {
            using (Document pdfDoc = new Document(PageSize.A4, 40f, 40f, 90f, 50f))
            {
                try
                {
                    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

                    PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, msReport);
                    pdfWriter.PageEvent = new ITextEvents();

                    pdfDoc.Open();
                    htmlparser.Parse(sr);

                    //PdfPublicelements.Count = pdfWriter.PageNumber;
                    pdfDoc.Close();
                    return true;

                }
                catch (Exception ex)
                {
                    return false;
                }

                finally
                {
                }
            }
        }
    }
Sharon AS
  • 364
  • 3
  • 6