1

我有这个用于 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);

现在,如何使用另一个按钮单击操作或在打印预览窗口中将与 printPreview 相同的输出保存为 PDF 文件。

4

2 回答 2

4

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

如果您想将该功能内置到您的应用程序中,您应该查看这个问题

于 2013-05-07T18:57:17.630 回答
1

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

在 PrintPreviewDialogue 中添加一个按钮;

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

来自:代码项目

于 2013-05-07T19:02:11.233 回答