1

I have this code for PrintPreview and print.

private void button2_Click_1(object sender, EventArgs e)
    {
        printPreviewDialog1.Document = printDocument1;
        printPreviewDialog1.ShowDialog();     
    }
private void printDocument1_PrintPage_1(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        e.Graphics.DrawImage(Logo.Image, 800, 100);
        e.Graphics.DrawString(label20.Text, label20.Font, Brushes.Black, 134, 100);
        e.Graphics.DrawString(label22.Text, label22.Font, Brushes.Black, 462, 100);
        e.Graphics.DrawString(textBox101.Text, textBox101.Font, Brushes.Black, 134, 230);
        e.Graphics.DrawString(textBox104.Text, textBox104.Font, Brushes.Black, 134, 270);

Now how can I save the same output as the printPreview as a PDF file with another buttonClick action or in the print Preview window.

user2162570
  • 71
  • 2
  • 10

2 Answers2

4

If you already using the printing features of WinForms it would be the simplest solution install a PDF printer program, e.g. PDFCreator. After installing it can be used like a real printer, but saves a PDF file.

If you want to build in the feature into your application, you should check out this question.

Community
  • 1
  • 1
Fox32
  • 13,126
  • 9
  • 50
  • 71
1

If you are intresteed in creating your own,You can use this .

To add a button in PrintPreviewDialougue ;

   class CustomPrintPreviewDialog : System.Windows.Forms.PrintPreviewDialog
    {
        public CustomPrintPreviewDialog()
            : base()

        {
            if(this.Controls.ContainsKey("toolstrip1"))
            {

                ToolStrip tStrip1 = (ToolStrip)this.Controls["toolstrip1"];
                ToolStripButton button1 = new ToolStripButton();
                button1.Text = "Save";
                button1.Click += new EventHandler(SaveDocument);
                button1.Visible = true;
                tStrip1.Items.Add(button1);
            }
        }

        protected void SaveDocument(object sender, EventArgs e)
        {
            // code for save the document
            MessageBox.Show("OK");
        }
    }

From :Codeproject

Zigma
  • 529
  • 6
  • 37