I'm wanting to print a standard sized envelope from a webpage using c#. Does anyone know how to do something like this? I would load the data from the system for the address and pass it. I just need the basic steps for doing so.
You would have to use a activex to do so.
This might help http://www.sharewareconnection.com/print-preview-activex-control.htm
Theoretically you can do it using css http://www.w3.org/TR/css-print/. Depending on how many browsers you have to support it may be a route to a great deal of pain as they all work slightly differently.
You are likely to have some issues with page sizing and orientation as your users may me on many differant printers. Printing from flash, pdf or silverlight 3 may be the easy route out.
I did end up going with a PDF. We use PDFSharplink text, which is a free PDF creator tool for .NET. I create the PDF on the fly using the function here, and then on the page I just load this page as a new window. Here's the method I ended up creating for anyone who wants to do something similar.
protected void DisplayPDFEnvelope()
{
try
{
PdfDocument document = new PdfDocument();
PdfPage pdfpage = new PdfPage();
XUnit pdfWidth = new XUnit(4.125, XGraphicsUnit.Inch);
XUnit pdfHeight = new XUnit(9.5, XGraphicsUnit.Inch);
pdfpage.Height = pdfHeight;
pdfpage.Width = pdfWidth;
pdfpage.Orientation = PageOrientation.Landscape;
XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);
document.AddPage(pdfpage);
// Create a font
XFont font = new XFont("ARIAL", 1, XFontStyle.Regular, options);
// Get an XGraphics object for drawing beneath the existing content
XGraphics gfx = XGraphics.FromPdfPage(pdfpage, XGraphicsPdfPageOptions.Append);
// This method just returns a formatted address from the DB using \n to
// do line breaks, i.e. 'Test Person\nAddress Line1\City, State Zip'
string address = GetAddress();
// Get the size (in point) of the text
XSize size = gfx.MeasureString(address, font);
// Create a graphical path
XGraphicsPath path = new XGraphicsPath();
path.AddString(address, font.FontFamily, XFontStyle.Regular, 10,
new XPoint(345, 160), XStringFormats.Default);
// Create a dimmed pen and brush
XPen pen = new XPen(XColor.FromGrayScale(0), 0); // XColor.FromArgb(50, 75, 0, 130), 3);
XBrush brush = new XSolidBrush(); // XColor.FromArgb(50, 106, 90, 205));
// Stroke the outline of the path
gfx.DrawPath(pen, brush, path);
MemoryStream stream = new MemoryStream();
document.Save(stream, false);
Page.Response.Clear();
Page.Response.ContentType = "application/pdf";
Page.Response.AppendHeader("Content-Length", stream.Length.ToString());
Page.Response.AppendHeader("Content-Type", "application/pdf");
Page.Response.AppendHeader("Content-Disposition", "inline;filename=envelope.pdf");
Page.Response.BinaryWrite(stream.ToArray());
Page.Response.Flush();
stream.Close();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
catch (Exception ex)
{
throw ex;
}
}