I am trying to write an image onto a PDF using iTextSharp, but aside from the images being written I also need to add text to the PDF. It seems like the best way to do this has been to write the text as an image and insert it onto the page. That all works fine, but when I take a look at the text I see that it is lower quality than I'd like. I end up getting something like this:
You can see there are a lot of "dots" that are different from the surrounding solid background. Here is the code I am using to draw the text:
private System.Drawing.Image DrawText(String text, System.Drawing.Font font, Color textColor, Color backColor)
{
Bitmap img = new Bitmap(1, 1);
Graphics drawing = Graphics.FromImage(img);
SizeF textSize = drawing.MeasureString(text, font);
img.Dispose();
drawing.Dispose();
img = new Bitmap((int) textSize.Width, (int) textSize.Height);
//img.SetResolution(200, 200);
drawing = Graphics.FromImage(img);
drawing.Clear(backColor);
Brush textBrush = new SolidBrush(textColor);
drawing.DrawString(text, font, textBrush, 0, 0);
//drawing.TextRenderingHint = TextRenderingHint.AntiAlias;
//drawing.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
//drawing.SmoothingMode = SmoothingMode.HighQuality;
//img.MakeTransparent(Color.MediumAquamarine);
drawing.Save();
textBrush.Dispose();
drawing.Dispose();
return img;
}
and the relevant text from the PDF writer:
var name = DrawText(Request.Form["name"], font, Color.Black, Color.White);
...
Image titleImg = Image.GetInstance(title.ToStream(ImageFormat.Jpeg));
titleImg.SetAbsolutePosition(float.Parse(Request.Form["x"]), float.Parse(Request.Form["y"]));
titleImg.ScaleToFit(16, 16);
pdfContentByte.AddImage(titleImg);
I'm not sure if it's an issue with the iTextSharp ScaleToFit()
or the drawing method, but I'd like to clean up the background color a bit. You can see the various methods I've tried commented out of the DrawText()
method.
Any thoughts would be appreciated.
Thanks!
The TextRenderingHint
and SmoothingMode
values you tried are actually counter-productive.
If you don't want antialiasing pixels whirling around the text turn them off!
Setting the TextRenderingHint
to SingleBitPerPixelGridFit
or SingleBitPerPixel
should work better.
But even with SmoothingMode.AntiAlias
the pixelation certainly wouldn't be so strong if you didn't save as jpeg
, which is probably the only real mistake.
Whenever text is involved do not use jpeg
, which is strictly for photographs but use png
!
So first change
Image titleImg = Image.GetInstance(title.ToStream(ImageFormat.Jpeg));
to
Image titleImg = Image.GetInstance(title.ToStream(ImageFormat.Png));
and only then check if you still want to optimze the TextRenderingHint
!
Note that this answer is strictly about getting rid of the artifacts around your text.
The best and fully scaleable way to enter text into a PFD
document is to enter real text, not bitmaps, just as mkl has commented. How to do that with ITextSharp
and in the context of your application is a good, new question you may want to post. (Include your failing efforts and an image of the desired result..!)