简体   繁体   中英

iTextSharp how to add Cyrillic font in PDF document

I have a previously created PDF file. I want to add a stamp to it with Cyrillic characters and a frame.

For example

例如

For this i use iTextSharp 5.5.13.3 I can make a rectangle, but when I want to add text, I get an error:

System.ArgumentException: 'windows-1252' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method. (Parameter 'name')

Code that does this:

PdfTemplate layer2 = appearance.GetLayer(2);
String text = " Копия ВЕРНА\n"
              + " Дата: " + DateTime.Now.ToString("dd.MM.yyyy HH:mm") +"\n";
string externalFont = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "Arial.TTF");
BaseFont externalBaseFont = BaseFont.CreateFont(externalFont, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); **Throws an exception here!** 
Font font = new Font(externalBaseFont,6, Font.NORMAL, BaseColor.BLACK);
float size = font.Size;
float margin = 1;
Rectangle dataRect = new Rectangle(margin, margin, appearance.Rect.Width- margin, appearance.Rect.Height - margin);
if (size <= 0)
{
    Rectangle sr = new Rectangle(dataRect.Width, dataRect.Height);
    size = ColumnText.FitText(font, text, sr, 10, appearance.RunDirection);
}
ColumnText ct = new ColumnText(layer2);
ct.RunDirection=(appearance.RunDirection);
ct.SetSimpleColumn(new Phrase(text, font), dataRect.Left, dataRect.Bottom, dataRect.Right, dataRect.Top, size, Element.ALIGN_LEFT);
ct.Go();
layer2.SetRGBColorStroke(0, 0, 0);
layer2.SetLineWidth(0.5);
layer2.Rectangle(dataRect.Left, dataRect.Bottom, dataRect.Width, dataRect.Height);
layer2.Stroke();

The solution to my problem was adding:

    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
    Encoding.GetEncoding("windows-1252");

before reading the pdf

using (PdfReader reader = new PdfReader(unsignedPdf))
{
 ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM