2009-05-12 2 views
1

나는 PDF 봉투를 만들 수있는 능력이 필요했기 때문에 좋은 해결책을 찾지 못해서 이것이 흥미있는 것으로 생각했습니다.PDF 봉투 만들기

답변

2

PDFSharp 무료 PDF 문서 도구를 사용합니다. 그것은 꽤 잘 해결되었습니다. 이렇게하는 방법은 다음과 같습니다. 그것은 새로운 pdf 문서를 만들고, 봉투 크기를 정하고, 주소를 중심에 둘 것입니다. GetAddress()는 DB에서 주소를 검색하는 데 사용되는 메서드 일뿐입니다. \ n을 사용하여 주소의 다른 줄을 줄 바꿈하십시오.

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 
     XGraphics gfx = XGraphics.FromPdfPage(pdfpage, XGraphicsPdfPageOptions.Append); 

     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); 
     XBrush brush = new XSolidBrush(); 

     // 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; 
    } 
} 
+0

+1 : 시간이 절약되었습니다. 감사합니다. –

+0

PDFSharp는 최고입니다. – devlord

0

PDFSharp 때문에, 좋은 iTextSharp,이 iText의 자바 포트, 주변의 첫번째 PDF 라이브러리 중 하나입니다.