Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Fri Jul 21, 2017 12:36 pm

Hi,

I'm trying to create a PdfTable where one column is an image. I do not have a database connection like in the samples. My code looks like that:

Code: Select all
            PdfTable table = new PdfTable();
            table.Style.BorderPen = new PdfPen(brush1, 0.75f);
            table.Style.ShowHeader = true;

            DataTable dataTable = new DataTable();
            dataTable.Columns.Add(new DataColumn("Icon", typeof(PdfImage)));
            dataTable.Columns.Add(new DataColumn("Name", typeof(string)));
            foreach (var legendItem in legendItems)
            {
                DataRow row = dataTable.NewRow();
                PdfImage pdfImage = PdfImage.FromImage(legendItem.Value);
                row["Icon"] = pdfImage;
                row["Name"] = legendItem.Key;
                dataTable.Rows.Add(row);
            }

            table.DataSourceType = PdfTableDataSourceType.TableDirect;
            table.DataSource = dataTable;

            float width = page.Canvas.ClientSize.Width - (table.Columns.Count + 1) * table.Style.BorderPen.Width;
            table.Columns[0].Width = width * 0.24f * width;
            table.Columns[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
            table.Columns[1].Width = width * 0.21f * width;
            table.Columns[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);


Doing so the table looks like that:

Icon Name
Spire.Pdf.Graphics.PdfBitmap String 1
Spire.Pdf.Graphics.PdfBitmap String 2
Spire.Pdf.Graphics.PdfBitmap String 3

How do I set the image in the DataTable so it is rendered correctly?

Best Regards,
Torsten

grl
 
Posts: 1
Joined: Fri Jul 21, 2017 8:30 am

Mon Jul 24, 2017 10:44 am

Hello,

Thanks for your inquiry,
I will look into it and reply you ASAP.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Tue Jul 25, 2017 9:29 am

Hello,

After investigation, here is sample code for your kind reference.
Code: Select all
    private  static  DataTable dataTableNew = new DataTable();
    private static string[,] Datas = new string[2, 2];
       private static void GetTable()
       {
           Datas[0, 0] = "pic1";
           Datas[0, 1] = @"F:\image\index.jpg";
           Datas[1, 0] = "pic2";
           Datas[1, 1] = @"F:\image\image1.jpg";

           dataTableNew.Columns.Add(new DataColumn("Name", typeof(string)));
           dataTableNew.Columns.Add(new DataColumn("Image", typeof(object)));

           for (int i = 0; i < Datas.Length/2; i++)
           {
               DataRow row = dataTableNew.NewRow();
               row["Name"] = Datas[i, 0];
               dataTableNew.Rows.Add(row);
           }

       }
     static  public void DrawImageInTable()
       {
           PdfDocument pdf = new PdfDocument();
           PdfPageBase page = pdf.Pages.Add();
           PdfTable table = new PdfTable();
           PdfBrush brush1 = PdfBrushes.Black;
           table.Style.BorderPen = new PdfPen(brush1, 0.75f);
           table.Style.ShowHeader = true;
           //get the datatable
           GetTable();
           table.DataSourceType = PdfTableDataSourceType.TableDirect;
           table.DataSource = dataTableNew;
           
           table.EndCellLayout += new EndCellLayoutEventHandler(table_EndCellLayoutTest);
           table.BeginRowLayout += new BeginRowLayoutEventHandler(table_BeginRowLayoutTest);
       
           PdfTableLayoutFormat tableLayout = new PdfTableLayoutFormat();
           tableLayout.Break = PdfLayoutBreakType.FitElement;
           tableLayout.Layout = PdfLayoutType.Paginate;


           PdfLayoutResult result = table.Draw(page, new PointF(0, 20), tableLayout);
           pdf.SaveToFile(@"123result.pdf");
       }
     private static void table_BeginRowLayoutTest(object sender, BeginRowLayoutEventArgs args)
     {
         if (args.RowIndex < 0)
         {
             //header
             return;
         }
         
         PdfImage image = PdfImage.FromFile(Datas[args.RowIndex, 1]);
         //set the row height accroding to the image
         args.MinimalHeight = 4 + image.PhysicalDimension.Height;

     }
      private static void table_EndCellLayoutTest(object sender, EndCellLayoutEventArgs args)
       {
           if (args.RowIndex < 0)
           {
               //header
               return;
           }
           if (args.CellIndex == 1)
           {
               PdfImage image = PdfImage.FromFile(Datas[args.RowIndex, 1]);
               float x = (args.Bounds.Width - image.PhysicalDimension.Width) / 2 + args.Bounds.X;
               float y = (args.Bounds.Height - image.PhysicalDimension.Height) / 2 + args.Bounds.Y;
               // draw the image
               args.Graphics.DrawImage(image, x, y);
           }
       }

In addition, you also could use PdfGrid to draw the image, here is the guide.

In Spire.PDF, the grid is a special table. It is more simple to import data from DataSource. In table, it cannot manipulate the cell and just can set the style by row. In grid, we can manipulate each cell, set different style for every cell.

If there is any question, please let us know.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Thu Jul 27, 2017 8:44 am

Hello,

How is the issue now ?
Could you please give us some feedback ?

Thanks,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.PDF