function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
alivepjcalivepjc 

Print envelope C# .net

Hi guys,

quick question. I wrote this app in c# .net that returns the billing address for any account entered. I need to print that address in an envelope size 10. I don't know how to do it. can anybody give me a hand please.

thanks!

chris

DevAngelDevAngel

Hi alivepjc,

I would check out the Visual Studio Tools for .Net or the Word object model.  It is very conceiveable that you could write a simple .NET addin to mail merge in to an envelope.  Printing to an evnvelope requires that you choose an application that can print and Word has a lot of envelope formats available.

 

Does this sound like what you want to do?

alivepjcalivepjc

I can open up word and print it from there, but I was wondering if there is a way of sending it directly to the printer...

thanks!!

darozdaroz
You can do that w/ C# without a problem. I do that with a few sControls myself when merge just won't cut it (need nested child relations or merge on a custom object).

private PrintDocument printSheet = new PrintDocument();
private PageSettings pgSettings = new PageSettings();
private PrinterSettings prtSettings = new PrinterSettings();

// I have a ComboBox that I populate with printer choices... The last 2 lines
// here are the important parts. Set the *exact* printer name you want
// to use to prtSettings.PrinterName as below and you'll set the printer used
private void cmbPrinter_SelectedIndexChanged(object sender, System.EventArgs e)
{
ComboBox cmb = (ComboBox) sender;
prtSettings.PrinterName = (String)cmb.SelectedItem;
printSheet.PrinterSettings = prtSettings;
}

// This is the handler for the "print" button. Note that I call the
// PrintDialog for the pre-print setup. (Any last second changes, etc)
// But in your case if you want an 'immediate' print, and know the
// exact printer name, and set your settings (pgSetting.PaperSize)
// ahead of time, you might be able to skip the dialog box after
// testing and go stright to printSheet.Print()
private void btnPrint_Click(object sender, System.EventArgs e)
{
printSheet.DefaultPageSettings = pgSettings;
printSheet.PrinterSettings = prtSettings;
PrintDialog dlg = new PrintDialog();
dlg.Document = printSheet;
if (dlg.ShowDialog() == DialogResult.OK)
{
//curPage = 0; curLineItem = 0;
printSheet.Print();
}
}


There's quite a bit not shown here that is non-trivial. In our environment we have set printers for certain tasks. For example this is from my invoice printing code. Our invoices are printed on a dot matrix printer on 3 part forms. So it's always going to be on that printer. The desktops are also standardized on certain printers as well, making margins easy.

When you actually layout the objects on the page in C# you'll have to take special care to handle page margins and scaling. On an envelope it's not so bad, you'll probibly only have a few objets to layout, but on a full page it's a bit different. I'll leave that to you.
alivepjcalivepjc

awesome! thank you! I still have one more question though...

I want to print a string, how do I draw the string? how do I send it to the printSheet.Print() method?

I promise I'll get my .net books!!!

darozdaroz
Somewhere in your oe you'll need this:
printSheet.PrintPage += new PrintPageEventHandler(printSheet_PrintPage);


This'll attach the function printSheet_PrintPage() to the page so that it knows how to 'draw' on it.

The function call will look like this:
private void printSheet_PrintPage(object sender, PrintPageEventArgs e) {}


Once in the function call you call
e.Graphics.DrawString()
to draw the text. Look up the call on MSDN for more detail.