[]
        
(Showing Draft Content)

Work with Web Applications

With PDF for .NET, you can write content directly into a web page's output buffer with no temporary files. To do so, follow the steps mentioned below to create Web Application with PDF for .NET.

  1. In your Web application project, add the C1.Pdf NuGet package.

  2. From Solution Explorer, open Pages | Index.cshtml | Index.cshtml.cs and add the following references:

    using C1.Pdf;
    using System.Drawing;
    
  3. Create a Pdf document using the C1PdfDocument class and add content to it using the following code.

    public void OnGet()
    {
        C1PdfDocument pdf = new C1PdfDocument();
        Font font = new Font("Arial", 12);
        RectangleF rect = new RectangleF(72, 72, 100, 50);
        string text = "Some long string to be rendered into a small rectangle. ";
        text = text + text + text + text + text + text;
        // Center align string.
        StringFormat sf = new StringFormat();
        sf.Alignment = StringAlignment.Center;
        sf.LineAlignment = StringAlignment.Center;
        pdf.DrawString(text, font, Brushes.Black, rect, sf);
        pdf.DrawRectangle(Pens.Gray, rect);
        RenderPDF(pdf);
    }
    
  4. Render the PDF document using the following code.

    protected void RenderPDF(C1PdfDocument doc)
    {
        // Render PDF document into memory-based
        // PDF stream.
    
        MemoryStream ms = new MemoryStream();
        doc.Save(ms);
        // Get response object.
        int length;
        HttpResponse rsp = this.Response;
        // Clear it
        rsp.Clear();
        // Write PDF stream into response buffer
        rsp.ContentType = "Application/pdf";
        length = (int)ms.Length;
        rsp.Body.Write(ms.GetBuffer(), 0, length);
    }