PDF for .NET
Web アプリケーションの操作

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:
    C#
    コードのコピー
    using C1.Pdf;
    using System.Drawing;
    
  3. Create a Pdf document using the C1PdfDocument class and add content to it using the following code.
    C#
    コードのコピー
    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;
        //文字列を中央揃えにします。
        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.
    C#
    コードのコピー
    protected void RenderPDF(C1PdfDocument doc)
    {
        // PDF ドキュメントをメモリベースに描画します
        // PDF ストリーム.
    
        MemoryStream ms = new MemoryStream();
        doc.Save(ms);
        // 応答オブジェクトを取得します
        int length;
        HttpResponse rsp = this.Response;
        // 解除します
        rsp.Clear();
        // PDF ストリームを応答バッファーに書き込みます
        rsp.ContentType = "Application/pdf";
        length = (int)ms.Length;
        rsp.Body.Write(ms.GetBuffer(), 0, length);
    }