在打印预览中另存为PDF

时间:2013-05-07 18:54:10

标签: c# pdf print-preview

我有PrintPreview和打印的代码。

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

现在,如何使用另一个buttonClick操作或打印预览窗口将printPreview的输出保存为PDF文件。

2 个答案:

答案 0 :(得分:4)

如果您已经使用WinForms的打印功能,那么安装PDF打印机程序将是最简单的解决方案,例如: PDFCreator。安装后,它可以像真正的打印机一样使用,但保存PDF文件。

如果您想在功能中加入该功能,请查看this question

答案 1 :(得分:1)

如果您有兴趣创建自己的,可以使用this

在“打印预览”对话框中添加按钮;

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

来自:Codeproject