It's possible to replace the built-in tags processors of iText(Sharp).
Here is the C# code with custom image tag processor which supports base64 images too. It's easy to convert it to java (base libraries iText and iTextSharp have the same API).
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
namespace CustomHtmlWorkerTag
{
///
/// Our custom HTML Tag to add an IElement.
///
public class CustomImageHTMLTagProcessor : IHTMLTagProcessor
{
///
/// Tells the HTMLWorker what to do when a close tag is encountered.
///
public void EndElement(HTMLWorker worker, string tag)
{
}
///
/// Tells the HTMLWorker what to do when an open tag is encountered.
///
public void StartElement(HTMLWorker worker, string tag, IDictionary attrs)
{
Image image;
var src = attrs["src"];
if (src.StartsWith("data:image/"))
{
// data:[][;charset=][;base64],
var base64Data = src.Substring(src.IndexOf(",") + 1);
var imagedata = Convert.FromBase64String(base64Data);
image = Image.GetInstance(imagedata);
}
else
{
image = Image.GetInstance(src);
}
worker.UpdateChain(tag, attrs);
worker.ProcessImage(image, attrs);
worker.UpdateChain(tag);
}
}
class Program
{
static void Main(string[] args)
{
using (var pdfDoc = new Document(PageSize.A4))
{
PdfWriter.GetInstance(pdfDoc, new FileStream("Test.pdf", FileMode.Create));
pdfDoc.Open();
FontFactory.Register("c:\\windows\\fonts\\tahoma.ttf");
var tags = new HTMLTagProcessors();
// Replace the built-in image processor
tags[HtmlTags.IMG] = new CustomImageHTMLTagProcessor();
var html = "
";
var styles = new StyleSheet();
styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.FONTFAMILY, "tahoma");
styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, "Identity-H");
PdfPCell pdfCell = new PdfPCell { Border = 0 };
pdfCell.RunDirection = PdfWriter.RUN_DIRECTION_LTR;
using (var reader = new StringReader(html))
{
var parsedHtmlElements = HTMLWorker.ParseToList(reader, styles, tags, null);
foreach (var htmlElement in parsedHtmlElements)
{
pdfCell.AddElement(htmlElement);
}
}
var table1 = new PdfPTable(1);
table1.AddCell(pdfCell);
pdfDoc.Add(table1);
}
Process.Start("Test.pdf");
}
}
}