How to print a String - c#

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

Related

Printer Printing Cutting Off Strings

I'm using this code to print a collection of strings from a Rich Text Box:
private void printBtn_Click(object sender, EventArgs e)
{
PrintDocument p = new PrintDocument();
p.OriginAtMargins = true;
Margins pMargins = new Margins(100, 100, 100, 100);
p.DefaultPageSettings.Margins = pMargins;
p.PrintPage += delegate (object sender1, PrintPageEventArgs e1)
{
e1.Graphics.DrawString(summaryBox.Text, 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();
this.Close();
}
catch (Exception ex)
{
throw new Exception("Error During Printing", ex);
}
}
However, long strings get cut off and don't automatically. Strings cut off to the right, and after the first page is filled, a second page is not printed and leftover content is simply ignored. Are the problems with the printer setup or with the rich text box or both? How can I make sure long strings wrap and second or third pages are printed if the content is too long?
You need to use PrintPageEventArgs.MarginBounds to get printable area within a print page.
Now your e1 is the parameter of type PrintPageEventArgs. and you will get MarginBounds like e1.MarginBounds.
So your code will be.
p.PrintPage += delegate (object sender1, PrintPageEventArgs e1)
{
e1.Graphics.DrawString(summaryBox.Text, new Font("Times New Roman", 12), new SolidBrush(Color.Black), new RectangleF(0, 0, e1.MarginBounds.Width, e1.MarginBounds.Height));
};

Set DocumentName of a Document from PrintPreviewDialog

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

C# Auto Generated Methods. How to add additional parameters

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

Implement print dialog in my code

I've been trying to solve this myself, but I haven't figured it out yet. I want to open a print dialog window when I'm pressing the btn_print button. I've called out one line I assume is not needed anymore as this is defining the size of the printed page.
Could anyone look at my code and tell me what I could do?
private void btn_print_Click(object sender, RoutedEventArgs e)
{
try
{
PrintDocument pd = new PrintDocument();
//pd.DefaultPageSettings.PaperSize = new PaperSize("A4", 827, 1169);
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.Print();
}
catch (Exception ex)
{
MessageBox.Show("An error occurred while printing", ex.ToString());
}
}
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
}
Try something like this:
PrintDocument pd = new PrintDocument();
//pd.DefaultPageSettings.PaperSize = new PaperSize("A4", 827, 1169);
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
System.Windows.Forms.PrintDialog p = new System.Windows.Forms.PrintDialog();
p.Document = pd;
if (p.ShowDialog() == System.Windows.Forms.DialogResult.OK)
pd.Print();

Printersettings in C#

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

Categories

Resources