PrintDocument for WinForms | ComponentOne
PrintDocument Library / Create Document
In This Topic
    Create Document
    In This Topic

    There are two ways to create documents in PrintDocument: StartDoc EndDoc method and Generate method. This section discusses the StartDoc and EndDoc methods. The Generate method is discussed in QuickStart.

    StartDoc - EndDoc Method

    StartDoc method marks the beginning of the document while EndDoc method marks its end. To add objects to the document block, StartDoc uses available methods such as RenderBlock, RenderDirect, or RenderInline. For more information on rendering objects, see RenderObjects topic.

    To below image shows a document created using StartDoc and EndDoc method.

    StartDoc() and EndDoc()

    The following code snippet illustrates the use of StartDoc and EndDoc methods.

    C#
    Copy Code
    RenderText txt1 = new RenderText();
    txt1.Text = "This is a render text sample";
    c1PrintDocument1.StartDoc();
    c1PrintDocument1.RenderBlock(txt1);
    c1PrintDocument1.EndDoc();
    

    Insert Page Break

    The C1PrintDocument class provides two ways to insert a new page or page break: NewPage method and BreakEnum enumeration. The NewPage method is used with StartDoc and EndDoc methods while BreakEnum is used with the Generate method of the C1PrintDocument class. The following code snippet demonstrates use of the NewPage method.

    C#
    Copy Code
    RenderText txt1 = new RenderText();
    txt1.Text = "This is line 1";
    RenderText txt2 = new RenderText();
    txt2.Text = "This is line 2";
    c1PrintDocument1.StartDoc();
    c1PrintDocument1.RenderBlock(txt1);
    c1PrintDocument1.AllowNonReflowableDocs = true;
    c1PrintDocument1.NewPage();
    c1PrintDocument1.RenderBlock(txt2);
    c1PrintDocument1.EndDoc();
    
    Note: To use the NewPage method, set AllowNonReflowableDocs property of C1PrintDocument class to true.

    You can visualize the output of above code snippet through the below image:

    Adding NewPage gif

    If you use Generate method, you can add a new page or a page break using the render object with the BreakEnum enumeration, as demonstrated in the code snippet below.

    C#
    Copy Code
    c1PrintDocument1.Body.Children.Add(txt1);
    c1PrintDocument1.Body.Children.Add(txt2);
    txt1.BreakAfter = BreakEnum.Page;
    this.c1PrintDocument1.Generate();
    

    You can visualize the output of above code snippet through the below image.

    Adding NewPage breakenum gif