I am working on a function to add watermark to PDF. The requirement is that the user can input the font type, and then add a watermark to the specified position of the specified pdf according to this font.
At present I have completed the conversion of System.Drawing.Font to iTextSharp.text.Font
It can also display normally for English and numbers.
The question now is: I have a need to display Chinese, but the font type entered by the user may not support Chinese, such as Cambria. Is there a way to make these fonts support Chinese?
Here is my Convert method:
private static iTextSharp.text.Font ConvertFont2ITextFont(System.Drawing.Font _font)
{
var fontFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Fonts);
if(!FontFactory.IsRegistered(_font.Name)) FontFactory.RegisterDirectory(fontFolderPath);
return FontFactory.GetFont(_font.Name, BaseFont.IDENTITY_H, ConvertFontStyle(_font.Style));
}
and here is my addWaterMark code:
PdfContentByte waterMark = stamp.GetOverContent(page);
BaseFont baseFont1 = ConvertFont2ITextFont(font1).BaseFont;
waterMark.BeginText();
waterMark.SetColorFill(basecolor);
waterMark.SetFontAndSize(baseFont1, font1.Size);
Point ptLocation = _GetLocation(nOrigin, new Size(Convert.ToInt32(pagesize.Width), Convert.ToInt32(pagesize.Height)), ptText);
waterMark.ShowTextAligned(PdfContentByte.ALIGN_CENTER, strText, ptLocation.X, ptLocation.Y, 0);
waterMark.EndText();
I found a solution which uses Fontselector
to set the English and Chinese font. But this way seems to not support to add watermark in given position...