Jane Ortega
Jane Ortega

Reputation: 87

How to use html string for iText7

I'm trying to display(simply show it and not download) a pdf, based from html string (not sure if that's the correct term?) using asp.net webforms via itext7.

using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Font;
using iText.Layout.Properties;
//I think I'm missing something here? I added itext7pdfhtml via nuget but not sure how to reference it.

using (var memoryStream = new MemoryStream())
{
   var pdfWriter = new PdfWriter(memoryStream);
   var pdfDocument = new PdfDocument(pdfWriter);
   var document = new Document(pdfDocument, PageSize.LETTER, true);
 //pdfDocument.getCatalog().setOpenAction;
   FontProvider provider = new FontProvider();
   provider.AddFont("Arial");

   StringBuilder sb = new StringBuilder();

     sb.Append("<table width='100%' cellspacing='0' cellpadding='2' style='border: 20px solid #ccc;font-size: 9pt;font-family:Arial'>");
     sb.Append("<tr><td align='center' style='background-color: blue' ><b>Sample</b></td></tr>");
     sb.Append("<tr><td colspan = '2'></td></tr>");
     sb.Append("<tr><td>");
     sb.Append("<b>Sample:</b>");
     sb.Append("</td><td align = 'right'><b>Sample</b>");
  // sb.Append(DateTime.Now);
     sb.Append(" </td></tr>");
     sb.Append("<tr><td colspan = '2'><b>This is example: </b>");
     sb.Append("this is trial");
     sb.Append("</td></tr>");
     sb.Append("</table>");
     sb.Append("<br />");

  // document = HtmlConverter.convertToPdf(sb.ToString(), memoryStream); // htmlconverter not found, already added itext7htmlpdf via nuget, not sure how to reference?
     document.Close();
     bytes = memoryStream.ToArray();
    }
string base64PDF = System.Convert.ToBase64String(bytes, 0, bytes.Length);
            string str = "<embed src='data:application/pdf;base64, " + base64PDF + "' type='application/pdf' width='500px' height='800px' />";
            pdfdiv.InnerHtml = str; // basically showing the pdf here. this already works if I use  document.Add(new Paragraph("test"));

My problem is the proper reference as well as the proper implementation of htmlconverter?

EDIT: these are the following currently installed via nuget: enter image description here

tried: using iText.Html2pdf but getting error does not exist.

Upvotes: 0

Views: 5406

Answers (1)

rhens
rhens

Reputation: 4871

You're indeed missing a using directive for the iText.Html2pdf namespace, which contains HtmlConverter.

Instead of the using directive, you can also try to use the fully qualified name to verify if pdfHTML is correctly installed in your project: iText.Html2pdf.HtmlConverter.ConvertToPdf(...)

Here's a fully working example that shows how to use HtmlConverter:

using System.IO;
using System.Text;
using iText.Html2pdf;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] bytes;

            using (var memoryStream = new MemoryStream())
            {
                StringBuilder sb = new StringBuilder();

                sb.Append("<table width='100%' cellspacing='0' cellpadding='2' style='border: 20px solid #ccc;font-size: 9pt;font-family:Arial'>");
                sb.Append("<tr><td align='center' style='background-color: blue' ><b>Sample</b></td></tr>");
                sb.Append("<tr><td colspan = '2'></td></tr>");
                sb.Append("<tr><td>");
                sb.Append("<b>Sample:</b>");
                sb.Append("</td><td align = 'right'><b>Sample</b>");
                // sb.Append(DateTime.Now);
                sb.Append(" </td></tr>");
                sb.Append("<tr><td colspan = '2'><b>This is example: </b>");
                sb.Append("this is trial");
                sb.Append("</td></tr>");
                sb.Append("</table>");
                sb.Append("<br />");

                HtmlConverter.ConvertToPdf(sb.ToString(), memoryStream);
                //iText.Html2pdf.HtmlConverter.ConvertToPdf(sb.ToString(), memoryStream);

                bytes = memoryStream.ToArray();
            }
            // writing PDF output to file for testing
            File.WriteAllBytes("test.pdf", bytes);
            //string base64PDF = System.Convert.ToBase64String(bytes, 0, bytes.Length);
            //string str = "<embed src='data:application/pdf;base64, " + base64PDF + "' type='application/pdf' width='500px' height='800px' />";
            //pdfdiv.InnerHtml = str;
        }
    }
}

Upvotes: 1

Related Questions