0

私がやろうとしているのは、.txtからWindows Journal(.jrn)ファイルを作成することです。この変換は、仮想の「ジャーナルノートライター」プリンターに印刷することで実行できます。私はこれを機能させるためのいくつかの異なる方法にしばらく苦労してきたので、物事を単純化することを試みることにしました(私は願っています)。

私が現在持っているもの

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();

これにより、ファイルはメモ帳で開きますが、印刷ダイアログは開きません。印刷ダイアログを開き、理想的には印刷ダイアログでいくつかのデフォルトオプションを指定できるようにしたいと思います。

私が試したもう1つのことは、これです(別のSOの質問で見つかりました):

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

これに伴う問題は、ファイルを変更するオプションを与えずに、ファイルをデフォルトのプリンター(物理プリンター)に自動的に印刷することです。

私が探しているもの

この時点で、「WindowsNoteWriter」に.txtを印刷する方法を探しています。外部アプリケーションを使わずに印刷してみましたが、それも問題でした。.jrnファイルに変換するための他の参照を見つけることができなかったので、私はどんなアイデアにもオープンです。

4

2 に答える 2

0

プリンターとして取得Journal Note Writerするには、プリンター設定->プリンターの追加に追加する必要があります(プログラムで実行できるかどうか疑問に思います)。

とにかく、ここで説明されているように、いつでもプレーンな.txtファイルの印刷物を入手できます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 に答える