【问题标题】:Pull Up Print Dialog In Notepad在记事本中拉出打印对话框
【发布时间】:2012-12-13 14:08:36
【问题描述】:

我正在尝试从 .txt 创建一个 Windows 日记 (.jrn) 文件。这种转换可以通过打印到虚拟的“Journal Note Writer”打印机来完成。我一直在努力使用几种不同的方法来让它工作一段时间,所以我决定尝试简化事情(我希望如此)。

我目前拥有的

Process p = new Process();
p.StartInfo = new ProcessStartInfo()
{
    FileName = fileToOpen, // My .txt file I'd like to convert to a .jrn
    CreateNoWindow = true,
    Arguments = "-print-dialog -exit-on-print"
};
p.Start();

这会在记事本中打开文件,但不会打开打印对话框。我希望打开打印对话框,并且最好能够在打印对话框中指定一些默认选项。

我尝试过的另一件事是(在另一个 SO 问题中找到):

Process p = new Process( );
p.StartInfo = new ProcessStartInfo( )
{
    CreateNoWindow = true,
    Verb = "print",
    FileName = fileToOpen
};
p.Start( );

这样做的问题是它只是自动将文件打印到默认打印机(物理打印机)而没有给我更改它的选项。

我在寻找什么

此时,我只是在寻找将 .txt 打印到“Windows Note Writer”的任何方法。我尝试在不通过外部应用程序的情况下进行打印,但也遇到了一些麻烦。我找不到任何其他关于转换为 .jrn 文件的参考资料,所以我愿意接受任何想法。

【问题讨论】:

  • 也许这篇文章有帮助? support.microsoft.com/kb/322091对不起,我自己没试过,但我在上班……
  • @Picacodigos - 谢谢你的链接。如果我无法通过记事本进行打印,看起来可能值得一试。我已经尝试过与此有些相似的方法,但我希望不必使用打印机帮助程序类。
  • 只是另一个想法。您是否尝试过 ShellExecute(ing) DOS 命令“TYPE > LPT1”?
  • stackoverflow.com/q/1472153/18192 建议这篇 MSDN 文章:msdn.microsoft.com/en-us/library/cwbe712d.aspx。它比 Picacodigos 的建议要简单一些,因为它专门指示如何打印文本文件,而不是一般地打印数据。

标签: c#


【解决方案1】:

要将Journal Note Writer 作为打印机,您必须将其添加到打印机首选项中 -> 添加打印机(我想知道是否可以通过编程方式完成)。

无论如何,您都可以按此处MSDN 所述获取纯 .txt 文件的打印件。

【讨论】:

    【解决方案2】:

    在为这个问题苦苦挣扎了一段时间后,我决定回去尝试直接通过应用程序处理打印,而不是通过记事本。我之前尝试此操作时遇到的问题是更改纸张大小会破坏生成的 .jrn 文件(打印输出将为空白)。事实证明,更改某些纸张尺寸设置对于在非本地纸张尺寸上打印是必要的。

    以下是最终代码,以防其他人遇到此问题。感谢大家的帮助。

    private void btnOpenAsJRN_Click(object sender, EventArgs e) {
        string fileToOpen = this.localDownloadPath + "\\" + this.filenameToDownload;
        //Create a StreamReader object
        reader = new StreamReader(fileToOpen);
        //Create a Verdana font with size 10
        lucida10Font = new Font("Lucida Console", 10);
        //Create a PrintDocument object
        PrintDocument pd = new PrintDocument();
    
        PaperSize paperSize = new PaperSize("Custom", 400, 1097);
        paperSize.RawKind = (int)PaperKind.Custom;
        pd.DefaultPageSettings.PaperSize = paperSize;
        pd.DefaultPageSettings.Margins = new Margins(20, 20, 30, 30);
    
        pd.PrinterSettings.PrinterName = ConfigurationManager.AppSettings["journalNotePrinter"];
        pd.DocumentName = this.filenameToDownload;
        //Add PrintPage event handler
        pd.PrintPage += new PrintPageEventHandler(this.PrintTextFileHandler);
    
        //Call Print Method
        try {
            pd.Print();
        }
        finally {
            //Close the reader
            if (reader != null) {
                reader.Close();
            }
            this.savedJnt = fileToOpen.Replace("txt", "jnt");
            System.Threading.Thread.Sleep(1000);
            if (File.Exists(this.savedJnt)) {
                lblJntSaved.Visible = true;
                lblJntSaved.ForeColor = Color.Green;
                lblJntSaved.Text = "File successfully located.";
                // If the file can be found, show all of the buttons for completing 
                // the final steps.
                lblFinalStep.Visible = true;
                btnMoveToSmoketown.Visible = true;
                btnEmail.Visible = true;
                txbEmailAddress.Visible = true;
            }
            else {
                lblJntSaved.Visible = true;
                lblJntSaved.ForeColor = Color.Red;
                lblJntSaved.Text = "File could not be located. Please check your .jnt location.";
            }
        }
    }
    
    private void PrintTextFileHandler(object sender, PrintPageEventArgs ppeArgs) {
        //Get the Graphics object
        Graphics g = ppeArgs.Graphics;
        float linesPerPage = 0;
        float yPos = 0;
        int count = 0;
        //Read margins from PrintPageEventArgs
        float leftMargin = ppeArgs.MarginBounds.Left;
        float topMargin = ppeArgs.MarginBounds.Top;
        string line = null;
        //Calculate the lines per page on the basis of the height of the page and the height of the font
        linesPerPage = ppeArgs.MarginBounds.Height /
        lucida10Font.GetHeight(g);
        //Now read lines one by one, using StreamReader
        while (count < linesPerPage &&
        ((line = reader.ReadLine()) != null)) {
            //Calculate the starting position
            yPos = topMargin + (count *
            lucida10Font.GetHeight(g));
            //Draw text
            g.DrawString(line, lucida10Font, Brushes.Black,
            leftMargin, yPos, new StringFormat());
            //Move to next line
            count++;
        }
        //If PrintPageEventArgs has more pages to print
        if (line != null) {
            ppeArgs.HasMorePages = true;
        }
        else {
            ppeArgs.HasMorePages = false;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-05
      • 2019-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-15
      相关资源
      最近更新 更多