PDFSharp with Chinese characters

前端 未结 3 1760
野性不改
野性不改 2021-01-20 16:23

I have problem with displaying Chinese characters in PDFSharp in C#. During process of creating the PDF string it\'s ok, but after creating pdf file it doesn\'t display it.

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-20 16:50

    Neither of the solutions worked for a newer version of the lib, so I came up with a workaround on drawing chars to a bitmap, then adding the bitmap to a PDF:

            Font font = GetFont(fieldInfo, fontSize * 0.97f); // Chosen empirically
            using (var imageStream = new MemoryStream())
            {
                // Draw string as an image
                using (var bitmap = new Bitmap((int) fieldRect.Width, (int) (fieldRect.Height * 1.5f)))
                using (Graphics graphics = Graphics.FromImage(bitmap))
                {
                    graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                    graphics.DrawString(fieldValue, font, Brushes.Black, PointF.Empty);
                    bitmap.Save(imageStream, ImageFormat.Png);
                }
    
                // Draw image on PDF
                using (XImage xImage = XImage.FromStream(imageStream))
                {
                    double labelPositionX = fieldRect.X1 + 2;
                    double labelPositionY = fieldRect.Y2 - 2;
                    xGraphics.DrawImage(xImage, labelPositionX, page.Height - labelPositionY);
                }
            }
    

提交回复
热议问题