0

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...

Juicern
  • 312
  • 1
  • 10
  • Maybe you could try to add a Chinese font as a reference in your project, and then point the path to it? – 高鵬翔 Dec 17 '20 at 03:50
  • No...It doesn't work...since the input font not supporting Chinese... – Juicern Dec 17 '20 at 04:11
  • Maybe use something like [this](https://stackoverflow.com/a/8615322/13107433) or [this2](https://stackoverflow.com/a/29238674/13107433) – 高鵬翔 Dec 17 '20 at 05:06
  • You use very low level methods to draw the water mark. At that API level you have to check yourself, character by character, which parts of the string shall be drawn using which font, and switch fonts accordingly. Alternatively you can switch to using higher level methods (e.g. `ColumnText` methods) which you can feed paragraph or phrase objects to, to use the `FontSelector`. – mkl Dec 17 '20 at 06:12

0 Answers0