Creating Paragraphs Using Rendering Objects in C1PrintDocument
Creating paragraphs using rendering objects in C1PrintDocument demonstrates how to use the RenderText method to render paragraphs of text. It also shows how to set up and use styles for your paragraphs.
1. Create a new Windows Forms application. Drag and drop a C1PrintPreview control onto your form. Drag and drop a C1PrintDocument component onto your form – it will appear in the components' tray below the form. The preview will have the default name c1PrintPreview1, the document c1PrintDocument1. Set the value of the Document property of c1PrintPreview1 control to c1PrintDocument1, so that the preview will show the document when the application runs. Double click on the form to create a handler for the Form_Load event – this is where all code shown below will be written.
2. Create a string variable to hold the text, an integer variable to count paragraphs, and an integer variable to count chapters. Assign the Chapter variable and the MainText to the RenderText class. The RenderText will render the text from the Chapter and MainText variables to the document. Assign some text to the string and have the sentence repeat three times.
Dim s$
Dim iChapter%, iParagraph%
' Will draw chapters.
Dim Chapter As New C1.C1PrintDocument.RenderText(Me.C1PrintDocument1)
' Will draw main text.
Dim MainText As New C1.C1PrintDocument.RenderText(Me.C1PrintDocument1)
s = "This sentence will repeat three times and it will appear under a chapter title. "
s = s & s & s
• C#
string s;
int iChapter, iParagraph;
// Will draw chapters.
RenderText Chapter = new RenderText(c1PrintDocument1);
// Will draw main text.
RenderText MainText = new RenderText(c1PrintDocument1);
s = "This sentence will repeat three times and it will appear under a chapter title. ";
s = s + s + s;
3. In this example, we will use the AlignChildrenHorz property of the C1DocStyle class to set the horizontal alignment of the paragraph to justify.
Me.C1PrintDocument1.Style.AlignChildrenHorz = C1.C1PrintDocument.AlignHorzEnum.Justify
• C#
this.c1PrintDocument1.Style.AlignChildrenHorz = C1.C1PrintDocument.AlignHorzEnum.Justify;
4. We will use the Spacing property of the C1DocStyle class to add spacing around the Chapter text. The default unit for the Spacing property is 1 inch. There will be an inch of empty space above the chapter and 5 millimeters of empty space below the chapter. Note, since the default unit is in inches, we use the UnitValue property to set the unit in millimeters.
' In default unit - inch.
Chapter.Style.Spacing.Top = 1
Chapter.Style.Spacing.BottomUnit.UnitValue = "5mm"
• C#
// In default unit – inch.
Chapter.Style.Spacing.Top = 1;
Chapter.Style.Spacing.BottomUnit.UnitValue = "5mm";
5. In this example, we will assign a new font to the chapter and the MainText. For the chapter, we'll use an Arial font that is 24 points in size, and its style is bold, italic, and underline. For the MainText, we'll use an Arial font that is 12 points in size.
Chapter.Style.Font = New Font("Arial", 24, FontStyle.Bold Or FontStyle.Italic Or FontStyle.Underline)
MainText.Style.Spacing.LeftUnit.UnitValue = "5mm"
MainText.Style.Spacing.BottomUnit.UnitValue = "5mm"
MainText.Style.Font = New Font("Arial", 12)
• C#
Chapter.Style.Font = new Font("Arial", 24, FontStyle.Bold | FontStyle.Italic | FontStyle.Underline);
MainText.Style.Spacing.LeftUnit.UnitValue = "5mm";
MainText.Style.Spacing.BottomUnit.UnitValue = "5mm";
MainText.Style.Font = new Font("Arial", 12);
6. Use the StartDoc method to start generating the document. Create two for loops. The first for loop will generate up to three chapters. Inside the first for loop add text to the Chapter variable and call it Chapter. Assign the Chapter variable to the RenderBlock method. Create another for loop to generate a paragraph for each chapter. The paragraph will be generated below each chapter. Assign MainText to the RenderBlock method.
Me.C1PrintDocument1.StartDoc()
For iChapter = 1 To 3
Chapter.Text = "Chapter " & iChapter
Me.C1PrintDocument1.RenderBlock(Chapter)
For iParagraph = 1 To 3
MainText.Text = s
Me.C1PrintDocument1.RenderBlock(MainText)
Next
Next
Me.C1PrintDocument1.EndDoc()
• C#
this.c1PrintDocument1.StartDoc();
for(iChapter = 1; iChapter < 4; iChapter++)
{
Chapter.Text = "Chapter " + iChapter;
this.c1PrintDocument1.RenderBlock(Chapter);
for(iParagraph = 1; iParagraph < 4; iParagraph++)
{
MainText.Text = s;
this.c1PrintDocument1.RenderBlock(MainText);
}
}
this.c1PrintDocument1.EndDoc();
Run the program and observe the following:
Your document will appear similar to the image below at run time.
|