How do I trigger the PrintPreviewDialog?
Asked Answered
K

1

0
using (PrintDialog printDialog1 = new PrintDialog())
{
   if (printDialog1.ShowDialog() == DialogResult.OK)
   {
       System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(saveAs.ToString());
       info.Arguments = "\"" + printDialog1.PrinterSettings.PrinterName + "\"";
       info.CreateNoWindow = true;
       info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
       info.UseShellExecute = true;
       info.Verb = "PrintTo";
       System.Diagnostics.Process.Start(info);
   }
}

The above code works fine. I just don't know how to change the code so that I can preview the Word document first.

Knell answered 24/1, 2013 at 21:46 Comment(6)
Have you tried using PrintPreviewDialog?Connotation
Did you check MSDN PrintPreviewDialog example code?Potts
or try something like this in your code PrintDialog printDialog = new PrintDialog(); printDialog.ShowDialog();Fulvous
Forget to mention, as per the title, actually i am asking where PrintPreviewDialog comes into play?Knell
AustinSalonen, Pilgerstorfer: I've seen the documentation. But as my comment to @Brian, I failed to see how to bind Word doc to PrinDocument doc. DJ, I've use the code in using().Knell
I am looking into this now Iyas, since you have piqued my interest :)Inclination
I
1

Okay, so I worked on this last night after getting home and I believe I figured it out. It's NOT perfect but, it does get you moving in the right direction. BTW, I created a simple WinForms app for this, and you will need to edit the code to suit your needs.

The code:

namespace WindowsFormsApplication1
{
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
        PrintPreviewDialog dlg = new PrintPreviewDialog();
        dlg.Document = doc;
        doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.PrintPage);
        dlg.ShowDialog();
    }

    private void PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        try
        {
            string fileName = @"C:\Users\brmoore\Desktop\New Text Document.txt";
            StreamReader sr = new StreamReader(fileName);
            string thisIsATest = sr.ReadToEnd();
            sr.Close();
            System.Drawing.Font printFont = new System.Drawing.Font("Arial", 14);
            e.Graphics.DrawString(thisIsATest, printFont, Brushes.Black, 100, 100);
        }

        catch (Exception exc)
        {
            MessageBox.Show(exc.ToString());
        }
    }
}

}

Inclination answered 24/1, 2013 at 21:58 Comment(7)
I've seen this code. The problem is I don't know how to bind the doc to my Word document. So I use the code I've mention earlier. But this code doesn't have preview dialog.Knell
I use Word Interop the create Word doc that I'm trying to print. But this two lines of code produce nothing. wordApp.Visible = true; aDoc.PrintPreview();. Thats why I try using PrintPreviewDialog, PrintDialog etc.Knell
whoa, this is the first time I can't get my problem solved here.Knell
I highly appreciate your try. Two thumbs up. But I cannot accept this as the answer for my question, because this code also in my knowledge. It only works with txt document, not Word document. Your answer does give me the direction to follow, now I'll try searching for "reading Word content streamreader c#". Any idea of good keywords?Knell
Always happy to help Iyas. In fact, I am still working on a solution for this - as time allows. Work has been pretty busy of late.Inclination
Brian, I'm away from my computer. Can you check if some part of your code could be replaced with MemoryStream mem which contains in-Memory Open XML Doc. Source: blogs.msdn.com/b/ericwhite/archive/2008/12/10/…Knell
Iyas, I will definitely look into that as soon as possible. I have kids and we are nearing their bedtime :)Inclination

© 2022 - 2024 — McMap. All rights reserved.