Click here to Skip to main content
15,918,267 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi I have PrintPreviewDialog1 in my windows application. This print preview dialog will be populated from the main form. So the default control of the PrintPreviewDialog has print ,zoom etc.
My question: How do I edit this control programaticaly to add save button ? I am tring to save the previewed document as PDF.
Posted

1 solution

Hi create a custom preview control like this..

the class document should have a reference to using System.Windows.Forms;

C#
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");
    }
}


Implement your save logic in the save document function.

Then you can instantiate this control as

<br />
<br />
            CustomPrintPreviewDialog dialog1 = new CustomPrintPreviewDialog();<br />
            dialog1.Show();<br />


and set whaterver properties or call methods as normal print preview dialog.


But basically save to PDF is just use the pdf printer. This control already have a print button. As you asked I gave given this way. Upto you how you want to implement the save function.
 
Share this answer
 
Comments
Member 13580434 23-Dec-17 23:25pm    
hi sir
i have the same question so please tell me where to add these codes
do i create a new class and add these codes in class or anywhere else
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");
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900