gpt4 book ai didi

c# - 使用 PrintDocument 打印表单

转载 作者:太空狗 更新时间:2023-10-29 21:45:01 27 4
gpt4 key购买 nike

我正在尝试使用 PrintDocument 进行打印的 MSDN 示例,但效果并不理想。我已经完成了所有编译工作,但是当我点击打印时,会弹出一个“传真发送设置”窗口。这应该发生吗?我正在尝试打印,而不是发送传真!

我需要更改什么才能直接将其打印到默认打印机?

谢谢!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Printing;

namespace WindowsFormsApplication1
{
public partial class Form4 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components;
private System.Windows.Forms.Button printButton;
private Font printFont;
private StreamReader streamToPrint;

public Form4()
{
// The Windows Forms Designer requires the following call.
InitializeComponent();
}

// The Click event is raised when the user clicks the Print button.
private void printButton_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.Print();

}

// The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
Single yPos = 0;
Single leftMargin = e.MarginBounds.Left;
Single topMargin = e.MarginBounds.Top;
Image img = Image.FromFile("logo.bmp");
Rectangle logo = new Rectangle(40, 40, 50, 50);
using (Font printFont = new Font("Arial", 10.0f))
{
e.Graphics.DrawImage(img, logo);
e.Graphics.DrawString("Testing!", printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
}
}


// The Windows Forms Designer requires the following procedure.
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.printButton = new System.Windows.Forms.Button();

this.ClientSize = new System.Drawing.Size(504, 381);
this.Text = "Print Example";

printButton.ImageAlign =
System.Drawing.ContentAlignment.MiddleLeft;
printButton.Location = new System.Drawing.Point(32, 110);
printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
printButton.TabIndex = 0;
printButton.Text = "Print the file.";
printButton.Size = new System.Drawing.Size(136, 40);
printButton.Click += new System.EventHandler(printButton_Click);

this.Controls.Add(printButton);
}

}
}

最佳答案

似乎传真机是您的默认打印机,最简单的补救方法是在打印页面之前添加打印对话框

PrintDialog printDialog = new PrintDialog();
printDialog.Document = printDocument;
//Show Print Dialog
if (printDialog.ShowDialog() == DialogResult.OK)
{
//Print the page
printDocument.Print();
}

这将让用户在打印前选择他们想要的打印机

关于c# - 使用 PrintDocument 打印表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19321133/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com