2009-05-01 60 views
1

我想要使用c#从网页打印标准大小的信封。有谁知道如何做这样的事情?我会从系统加载地址的数据并传递给它。我只需要这样做的基本步骤。从网站打印信封?

+1

在服务器或客户端? – 2009-05-01 23:01:42

回答

2

我确实最终用PDF格式。我们使用PDFSharp,这是一个免费的PDF创作者工具。我使用此处的函数即时创建PDF,然后在页面上,我只是将此页面作为新窗口加载。这是我最终为任何想要做类似事情的人创建的方法。

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

理论上你可以使用CSS http://www.w3.org/TR/css-print/做到这一点。取决于你需要支持多少浏览器,它可能是一个很大的痛苦,因为它们的工作方式稍有不同。

你很可能与页面大小和方向的一些问题为您的用户或许我的许多歧打印机。从flash,pdf或silverlight 3打印可能是简单的路线。

+0

对PDF的+1,尽管CSS *可能能够做到这一点 - PDF是明显的第一选择。 – 2009-05-01 23:14:24

0

您可以从this site打印信封。

它增加了条形码和一切。从网络打印。