Add the below width and height of the image in the above code of Ajay Deshwal which is working perfectly for me.
package com.example.itext.processor;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.log.Level;
import com.itextpdf.text.log.Logger;
import com.itextpdf.text.log.LoggerFactory;
import com.itextpdf.text.pdf.codec.Base64;
import com.itextpdf.tool.xml.NoCustomContextException;
import com.itextpdf.tool.xml.Tag;
import com.itextpdf.tool.xml.WorkerContext;
import com.itextpdf.tool.xml.css.CssUtils;
import com.itextpdf.tool.xml.exceptions.LocaleMessages;
import com.itextpdf.tool.xml.exceptions.RuntimeWorkerException;
import com.itextpdf.tool.xml.html.HTML;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext;
public class ImageTagProcessor extends com.itextpdf.tool.xml.html.Image {
private final CssUtils utils = CssUtils.getInstance();
private final Logger logger = LoggerFactory.getLogger(getClass());
/*
* (non-Javadoc)
*
* @see
* com.itextpdf.tool.xml.TagProcessor#endElement(com.itextpdf.tool.xml.Tag,
* java.util.List, com.itextpdf.text.Document)
*/
@Override
public List end(final WorkerContext ctx, final Tag tag,
final List currentContent) {
final Map attributes = tag.getAttributes();
String src = attributes.get(HTML.Attribute.SRC);
List elements = new ArrayList(1);
if (null != src && src.length() > 0) {
Image img = null;
if (src.startsWith("data:image/png;base64")) {
final String base64Data = src.substring(src.indexOf(",") + 1);
try {
img = Image.getInstance(Base64.decode(base64Data));
} catch (Exception e) {
if (logger.isLogging(Level.ERROR)) {
logger.error(String.format(LocaleMessages.getInstance()
.getMessage(
LocaleMessages.HTML_IMG_RETRIEVE_FAIL),
src), e);
}
}
if (null != img) {
String width = attributes.get(HTML.Attribute.WIDTH);
float widthInPoints = utils.parsePxInCmMmPcToPt(width);
String height = attributes.get(HTML.Attribute.HEIGHT);
if (width == null || height == null)
img.setScaleToFitLineWhenOverflow(true);
else
img.setScaleToFitLineWhenOverflow(false);
float heightInPoints = utils.parsePxInCmMmPcToPt(height);
if (widthInPoints > 0 && heightInPoints > 0) {
img.scaleAbsolute(widthInPoints, heightInPoints);
} else if (widthInPoints > 0) {
heightInPoints = img.getHeight() * widthInPoints
/ img.getWidth();
img.scaleAbsolute(widthInPoints, heightInPoints);
} else if (heightInPoints > 0) {
widthInPoints = img.getWidth() * heightInPoints
/ img.getHeight();
img.scaleAbsolute(widthInPoints, heightInPoints);
}
}
if (img != null) {
try {
final HtmlPipelineContext htmlPipelineContext = getHtmlPipelineContext(ctx);
elements
.add(getCssAppliers()
.apply(
new Chunk(
(com.itextpdf.text.Image) getCssAppliers()
.apply(img,
tag,
htmlPipelineContext),
0, 0, true), tag,
htmlPipelineContext));
} catch (NoCustomContextException e) {
throw new RuntimeWorkerException(e);
}
}
}
if (img == null) {
elements = super.end(ctx, tag, currentContent);
}
}
return elements;
}
}