| Tables are very important elements while displaying data. They give a very presentable format for data representation. Mostly tables are used to display numeric information. Aspose.Pdf provides a class named Table which offers you the capability to create tables in PDF file. Rather than creating simple table you can set different formatting options for the table, which includes setting table border style, margin information, Alignment, Background color, Table Members property for the table columns, Title information, set the value for rows to be repeated over every page and many more. For more detailed information on supported features offered by Table class, please visit Table Members . |
Solution
According to our DOM (Document Object Model) a document is composed of Sections. Where a section may be a Header/Footer or a body of the document. A section contains one or more paragraphs, and a paragraph may be an Image, a text, a form field, Heading, FloatingBox, graph, Attachment, or a table. Whereas, a table has a collection of Rows, a row has a collection of Cells and a cell is a collection of Paragraphs. So, as according to our DOM, a table cell may contain any of the paragraph elements specified above which also includes images. One must have a clear understanding of cell width, especially when displaying an image in table cell, so that the image width should be fixed according to the width of a cell, in order to display it properly. Width of an Image can be fixed by setting the FixWidth property of ImageInfo class. In this case the image is scaled to the fixed width of table cell.
i.e. image.ImageInfo.FixWidth = cellWidth - cellBorderWidth - cell.Padding.Left - cell.Padding.Right;
//Create a section in the pdf document Aspose.Pdf.Generator.Pdf pdfConv = new Aspose.Pdf.Generator.Pdf(); //Create a section in the pdf document Aspose.Pdf.Generator.Section sec1 = pdfConv.Sections.Add(); //Instantiate a table object Aspose.Pdf.Generator.Table tab1 = new Aspose.Pdf.Generator.Table(); //Add the table in paragraphs collection of the desired section sec1.Paragraphs.Add(tab1); //Set default cell border using BorderInfo object tab1.DefaultCellBorder = new Aspose.Pdf.Generator.BorderInfo((int) Aspose.Pdf.Generator.BorderSide.All, 0.1F); //Set with column widths of the table tab1.ColumnWidths = "100 100 120"; Aspose.Pdf.Generator.Image img = new Aspose.Pdf.Generator.Image(); img.ImageInfo.File = @"D:\pdftest\Aspose.jpg"; img.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Jpeg; //Create rows in the table and then cells in the rows Aspose.Pdf.Generator.Row row1 = tab1.Rows.Add(); row1.Cells.Add("Sample text in cell"); // Add the cell which holds the image Aspose.Pdf.Generator.Cell cell2 = row1.Cells.Add(); //Add the image to the table cell cell2.Paragraphs.Add(img); row1.Cells.Add("Previous cell with image"); row1.Cells[2].VerticalAlignment= Aspose.Pdf.Generator.VerticalAlignmentType.Center; // save the Pdf file pdfConv.Save(@"d:\pdftest\Image_in_Cell.pdf");
‘Instantiate PDF instance by calling empty constructor Dim pdfConv As Aspose.Pdf.Generator.Pdf = New Aspose.Pdf.Generator.Pdf() 'Create a section in the pdf document Dim sec1 As Aspose.Pdf.Generator.Section = pdfConv.Sections.Add() 'Instantiate a table object Dim tab1 As Aspose.Pdf.Generator.Table = New Aspose.Pdf.Generator.Table() 'Add the table in paragraphs collection of the desired section sec1.Paragraphs.Add(tab1) 'Set default cell border using BorderInfo object tab1.DefaultCellBorder = New Aspose.Pdf.Generator.BorderInfo(Aspose.Pdf.Generator.BorderSide.All, 0.1F) 'Set with column widths of the table tab1.ColumnWidths = "100 100 120" Dim img As Aspose.Pdf.Generator.Image = New Aspose.Pdf.Generator.Image() img.ImageInfo.File = "D:\pdftest\Aspose.jpg" img.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Jpeg 'Create rows in the table and then cells in the rows Dim row1 As Aspose.Pdf.Generator.Row = tab1.Rows.Add() row1.Cells.Add("Sample text in cell") ' Add the cell which holds the image Dim cell2 As Aspose.Pdf.Generator.Cell = row1.Cells.Add() 'Add the image to the table cell cell2.Paragraphs.Add(img) row1.Cells.Add("Previous cell with image") row1.Cells(2).VerticalAlignment = Aspose.Pdf.Generator.VerticalAlignmentType.Center ' save the Pdf file pdfConv.Save("d:\pdftest\Image_in_Cell.pdf")
