Reports for WinForms
ドキュメントに段落を追加する
クイックスタート > 印刷プレビューを開始する > 文字列を追加する > ドキュメントに段落を追加する

C1PrintDocument のすべてのコンテンツは、描画オブジェクトによって表されます。C1Preview アセンブリは、RenderObject から派生されたクラスの階層を提供します。これらのクラスは、テキスト、画像などのさまざまなタイプのコンテンツを表すように設計されています。たとえば、先ほどは  RenderTextクラスを使用して、ドキュメントにテキスト行を追加しました。このセクションでは、RenderParagraph クラスを使用して、テキストの段落を作成する方法を説明します。段落では、複数のスタイル、インライン画像、およびハイパーリンクを使って描画される複数のテキストフラグメントを組み合わせることができます。

メモ:このトピック内のサンプルコードは、「using C1.C1Preview;」ディレクティブ(C# の場合。他の言語の場合は相当する構文)がファイルに挿入されていることを前提としています。したがって、完全修飾された型名(C1.C1Preview.RenderText など)ではなく、クラス名部分(RenderText)だけを使用します。

RenderParagraph は、次のコードを使って作成できます。

Visual Basic コードの書き方

Visual Basic
コードのコピー
Dim rp As New RenderParagraph()

C# コードの書き方

C#
コードのコピー
RenderParagraph rp = new RenderParagraph();

次の場合は、RenderText ではなく、段落を使用する必要があります。

段落のコンテンツは、いくつかの ParagraphObject オブジェクトから成ります。ParagraphObject は抽象基本クラスです。継承クラスとして ParagraphTextParagraphImage があり、これらはそれぞれテキストフラグメントとインライン画像を表します。段落にコンテンツを挿入するには、この2つの型のオブジェクトを作成し、それらを RenderParagraph.Content コレクションに追加します。これらのオブジェクトを作成/設定しやすいように、さまざまなオーバーロードコンストラクタとプロパティが提供されています。段落内ハイパーリンクを作成するには、ハイパーリンクにする段落オブジェクトの Hyperlinkプロパティを指定します。別の方法では、次の例に示すように、ショートカットメソッド AddTextAddImage および AddHyperlink、のさまざまなオーバーロードを使用します。

Visual Basic コードの書き方

Visual Basic
コードのコピー
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    ' 段落を作成します
    Dim rpar As New RenderParagraph()    
    Dim f As New Font(rpar.Style.Font, FontStyle.Bold)
                
    rpar.Content.AddText("This is a paragraph. This is normal text. ")    
    rpar.Content.AddText("This text is bold. ", f)    
    rpar.Content.AddText("This text is red. ", Color.Red)    
    rpar.Content.AddText("This text is superscript. ", TextPositionEnum.Superscript)    
    rpar.Content.AddText("This text is bold and red. ", f, Color.Red)    
    rpar.Content.AddText("This text is bold and red and subscript. ", f, Color.Red, TextPositionEnum.Subscript)    
    rpar.Content.AddText("This is normal text again. ")    
    rpar.Content.AddHyperlink("This is a link to the start of this paragraph.", rpar.Content(0))    
    rpar.Content.AddText("Finally, here is an inline image: ")    
    rpar.Content.AddImage(Me.Icon.ToBitmap())    
    rpar.Content.AddText(".")

    ' 段落をドキュメントに追加します
    Me.C1PrintDocument1.Body.Children.Add(rpar)    
    Me.C1PrintDocument1.Generate()
                
End Sub

C# コードの書き方

C#
コードのコピー
private void Form1_Load(object sender, EventArgs e)    
{    
    // 段落を作成します
    RenderParagraph rpar = new RenderParagraph();    
    Font f = new Font(rpar.Style.Font, FontStyle.Bold);

    rpar.Content.AddText("This is a paragraph. This is normal text. ");    
    rpar.Content.AddText("This text is bold. ", f);    
    rpar.Content.AddText("This text is red. ", Color.Red);    
    rpar.Content.AddText("This text is superscript. ", TextPositionEnum.Superscript);    
    rpar.Content.AddText("This text is bold and red. ", f, Color.Red);    
    rpar.Content.AddText("This text is bold and red and subscript. ", f, Color.Red, TextPositionEnum.Subscript);    
    rpar.Content.AddText("This is normal text again. ");    
    rpar.Content.AddHyperlink("This is a link to the start of this paragraph.", rpar.Content[0]);    
    rpar.Content.AddText("Finally, here is an inline image: ");    
    rpar.Content.AddImage(this.Icon.ToBitmap());    
    rpar.Content.AddText(".");

    // 段落をドキュメントに追加します    
    this.c1PrintDocument1.Body.Children.Add(rpar);    
    this.c1PrintDocument1.Generate();
    
}

プログラムを実行し、次の点を確認します。

テーブルは次のように表示されます。