我需要创建一个PDF信封的能力,并没有找到一个好的解决办法这样做,所以我认为这可能是某些利益。

有帮助吗?

解决方案

我们使用 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;
    }
}

其他提示

PDFSharp好,所以是 iTextSharp的,iText的的渣口,第一PDF库之一周围。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top