2009-05-01 63 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打印信封。

它增加了條形碼和一切。從網絡打印。