PrintDocument Library / Create Document
Create Document

The C1PrintDocument class supports two different approaches to create documents: Generate method and StartDoc-EndDoc method. The following sections describe each method and highlight their implementation differences.

Generate Method

The Generate method of the C1PrintDocument class allows for seamless document generation. This method is used when the render objects are added directly into the body of the document as illustrated in the code below:

C#
Copy Code
this.c1PrintDocument1.Body.Children.Add(new RenderText("Hello, World!"));
this.c1PrintDocument1.Generate();

Optionally, you can also use the RefreshModeEnum.RefreshCalculatedValues refresh mode to update data-bound and calculated values while generating the document.

StartDoc - EndDoc Method

StartDoc method initiates document generation 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. The GIF below shows a document created using StartDoc and EndDoc method:

StartDoc() and EndDoc()

The following code illustrates the StartDoc and EndDoc methods to create document:

C#
Copy Code
RenderText txt1 = new RenderText();
txt1.Text = "This is a render text sample";
c1PrintDocument1.StartDoc();
c1PrintDocument1.RenderBlock(txt1);
c1PrintDocument1.EndDoc();
Note: The StartDoc-EndDoc and the Generate methods cannot be used together. Once the StartDoc method is called, the document generation has already begun; hence, one cannot make a call to refresh or regenerate the document in between by using the Generate method.

Page Break

The C1PrintDocument class provides two ways to insert a new page or page break: NewPage method and BreakEnum enumeration.

NewPage method

The NewPage method of C1PrintDocument class is used with StartDoc and EndDoc methods to insert a new page into the document. The GIF below demonstrates the NewPage method to add a new page to the document

Adding NewPage gif

The following code snippet illustrates the NewPage method to insert a new page in the document.

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.

BreakEnum

When the Generate method is used, you can add a page break before or after a render object using the BreakEnum enumeration. The GIF below demonstrates the use of BreakEnum to insert a page break after text in the Page 1:

Adding NewPage breakenum gif

The below code illustrates use of BreakEnum to insert page break in the document:

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