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.
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.
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
Related
I am facing a problem to print using print document C#. When I am going to print and input 1 to 3 pages from 9 pages using printer setting and then command for print, this command cannot print 1 to 3 pages, this command print by default all documents. How can I fix this?
This is the code:
private void PrintPreview_Click(object sender, EventArgs e)
{
ToolStripButton b = new ToolStripButton();
b.Text = "print";
((ToolStrip)(printPreviewDialog1.Controls[1])).Items.RemoveAt(0);
((ToolStrip)(printPreviewDialog1.Controls[1])).Items.Insert(0, b);
((ToolStripButton)((ToolStrip)printPreviewDialog1.Controls[1]).Items[0]).Click += new System.EventHandler(this.button2_Click);
printPreviewDialog1.WindowState = FormWindowState.Maximized;
if (printPreviewDialog1.ShowDialog() == DialogResult.OK)
{
printPreviewDialog1.Show();
}
}
private void Print_Click(object sender, EventArgs e)
{
PrintDialog p1 = new PrintDialog();
p1.AllowSelection = true;
p1.AllowSomePages = true;
if (p1.ShowDialog ()== DialogResult.OK)
{
printDocument1.Print();
}
}
I'm currently trying to learn how to use the print functions in C#, and I have a problem when I'm trying to print out labels in my windows forms application.
What I want to do, is when I click button 1, I want to put the text from the labels (or draw an image of them) into a document, that can be printed.
I'm still new to programming, so this print function is very confusing for me.
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawString(label1.Text, label2.Text, label3.Text, label4.Text, label5.Text, label6.Text, label7.Text, label8.Text, label9.Text);
}
private void button1_Click(object sender, EventArgs e)
{
this.printDocument1.PrintPage += new
System.Drawing.Printing.PrintPageEventHandler
(this.printDocument1_PrintPage);
}
private void PrintPage(object o, PrintPageEventArgs e)
{
System.Drawing.Image img = System.Drawing.Image.FromFile("C://gul.PNG");
Point loc = new Point(10, 10);
e.Graphics.DrawImage(img, loc);
}
What do I need to do different, to be able to do this?
Use the Form.DrawToBitmap() method.
For example a form like this:
When the print button is pressed:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
var pd = new PrintDocument();
pd.PrintPage+=(s,ev)=>
{
var bmp = new Bitmap(Width, Height);
this.DrawToBitmap(bmp, new Rectangle(Point.Empty, this.Size));
ev.Graphics.DrawImageUnscaled(bmp, ev.MarginBounds.Location);
ev.HasMorePages=false;
};
var dlg = new PrintPreviewDialog();
dlg.Document=pd;
dlg.ShowDialog();
}
}
With the result:
Good day! I was trying to print a png file using a Picturebox as my button.
However, I can't seem to print. Could you please help me? or Give me a link that will guide me on how to print on a default printer using C# VS 2010
private void pictureBox2_Click(object sender, EventArgs e)
{
using (PrintDocument pd = new PrintDocument())
{
using (PrintDialog printDialog = new PrintDialog())
{
if (printDialog.ShowDialog() == DialogResult.Yes)
{
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();
}
}
}
}
private void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
System.Drawing.Image img = Image.FromFile(#"C:\Coke\res10.png");
e.Graphics.DrawImage(img, 0,0);
}
Found a solution for this.
PrintDocument pd = new PrintDocument();
pd.PrintPage += (sender, args) =>
{
Image i = Image.FromFile("C://tesimage.PNG");
args.Graphics.DrawImage(i, args.PageBounds);
};
pd.Print();
This is a simple one, but easy to miss..
You are doing just about everything right. Just one tiny thing is not 'OK':
You need to change
if (printDialog.ShowDialog() == DialogResult.Yes)
to
if (printDialog.ShowDialog() == DialogResult.OK)
It is not a question, after all, so it shows 'OK' and 'Cancel' not 'Yes' and 'No'.
I am using ExportToXlsx() method in order to export data from GridView to Excel Sheet.
this seams to export and format the data just fine, even my conditional formatting is being exported as is.
The only thing I would like to know is that how do I add a logo and header in that excel file without having to add it manually. Like in a Crystal Report we can add these things at the design time. Is there something that can be done ?
You can do that with PrintableComponentLink and write a CreateReportHeaderArea event handler:
private void button_Export_Click(object sender, EventArgs e)
{
var y = new DevExpress.XtraPrinting.PrintableComponentLink(new PrintingSystem());
y.Component = gridControl1;
y.CreateReportHeaderArea += y_CreateReportHeaderArea;
if (saveFileDialog_Report.ShowDialog() == DialogResult.OK)
{
if (saveFileDialog_Report.FileName.ToLower().EndsWith("xlsx"))
{
y.ExportToXlsx(saveFileDialog_Report.FileName);
}
}
}
void y_CreateReportHeaderArea(object sender, CreateAreaEventArgs e)
{
DevExpress.XtraPrinting.TextBrick brick;
brick = e.Graph.DrawString("My Report Title Here", Color.Navy, new RectangleF(0, 0, 500, 40), DevExpress.XtraPrinting.BorderSide.None);
brick.Font = new Font("Arial", 16);
brick.StringFormat = new DevExpress.XtraPrinting.BrickStringFormat(StringAlignment.Center);
}
In addition to Milen's answer that added the header, I was also able to add an image to the report like below
void y_CreateReportHeaderArea(object sender, CreateAreaEventArgs e)
{
Image newimage = Image.FromFile("path");
DevExpress.XtraPrinting.ImageBrick iBrick;
iBrick = e.Graph.DrawImage(newimage, new RectangleF(0,0,40,40));
DevExpress.XtraPrinting.TextBrick brick;
brick = e.Graph.DrawString("My Report Title Here", Color.Navy, new RectangleF(40, 0, 500, 40), DevExpress.XtraPrinting.BorderSide.None);
brick.Font = new Font("Arial", 16);
brick.StringFormat = new DevExpress.XtraPrinting.BrickStringFormat(StringAlignment.Center);
}
just make sure that you keep the coordinates and size correct so the image and text dont overlap.
I have a bitmap I want the user to see before he prints it. So I open for him print preview, if the user decides to print I want to execute some code.
The problem is, printPreviewDialog will not return an answer. This may be because it has only a print button and close button, but no print-and-close so I can know the user decided to print.
If you have a solution for that I'll be happy, if you think it's not the best way to do so please tell me.
code:
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(Print_Page);
PrintPreviewDialog pritdlg = new PrintPreviewDialog();
pritdlg.Document = pd;
if (pritdlg.ShowDialog() == DialogResult.OK)
pd.Print();
else
MessageBox.Show("you have canceled print");
private void Print_Page(object o, PrintPageEventArgs e)
{
e.Graphics.DrawImage(target, 0,0);
}
Subscribe to the EndPrint event of the document you are sending to the printPreviewDialog control, then check the PrintAction in its PrintEventArgs argument.
Example:
private void buttonPrintPreview_Click(object sender, EventArgs e)
{
PrintPreviewDialog printDialog = new PrintPreviewDialog();
printDialog.Document = yourDocument;
yourDocument.EndPrint += doc_EndPrint; // Subscribe to EndPrint event of your document here.
printDialog.ShowDialog();
}
void doc_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
if (e.PrintAction == System.Drawing.Printing.PrintAction.PrintToPrinter)
{
// Printing to the printer!
}
else if (e.PrintAction == System.Drawing.Printing.PrintAction.PrintToPreview)
{
// Printing to the preview dialog!
}
}