As far as Text paragraphs are concerned, we know that we can set the Text formatting using the TextInfo object. TextInfo object is encapsulated as a property in most of the Aspose.Pdf classes like:
In the Aspose.Pdf DOM , the child nodes can inherit TextInfo (containing Text Format Information) from their parent nodes as shown in the figure below:
|
The above diagram depicts that if you want to use same text format settings for all Text paragraphs in a Section then it is not necessary to apply same text format settings for each Paragraph individually.
To obtain desired results with minimum effort, you just need to set the text format settings for the Section object. And the newly created Text paragraphs can inherit text format settings from the Section created before. For this, it is must for the Text paragraphs to be created by calling one of the overloaded constructors of Text class that takes a Section instance as parameter to inherit its settings.
Code Snippet
[C#]
//Instantiate Pdf instance by calling its empty constructor
Pdf pdf1 = new Pdf();
//Create a new section in the Pdf object
Section sec1 = pdf1.Sections.Add();
//Set text color to blue in the whole section
sec1.TextInfo.Color = new Aspose.Pdf.Color("Blue");
//Add 1st paragraph (inheriting the text format settings from the section)
//to the section
sec1.Paragraphs.Add(new Aspose.Pdf.Text(sec1,"paragraph 1 "));
//Add 2nd paragraph (inheriting the text format settings from the section)
//to the section
sec1.Paragraphs.Add(new Aspose.Pdf.Text(sec1,"paragraph 2"));
//Create 3rd paragraph (inheriting the text format settings from the section)
Text t3 = new Aspose.Pdf.Text(sec1);
//Create a segment "seg1" in the paragraph "t3"
Segment seg1 = new Segment(t3);
//Assign some content to the segment
seg1.Content = "paragraph 3 segment 1";
//Set the color of the segment to red
seg1.TextInfo.Color = new Aspose.Pdf.Color("Red");
//Add segment (with red text color) to the paragraph
t3.Segments.Add(seg1);
//Create a new segment "seg2" in the paragraph "t3"
Segment seg2 = new Segment(t3);
//Assign some content to the segment
seg2.Content = "paragraph 3 segment 2";
//Set the color of the segment to green
seg2.TextInfo.Color = new Aspose.Pdf.Color("Green");
//Add segment (with green text color) to the paragraph
t3.Segments.Add(seg2);
//Add 3rd text paragraph to the section with overridden text format settings
sec1.Paragraphs.Add(t3);
//Add 4th paragraph (inheriting the text format settings from the section)
//to the section
sec1.Paragraphs.Add(new Aspose.Pdf.Text(sec1,"paragraph 4"));
[VB.NET]
'Instantiate Pdf instance by calling its empty constructor
Dim pdf1 As Pdf = New Pdf()
'Create a new section in the Pdf object
Dim sec1 As Section = pdf1.Sections.Add()
'Set text color to blue in the whole section
sec1.TextInfo.Color = New Aspose.Pdf.Color("Blue")
'Add 1st paragraph (inheriting the text format settings from the section)
'to the section
sec1.Paragraphs.Add(New Aspose.Pdf.Text(sec1, "paragraph 1 "))
'Add 2nd paragraph (inheriting the text format settings from the section)
'to the section
sec1.Paragraphs.Add(New Aspose.Pdf.Text(sec1, "paragraph 2"))
'Create 3rd paragraph (inheriting the text format settings from the section)
Dim t3 As Aspose.Pdf.Text = New Aspose.Pdf.Text(sec1)
'Create a segment "seg1" in the paragraph "t3"
Dim seg1 As Segment = New Segment(t3)
'Assign some content to the segment
seg1.Content = "paragraph 3 segment 1"
'Set the color of the segment to red
seg1.TextInfo.Color = New Aspose.Pdf.Color("Red")
'Add segment (with red text color) to the paragraph
t3.Segments.Add(seg1)
'Create a new segment "seg2" in the paragraph "t3"
Dim seg2 As Segment = New Segment(t3)
'Assign some content to the segment
seg2.Content = "paragraph 3 segment 2"
'Set the color of the segment to green
seg2.TextInfo.Color = New Aspose.Pdf.Color("Green")
'Add segment (with green text color) to the paragraph
t3.Segments.Add(seg2)
'Add 3rd text paragraph to the section with overridden text format settings
sec1.Paragraphs.Add(t3)
'Add 4th paragraph (inheriting the text format settings from the section)
'to the section
sec1.Paragraphs.Add(New Aspose.Pdf.Text(sec1, "paragraph 4"))
[JAVA]
//Instantiate Pdf instance by calling its empty constructor
Pdf pdf1 = new Pdf();
//Create a new section in the Pdf object
Section sec1 = pdf1.getSections().add();
//Set text color to blue in the whole section
sec1.getTextInfo().setTextColor(Color.Blue);
//Add 1st paragraph (inheriting the text format settings from the section)
//to the section
sec1.getParagraphs().add(new Text(sec1,"paragraph 1 "));
//Add 2nd paragraph (inheriting the text format settings from the section)
//to the section
sec1.getParagraphs().add(new Text(sec1,"paragraph 2 "));
//Create 3rd paragraph (inheriting the text format settings from the section)
Text t3 = new Text(sec1);
//Create a segment "seg1" in the paragraph "t3"
Segment seg1 = new Segment(t3);
//Assign some content to the segment
seg1.setContent("paragraph 3 segment 1");
//Set the color of the segment to red
seg1.getTextInfo().setTextColor(Color.Red);
//Add segment (with red text color) to the paragraph
t3.getSegments().add(seg1);
//Create a new segment "seg2" in the paragraph "t3"
Segment seg2 = new Segment(t3);
//Assign some content to the segment
seg2.setContent("paragraph 3 segment 2");
//Set the color of the segment to green
seg2.getTextInfo().setTextColor(Color.Green);
//Add segment (with green text color) to the paragraph
t3.getSegments().add(seg2);
//Add 3rd text paragraph to the section with overridden text format settings
sec1.getParagraphs().add(t3);
//Add 4th paragraph (inheriting the text format settings from the section)
//to the section
sec1.getParagraphs().add(new Text(sec1,"paragraph 4"));
[XML]
<Pdf xmlns="Aspose.Pdf">
<Section TextColor="Blue">
<Text>
<Segment>paragraph 1</Segment>
</Text>
<Text MarginTop="10">
<Segment>paragraph 2</Segment>
</Text>
<Text MarginTop="10" Color="Red">
<Segment>paragraph 3 segment 1 </Segment>
<Segment Color="Green">paragraph 3 segment 2</Segment>
</Text>
<Text MarginTop="10">
<Segment>paragraph 4</Segment>
</Text>
</Section>
</Pdf>
After you apply the example code given above, the following output would be achieved in the PDF document:
|