首页 > 解决方案 > 从 .NET 应用程序打印时,符号字体被光栅化

问题描述

我通过打印到设置为打印到文件的 Postscript 打印机从 C# 创建 Postscript 文件。我正在使用 .NET PrintDocument 类和 Graphics.DrawString 来呈现文本。

如果我使用符号或象形文字字体(例如 Symbol 或 WingDings),文本会在 Postscript 文件中被光栅化。如果我使用例如 Arial,那么文本不会被光栅化。我的目标是从我的应用程序中生成一个 Postscript 文件,其中使用符号字体的文本没有被光栅化。

如果我使用记事本中的符号字体打印相同的文本,则文本不会被光栅化,因此乍一看似乎不是打印机驱动程序的限制。

我在做什么错/不同导致光栅化?

#region Usings

using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;

#endregion

public class PrintingExample
{
    private string _fontName;
    private string _printerName;
    private string _textToPrint;

    //private bool endPrintFired = false;

    // private bool keepGoing = true;
    private Font printFont;

    public PrintingExample(string fontName, string printerName, string textToPrint)
    {
        _fontName = fontName;
        _printerName = printerName;
        _textToPrint = textToPrint;
    }

    // The PrintPage event is raised for each page to be printed.
    private void pd_PrintPage(object sender, PrintPageEventArgs ev)
    {
        const float fontSize = 14.0f;

        var fFamily = new FontFamily(_fontName);
        printFont = new Font(fFamily, fontSize, FontStyle.Regular, GraphicsUnit.Point);


        var leftMargin = ev.MarginBounds.Left;
        var topMargin = ev.MarginBounds.Top;

        ev.Graphics.DrawString(_textToPrint, printFont, Brushes.Black, new Point(leftMargin, topMargin), new StringFormat());

        // Only printing one page
        ev.HasMorePages = false;
    }

    // Print the file.
    public void Printing()
    {
        try
        {
            var outputFile = string.Empty;
            try
            {
                outputFile = Path.Combine(@"c:\temp\print\", Path.GetRandomFileName() + ".ps");

                var pd = new PrintDocument {PrinterSettings = {
                    PrinterName = _printerName, 
                    PrintToFile = true,
                    PrintFileName = outputFile}

                };
                pd.PrintPage += pd_PrintPage;


                PrintController pc = new StandardPrintController();
                PrintController printController = new PrintControllerWithStatusDialog(pc);

                pd.PrintController = printController;

                // Print the document.
                pd.Print();
            }
            finally
            {
                // For debugging, loads in default associated application
                if (File.Exists(outputFile))
                    Process.Start(outputFile);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }


    // This is the main entry point for the application.
    public static void Main(string[] args)
    {
        const string fontName = "Symbol";
        const string printerName = "<Choose an installed Postscript printer here>";

        const string textToPrint = "1234567890\r\nabcdefghijklmnopqrstuvwxyz";
        var p = new PrintingExample(fontName, printerName, textToPrint);
        p.Printing();
    }
}

标签: c#windowsprintingfonts

解决方案


这原来是 Windows 2012R2、10、Server 2016 和 Server 2019 中的一个 bug(server 2008R2 不受影响,这导致我们打开了一个 MS 案例)。只有一些字体受到影响。它已在 2021 年 3 月的 Windows 10、Server 2016 和 Server 2019 操作系统更新中得到解决


推荐阅读