本文介绍了如何将 PrintDocument .PrinterSettings 保存在文件中,以便用户不必每次都选择打印机设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用打印预览打印文档,每次用户单击打印数据时,都会提示要求打印机设置的对话框,这当然不好.因此,为了删除那个打印对话框,我需要将打印对话框设置存储在硬盘上的某个位置.

代码

 PrintDocument pd = new PrintDocument();pd.PrintPage += new PrintPageEventHandler(PrintPage);PrintDialog pdi = new PrintDialog();pdi.Document = pd;如果(pdi.ShowDialog() == DialogResult.OK){doc.PrinterSettings = pdi.PrinterSettings;pd.Print();}别的{MessageBox.Show("打印取消");}

问题

如何以某种格式存储打印对话框设置并检索它并将其存储在对象中?

P.S:下次重新启动应用程序时应保存设置.

解决方案

在您的项目的设置文件中创建一个设置键并选择

注意:

编辑

由于PrinterSettings 类是可序列化的,另一种解决方案是将其转换为base64 字符串并将其保存在字符串中并从字符串中恢复.因此,您可以创建一个字符串设置键 PrinterSettings 并将 PrinterSettings 保存在其中或从中恢复:

保存:

Properties.Settings.Default.PrinterSettings=SettingToString(printDialog1.PrinterSettings);Properties.Settings.Default.Save();

阅读:

printDocument1.PrinterSettings = SettingFromString(Properties.Settings.Default.PrinterSettings);

辅助方法:

private string SettingToString(PrinterSettings settings){如果(设置 == 空)返回空;var bf = new BinaryFormatter();使用 (var ms = new MemoryStream()){bf.Serialize(ms, settings);返回 Convert.ToBase64String(ms.ToArray());}}private PrinterSettings SettingFromString(string base64){尝试{BinaryFormatter bf = new BinaryFormatter();使用 (var ms = new MemoryStream(Convert.FromBase64String(base64))){返回 (PrinterSettings)bf.Deserialize(ms);}}捕获(异常){返回新的打印机设置();}}

I am printing a document using print preview and every-time user click to print the data a dialogue asking for printer settings gets prompted which is of course not good. So, in order to remove that print dialogue box I need to store the print dialogue settings some where on the hard disk.

Code

        PrintDocument pd = new PrintDocument();
        pd.PrintPage += new PrintPageEventHandler(PrintPage);
        PrintDialog pdi = new PrintDialog();
        pdi.Document = pd;
        if (pdi.ShowDialog() == DialogResult.OK)
        {
            doc.PrinterSettings = pdi.PrinterSettings;
            pd.Print();
        }
        else
        {
            MessageBox.Show("Print Cancelled");
        }

Problem

how to store print dialog setting in some format and retrieve and store it in the object?

P.S: settings should be saved for the next time when the application is restarted.

解决方案

Create a setting key in Setting file of your project and choose System.drawing.Printing.PrinterSettings type for it and name it for example PrinterSettins. Then you can save your printer settings in this property or read from this property simply.

Also you can create some setting keys for individual properties of PrinterSettings like PrinterName save them.

Save:

if(printDialog1.ShowDialog()==DialogResult.OK)
{
   Properties.Settings.Default.PrinterSettings = printDialog1.PrinterSettings;
   //Properties.Settings.PrinterName = printDialog1.PrinterSettings.PrinterName;
   Properties.Settings.Default.Save();
}

Read:

printDocument1.PrinterSettings = Properties.Settings.Default.PrinterSettings;
//printDocument1.PrinterSettings.PrinterName = Properties.Settings.Default.PrinterName;

Note:

EDIT

Since PrinterSettings class is serializable, another solution is to convert it to a base64 string and save it in a string and restore it from string. So you can create a string setting key PrinterSettings and save PrinterSettings in it or restore from it:

Save:

Properties.Settings.Default.PrinterSettings=SettingToString( printDialog1.PrinterSettings);
Properties.Settings.Default.Save();

Read:

printDocument1.PrinterSettings = SettingFromString(Properties.Settings.Default.PrinterSettings);

Helper Methods:

private string SettingToString(PrinterSettings settings)
{
    if (settings == null)
        return null;

    var bf = new BinaryFormatter();
    using (var ms = new MemoryStream())
    {
        bf.Serialize(ms, settings);
        return Convert.ToBase64String(ms.ToArray());
    }
}

private PrinterSettings SettingFromString(string base64)
{
    try
    {
        BinaryFormatter bf = new BinaryFormatter();
        using (var ms = new MemoryStream(Convert.FromBase64String(base64)))
        {
            return (PrinterSettings)bf.Deserialize(ms);
        }
    }
    catch (Exception)
    {
        return new PrinterSettings();
    }
}

这篇关于如何将 PrintDocument .PrinterSettings 保存在文件中,以便用户不必每次都选择打印机设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 05:39