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.

Sat May 01, 2021 5:30 am

Hi,
I add a table and set column width with bellow code ,

Code: Select all
grid.Style.CellPadding = new PdfPaddings(3f, 3f, 3f, 3f);
                grid.Columns.Add(9);

                width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1);
               
                grid.Columns[0].Width = width * 0.03f;
                grid.Columns[1].Width = width * 0.43f;
                grid.Columns[2].Width = width * 0.08f;
                grid.Columns[3].Width = width * 0.05f;
                grid.Columns[4].Width = width * 0.09f;
                grid.Columns[5].Width = width * 0.09f;
                grid.Columns[6].Width = width * 0.09f;
                grid.Columns[7].Width = width * 0.05f;
                grid.Columns[8].Width = width * 0.09f;

The out put as bellow
pdf.png

but the table is not cover available width of the page .How to solve this.

kuntal.bose
 
Posts: 20
Joined: Thu Apr 29, 2021 6:05 am

Mon May 03, 2021 3:20 am

Hello,

Thanks for your inquiry and sorry for the late reply as weekend.
Based on your previous question posted on our forum, I made some changes to the code, as shown below.
If there are still any issues, please feel free to contact us.
Code: Select all
       static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            PdfPageBase page = doc.Pages.Add();
            Test(page);
            PdfGrid grid = new PdfGrid();
            grid.Columns.Add(9);
            float width = page.Canvas.ClientSize.Width - 3;

            grid.Style.CellPadding = new PdfPaddings(3f, 3f, 3f, 3f);

            PdfGridRow pdfGridRow = grid.Rows.Add();

            grid.Columns[0].Width = width * 0.03f;
            grid.Columns[1].Width = width * 0.43f;
            grid.Columns[2].Width = width * 0.08f;
            grid.Columns[3].Width = width * 0.05f;
            grid.Columns[4].Width = width * 0.09f;
            grid.Columns[5].Width = width * 0.09f;
            grid.Columns[6].Width = width * 0.09f;
            grid.Columns[7].Width = width * 0.05f;
            grid.Columns[8].Width = width * 0.09f;
            pdfGridRow.Cells[0].Value = "SI";
            pdfGridRow.Cells[1].Value = "Description of Goods";
            pdfGridRow.Cells[2].Value = "HSN";
            pdfGridRow.Cells[3].Value = "UOM";
            pdfGridRow.Cells[4].Value = "Shipped QTY";
            pdfGridRow.Cells[5].Value = "Billed QTY";
            pdfGridRow.Cells[6].Value = "Rate";
            pdfGridRow.Cells[7].Value = "Disc%";
            pdfGridRow.Cells[8].Value = "Amount";


            PdfLayoutResult result = grid.Draw(page, new PointF(0, 40));

            doc.SaveToFile("result2.pdf");
        }

        private static void Test(PdfPageBase page)
        {
            PdfGrid grid = new PdfGrid();

            grid.Columns.Add(2);

            float width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1);

            grid.Columns[0].Width = width * 0.50f;
            grid.Columns[1].Width = width * 0.50f;

            PdfGridRow pdfGridRow = grid.Rows.Add();
            pdfGridRow.Height = 25;

            pdfGridRow.Cells[0].Value = "Sample Text";

            PdfLayoutResult result = grid.Draw(page, new PointF(0, 10));
        }



Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Mon May 03, 2021 3:53 am

Hi,
Thanks for reply,

You change
Code: Select all
 width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1);

to
Code: Select all
width = page.Canvas.ClientSize.Width - 3;


can you please tell me why you minus 3 from width?how you determine that we need to subtract 3?

kuntal.bose
 
Posts: 20
Joined: Thu Apr 29, 2021 6:05 am

Mon May 03, 2021 6:25 am

Hello,

Thanks for your response.
In the code I provided, when drawing the first grid (This code references the code you used in this post "Embed a Grid into a Grid Cell in PDF"), its width is “page.Canvas.ClientSize.Width - (grid.Columns.Count + 1) = page.Canvas.ClientSize.Width – 3”. So in order to keep the width of the second grid consistent with it, I set the width of the second grid as “page. Canvas. Clientsize. Width - 3” as shown below.
screenshot.png


If you want the width of all grids to be the same (no matter how many columns there are), you can use the following code instead.
Code: Select all
float width = page.Canvas.ClientSize.Width;


Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.PDF

cron