Aspose.Pdf

Set Text Background Color

What if a developer would like to create a Text Paragraph with colored background?

 

Well, Aspose.Pdf fulfills this desire of developers too. We can set any background color for a Text Paragraph by using the TextInfo.BackgroundColor property of Text class.

 

Aspose.Pdf contains a Color class that is used to represent variety of colors. All we have to do is to instantiate the Color object by calling its constructor. The constructor of Color class takes the color name as string argument. After the Color object is created, we assign its reference to TextInfo.BackgroundColor property of Text class.

 

Note: Setting background color for Segment is not supported.

 

Code Snippet

 

[C#]

 

//Instantiate Pdf instance by calling its empty constructor

Pdf pdf1 = new Pdf()

 

//Create a section in the Pdf object

Section sec1 = pdf1.Sections.Add();

 

//Create a text paragraph

Text text1 = new Text(sec1,"Hello Aspose.Pdf");

 

//Set the BackgroundColor of the text paragraph to Red

text1.TextInfo.BackgroundColor=new Aspose.Pdf.Color("Red");

 

//Add the text paragraph to the section

sec1.Paragraphs.Add(text1);

 

//Save the Pdf

pdf1.Save(...);

 

[VB.NET]

 

'Instantiate Pdf instance by calling its empty constructor

Dim pdf1 As Pdf =  New Pdf()

 

'Create a section in the Pdf object

Dim sec1 As Section =  pdf1.Sections.Add()

 

'Create a text paragraph

Dim text1 As Text =  New Text(sec1,"Hello Aspose.Pdf")

 

'Set the BackgroundColor of the text paragraph to Red

text1.TextInfo.BackgroundColor=New Aspose.Pdf.Color("Red")

 

'Add the text paragraph to the section

sec1.Paragraphs.Add(text1)

 

'Save the Pdf

pdf1.Save(...)

 

[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();

 

//Create a new text paragraph

Text text1 = new Text(sec1,"Hello Aspose.Pdf");

 

//Set the BackgroundColor of the text paragraph to Red

text1.getTextInfo().setBackGroundColor(Color.Red);

 

//Add the text paragraph to the section

sec1.getParagraphs().add(text1);

 

//Save the Pdf

FileOutputStream out = new FileOutputStream(new File("..."));

pdf1.save(out);

 

[XML]

 

<?xml version="1.0" encoding="utf-8" ?>

  <Pdf xmlns="Aspose.Pdf">

   <Section>

        <Text BackgroundColor="Red">

                <Segment>Hello Aspose.Pdf</Segment>

        </Text>

   </Section>

  </Pdf>