Placing a text around an image

Skip to end of metadata
Go to start of metadata
Placing a text around an image is a very common practice. You may even notice that in MS word you can wrap the text around an image. Let s think of a simple scenario in whi ch you need to place a text along an image. As we know that by default, Aspose.Pdf for .NET places the paragraphs in Top-Left to Bottom-Right positioning order. So placing a text around an image would be bit tricky. It even gets more complicated, if we need to palce some formatted text in a flow. In order to accomplish this requirement, we would be using a table and placing text in individual table cells.

Working Solution

InLineHTML text can be used to display formatted text along with the normal text. To render the HTML text according to its tag formatting, please set the value of IsHtmlTagSupported property to true. For the above scenario, we need to create a table with two columns and two rows. In first column of first row, we need to place the image object and in second column we will place the text contents. As we can see that t he heading object is in Bold and font size is very large as compare d to rest of the text contents. This requirement can easily be accomplished when using HTML text with large font size and different color for the text. Regarding the contents below the heading, the font size and color is different.
Now getting back to the second row where table cell is very large, so in order to accomplish this, we need to set the value of ColumnsSpan property of table cell to 2 and add the text contents inside the paragraphs collection of table cell. Please take a look over the following code snippet to fulfill the requirement.

C#
//Instantiate Pdf document object 
Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf();
//Create a section in the Pdf 
Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add();

//Instantiate a table object 
Aspose.Pdf.Generator.Table table1 = new Aspose.Pdf.Generator.Table();
//Add the table in paragraphs collection of the desired section 
sec1.Paragraphs.Add(table1);
//Set with column widths of the table 
table1.ColumnWidths ="120 270";

//Create MarginInfo object and set its left, bottom, right and top margins 
Aspose.Pdf.Generator.MarginInfo margin = new Aspose.Pdf.Generator.MarginInfo();
margin.Top = 5f;
margin.Left = 5f;
margin.Right = 5f;
margin.Bottom = 5f;
//Set the default cell padding to the MarginInfo object 
table1.DefaultCellPadding = margin;

//Create rows in the table and then cells in the rows 
Aspose.Pdf.Generator.Row row1 = table1.Rows.Add();

// create an image object
Aspose.Pdf.Generator.Image logo = new Aspose.Pdf.Generator.Image();
// specify the image file path
logo.ImageInfo.File = @"d:/pdftest/aspose_pdf-for-_net.jpg";
// specify the image Fixed Height
logo.ImageInfo.FixHeight = 120;
// specify the image Fixed Width
logo.ImageInfo.FixWidth= 110;
row1.Cells.Add();
// add the image to paragraphs collection of the table cell
row1.Cells[0].Paragraphs.Add(logo);

//Create string variables with text containing html tags 
string TitleString = "<font face=\"Arial\" size=6 color=\"#101090\"><b> Aspose.Pdf for .NET</b></font>";
// create a text object to be added to the right of image
Aspose.Pdf.Generator.Text TitleText = new Aspose.Pdf.Generator.Text(TitleString);
TitleText.IsHtmlTagSupported =true;
row1.Cells.Add();
 //Add the text paragraphs containing HTML text to the table cell
row1.Cells[1].Paragraphs.Add(TitleText);
// set the vertical alignment of the row contents as Top
row1.VerticalAlignment = Aspose.Pdf.Generator.VerticalAlignmentType.Top;

string BodyString1 = "<font face=\"Arial\" size=2><br/>Aspose.Pdf for .NET is a non-graphical PDF® document reporting component that enables .NET applications to <b> create PDF documents from scratch </b> without utilizing Adobe Acrobat®. Aspose.Pdf for .NET is very affordably priced and offers a wealth of strong features including: compression, tables, graphs, images, hyperlinks, security and custom fonts. </font>";

//add a text segment to segments collection of the text object
TitleText.Segments.Add(BodyString1);

//Create rows in the table and then cells in the rows
Aspose.Pdf.Generator.Row SecondRow = table1.Rows.Add();
SecondRow.Cells.Add();
// set the row span value for Second row as 2
SecondRow.Cells[0].ColumnsSpan = 2;
// Set the vertical alignment of the second row as Top
SecondRow.VerticalAlignment = Aspose.Pdf.Generator.VerticalAlignmentType.Top;

string SecondRowString = "<font face=\"Arial\" size=2>Aspose.Pdf for .NET supports the creation of PDF files through API and XML or XSL-FO templates. Aspose.Pdf for .NET is very easy to use and is provided with 14 fully featured demos written in both C# and Visual Basic.</font>"; 
            Aspose.Pdf.Generator.Text SecondRowText = new Aspose.Pdf.Generator.Text(SecondRowString);
