0

我要做的是从 .txt 创建一个 Windows Journal (.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 文件的参考资料,所以我愿意接受任何想法。

4

2 回答 2

0

To get Journal Note Writer as printer you would have to add it into the printer preferences -> Add Printer (I wonder whether it could be done programmatically).

Anyways you can always get a print of a plain .txt file as described here at MSDN.

于 2012-12-13T14:43:21.093 回答
0

在为这个问题挣扎了一段时间后,我决定回去尝试直接通过应用程序处理打印,而不通过记事本。我之前尝试这个时遇到的问题是更改纸张大小会破坏生成的 .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;
    }
}
于 2012-12-13T20:10:58.510 回答