I want to set the title to the document I use in PrintPreviewDialog in Windows Forms. I tried something, but for sure this isn't the way to set the TITLE to this type of Document. Can you help me do this?
private void pd_print(object sender, PrintPageEventArgs e)
{
Graphics gr = e.Graphics;
gr.DrawString("Sales", new Font(FontFamily.GenericMonospace, 12, FontStyle.Bold), new SolidBrush(Color.Black), new Point(40, 40));
}
private void tiparireToolStripMenuItem_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.pd_print);
PrintPreviewDialog dlg = new PrintPreviewDialog();
dlg.Document = pd;
dlg.ShowDialog();
}
The Preview Dialog is just a form where they hid the Text property.
PrintPreviewDialog dlg = new PrintPreviewDialog();
((Form)dlg).Text = "My Title";
dlg.Document = pd;
dlg.ShowDialog();
Related
Working on a Receipt Printer app for a local business. It's technically a re-write, and I'm approaching this as a complete refactor.
The original code looks like this:
private void btn_print_Click(object sender, EventArgs e)
{
PrintDialog pd = new PrintDialog();
PrintDocument doc = new PrintDocument();
pd.Document = doc;
doc.PrintPage += new PrintPageEventHandler(pd_printpage);
DialogResult res = pd.ShowDialog();
if (res == DialogResult.OK)
{
doc.DefaultPageSettings.Landscape = false;
doc.PrinterSettings.DefaultPageSettings.PaperSize = new PaperSize("Env10", 4, 8);
doc.Print();
}
}
private void pd_printpage(object sender, PrintPageEventArgs e)
{
Graphics gfx = e.Graphics;
SolidBrush blk = new SolidBrush(Color.Black);
Font courier = new Font("Courier New", 12);
Font lucidia = new Font("Lucida Sans", 12);
Font lucidaplus = new Font("Lucida Sans", 18);
StringFormat fmt = new StringFormat(StringFormatFlags.DirectionVertical);
/***** Create Object for Reciept *****/
Recpt rcpt = new Recpt();
rcpt.Contrib = new ContInfo();
rcpt.Pri = new Address();
rcpt.Alt = new Address();
//--- Remainder of function omitted.
Here is a better factored version that I'm currently working on.
static class Printality
{
private static SolidBrush Blk = new SolidBrush(Color.Black);
private static Font CourierBody = new Font("Courier New", 12);
private static Font LucidaBody = new Font("Lucida Sans", 12);
private static Font LucidaHeader = new Font("Lucida Sans", 18);
private static StringFormat fmt;
public static void PrintReciept(Receipt _rec)
{
PrintDialog pd = new PrintDialog();
PrintDocument doc = new PrintDocument();
doc.PrintPage += new PrintPageEventHandler(pd_printPage);
}
private static void pd_printPage(object sender, PrintPageEventArgs e)
{
Graphics gfx = e.Graphics;
fmt = new StringFormat(StringFormatFlags.DirectionVertical);
}
}
the big question is this: I'm passing a Receipt object to the function printReceipt() how should I also pass it to pd_printPage()?
If I understood your question clearly, you want to pass parameter to event.
doc.PrintPage += (sender, e) => pd_printPage(sender, e, _rec);
private static void pd_printPage(object sender, PrintPageEventArgs e, Receipt rec)
{
Graphics gfx = e.Graphics;
fmt = new StringFormat(StringFormatFlags.DirectionVertical);
}
I am using a PrintDialog and PrintDocument in c# windows application, WinForms, to print a document.
Printing working fine but I want to display the document before printing, so that I can check if PointF for DrawString is like I want it.How I can do that? Is there any tool that make it easy to define a pointF on A4 document?
private void buttonPrintShows_Click(object sender, EventArgs e)
{
PrintDialog pd = new PrintDialog();
pd.Document = printDocumentStatistic;
if (pd.ShowDialog() == DialogResult.OK)
{
printDocumentStatistic.Print();
}
}
private void printDocumentStatistic_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString("Shows:",new System.Drawing.Font("Arial", 20f), new SolidBrush(Color.Red), new PointF(35, 50));
e.Graphics.DrawString("Act:", new System.Drawing.Font("Arial", 20f), new SolidBrush(Color.Red), new PointF(35, 75));
e.Graphics.DrawString(show_name, new System.Drawing.Font("Arial", 20f), new SolidBrush(Color.Red), new PointF(100, 50));
e.Graphics.DrawString(akt_name, new System.Drawing.Font("Arial", 20f), new SolidBrush(Color.Red), new PointF(100, 75));
}
There is a simple way. Add a printPreviewDialog and give your document path to it.
printPreviewDialog1.Document = printDocument1;
You will find more info here.
How do I print out a String that I generated in Winforms? The String I'd like to print out is located in a UserControl.
This is what I already have. When I press the Printing Button, nothing is printed.
private void print_Click(object sender, EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
PrintDocument printDocument = new PrintDocument();
printDialog.Document = printDocument;
printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);
DialogResult result = printDialog.ShowDialog();
if (result == DialogResult.OK)
{
printDocument.Print();
}
PrintDocument recordDoc;
// Create the document and name it
recordDoc= new PrintDocument();
recordDoc.DocumentName = "Customer Receipt";
recordDoc.PrintPage += new PrintPageEventHandler(this.PrintReceiptPage);
// Preview document
dlgPreview.Document = recordDoc;
dlgPreview.ShowDialog();
// Dispose of document when done printing
recordDoc.Dispose();
}
In the PrintPage event try this
e1.Graphics.DrawString(s, new Font("Times New Roman", 12), new SolidBrush(Color.Black), new RectangleF(0, 0, p.DefaultPageSettings.PrintableArea.Width, p.DefaultPageSettings.PrintableArea.Height));
try
{
p.Print();
}
catch (Exception ex)
{
throw new Exception("Exception Occured While Printing", ex);
}
taken from https://social.msdn.microsoft.com/Forums/en-US/93e54c4f-fd07-4b60-9922-102439292f52/c-printing-a-string-to-printer?forum=csharplanguage
I'm trying MSDN's example of printing using PrintDocument, but it's not going so well. I've got it all to compile, but when I hit print, a "Fax Sending Settings" window pops up. Is this supposed to happen? Im trying to print, not send a fax!
What would I have to change to print this straight to the default printer instead?
Thanks!
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);
}
}
}
It's seems like a fax machine is your default printer, the easiest way to remedy this would be to add a print dialog before printing the page
PrintDialog printDialog = new PrintDialog();
printDialog.Document = printDocument;
//Show Print Dialog
if (printDialog.ShowDialog() == DialogResult.OK)
{
//Print the page
printDocument.Print();
}
This will let the user select their desired printer before printing
Firs you should declare an object of System.Drawing.Printing.PrintDocument:
private System.Drawing.Printing.PrintDocument printDocument = new System.Drawing.Printing.PrintDocument();
Then add the code described in the previous answer:
PrintDialog printDialog = new PrintDialog();
printDialog.Document = printDocument;
//Show Print Dialog
if (printDialog.ShowDialog() == DialogResult.OK)
{
//Print the page
printDocument.Print();
}
I am creating a PDF File in my application, and then I print it (works fine.)
When I print this pdf on another computer/Printer it doesn't look the same! I want it so that it always looks the same, on whichever printer I print on.
Maybe I have to set the borders? Like this:
PrinterSettings ps = new PrinterSettings();
ps.DefaultPageSettings.HardMarginX = 0;
ps.DefaultPageSettings.HardMarginY = 0;
But HardMargin is not writable. Have you guys got some ideas?
Try to set up this way:
PrintDocument printDocument1 = new PrintDocument();
var printerSettings = new System.Drawing.Printing.PrinterSettings();
printerSettings.PrinterName = "Printer name";// optional
//printerSettings.PrinterName = "HP Officejet J6400 series";
printDocument1.PrinterSettings = printerSettings;
printDocument1.PrintPage += printDocument1_PrintPage;
PrintDialog printDialog1 = new PrintDialog();
printDialog1.Document = printDocument1;
// in the dialog, you can set up the paper size, etc.
printDialog1.UseEXDialog = true;
if (printDialog1.ShowDialog() == DialogResult.OK)
{
printDocument1.Print();
}
Handler function:
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//This print form a rich textbox, but you can render pdf here.
//e.Graphics.DrawString(richTextBox1.Text, richTextBox1.Font, Brushes.Black, 100, 20);
//e.Graphics.PageUnit = GraphicsUnit.Inch;
}