SecondRowText.IsHtmlTagSupported = true;
//Add the text paragraphs containing HTML text to the table cell
SecondRow.Cells[0].Paragraphs.Add(SecondRowText);

pdf1.Save(@"d:/pdftest/LogoWithText_Test.pdf");
 

VB.NET
'Instantiate Pdf document object 
Dim pdf1 As Aspose.Pdf.Generator.Pdf = New Aspose.Pdf.Generator.Pdf()
'Create a section in the Pdf 
Dim sec1 As Aspose.Pdf.Generator.Section = pdf1.Sections.Add()

'Instantiate a table object 
Dim table1 As Aspose.Pdf.Generator.Table = New Aspose.Pdf.Generator.Table()
'Add the table in paragraphs collection of the desired section 
sec1.Paragraphs.Add(table1)
'Set with column widths of the table 
table1.ColumnWidths = "120 270"

'Create MarginInfo object and set its left, bottom, right and top margins 
Dim margin As Aspose.Pdf.Generator.MarginInfo = New Aspose.Pdf.Generator.MarginInfo()
margin.Top = 5.0F
margin.Left = 5.0F
margin.Right = 5.0F
margin.Bottom = 5.0F
'Set the default cell padding to the MarginInfo object 
table1.DefaultCellPadding = margin

'Create rows in the table and then cells in the rows 
Dim row1 As Aspose.Pdf.Generator.Row = table1.Rows.Add()

' create an image object
Dim logo As Aspose.Pdf.Generator.Image = New Aspose.Pdf.Generator.Image()
' specify the image file path
logo.ImageInfo.File = "d:/pdftest/aspose_pdf-for-_net.jpg"
' specify the image Fixed Height
logo.ImageInfo.FixHeight = 120
' specify the image Fixed Width
logo.ImageInfo.FixWidth = 110
row1.Cells.Add()
' add the image to paragraphs collection of the table cell
row1.Cells(0).Paragraphs.Add(logo)

'Create string variables with text containing html tags 
Dim TitleString As String = "<font face=""Arial"" size=6 color=""#101090""><b> Aspose.Pdf for .NET</b></font>"
' create a text object to be added to the right of image
Dim TitleText As Aspose.Pdf.Generator.Text = New Aspose.Pdf.Generator.Text(TitleString)
TitleText.IsHtmlTagSupported = True
row1.Cells.Add()
'Add the text paragraphs containing HTML text to the table cell
row1.Cells(1).Paragraphs.Add(TitleText)
' set the vertical alignment of the row contents as Top
row1.VerticalAlignment = Aspose.Pdf.Generator.VerticalAlignmentType.Top

Dim BodyString1 As String = "<font face=""Arial"" size=2><br/>Aspose.Pdf for .NET is a non-graphical PDF® document reporting component that enables .NET applications to <b> create PDF documents from scratch </b> without utilizing Adobe Acrobat®. Aspose.Pdf for .NET is very affordably priced and offers a wealth of strong features including: compression, tables, graphs, images, hyperlinks, security and custom fonts. </font>"

'add a text segment to segments collection of the text object
TitleText.Segments.Add(BodyString1)

'Create rows in the table and then cells in the rows
Dim SecondRow As Aspose.Pdf.Generator.Row = table1.Rows.Add()
SecondRow.Cells.Add()
' set the row span value for Second row as 2
SecondRow.Cells(0).ColumnsSpan = 2
' Set the vertical alignment of the second row as Top
SecondRow.VerticalAlignment = Aspose.Pdf.Generator.VerticalAlignmentType.Top

Dim SecondRowString As String = "<font face=""Arial"" size=2>Aspose.Pdf for .NET supports the creation of PDF files through API and XML or XSL-FO templates. Aspose.Pdf for .NET is very easy to use and is provided with 14 fully featured demos written in both C# and Visual Basic.</font>"
Dim SecondRowText As Aspose.Pdf.Generator.Text = New Aspose.Pdf.Generator.Text(SecondRowString)
SecondRowText.IsHtmlTagSupported = True
'Add the text paragraphs containing HTML text to the table cell
SecondRow.Cells(0).Paragraphs.Add(SecondRowText)

pdf1.Save("d:/pdftest/LogoWithText_Test.pdf")
 




Fig:1 - Sample output generated with above code.

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.