• PDFSHARP 解决汉字问题


    客户要求生成PDF文件,使用了PDFSHARP,但发现汉字全变成了乱码。网上也没有找到针对性的教程。

    经翻查源码,最终解决办法如下:

    LoopNum++;
                this.textBox1.Text += ""+LoopNum.ToString()+"次循环" +"\r\n";
                // Create a new PDF document
                PdfDocument document = new PdfDocument();
                document.Info.Title = "PDFSHARP测试";
    
                // Create an empty page
                PdfPage page = document.AddPage();
    
                // Get an XGraphics object for drawing
                XGraphics gfx = XGraphics.FromPdfPage(page);
    
                //XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);
    
                System.Drawing.Text.PrivateFontCollection pfcFonts = new System.Drawing.Text.PrivateFontCollection();
                string strFontPath = @"C:/Windows/Fonts/msyh.ttf";//字体设置为微软雅黑
                pfcFonts.AddFontFile(strFontPath);
    
                XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);
                XFont font = new XFont(pfcFonts.Families[0], 15, XFontStyle.Regular, options);
     
                // Create a font
                //XFont font = new XFont("Times New Roman", 20, XFontStyle.BoldItalic);
    
                // Draw the text
                gfx.DrawString("你好,世界!", font, XBrushes.Black,
                  new XRect(0, 0, page.Width, page.Height),
                  XStringFormats.Center);
    
                // Save the document...
                string filename = "HelloWorld_"+LoopNum.ToString()+".pdf";
                document.Save(filename); 
  • 相关阅读:
    FTP登录 下载上传文件(太过简单暂时未写)
    依据文档批次命名图片
    UDP协议
    tcp协议(三次握手 四次挥手)
    1956年人工智能元年,而今天是我进入人工智能的日子,哈哈哈
    对比文件简易代码
    爬取网站内容,存放到excel中
    Redis-Py 使用
    python list减法
    sort 使用key函数时,赋多个排序参数
  • 原文地址:https://www.cnblogs.com/qingshan/p/2584884.html
Copyright © 2020-2023  润新知