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 Apr 30, 2021 5:23 am

Hi,
I Follow Embed a Grid into a Grid Cell in PDF in C# post and I want the embedded grid to be full width .How do I do this?

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

Fri Apr 30, 2021 7:22 am

Hello,

Thanks for your inquiry.
Please use the below modified code to make the embedded grid to be full width. The attachment(EmbedGridInCell.zip) is my testing result. If this effect is not what you want, to help us further investigate it, please share us with some screenshots to show your desired output effect. Thanks in advance.
Code: Select all
//Create a pdf document
PdfDocument pdf = new PdfDocument();
//Add a page
PdfPageBase page = pdf.Pages.Add();
//Create a pdf grid
PdfGrid grid = new PdfGrid();
//Add two rows
PdfGridRow row1 = grid.Rows.Add();
PdfGridRow row2 = grid.Rows.Add();
//Set Top and Bottom cell padding of the grid
grid.Style.CellPadding.Top = 5f;
grid.Style.CellPadding.Bottom = 5f;

//Add two columns
grid.Columns.Add(2);

//Set the columns’ width 
grid.Columns[0].Width = 120f;
grid.Columns[1].Width = 120f;

//Create another grid to embed
PdfGrid embedGrid = new PdfGrid();

//Add a row
PdfGridRow newRow = embedGrid.Rows.Add();

//Add two columns
embedGrid.Columns.Add(2);

//set the column width to 60f
embedGrid.Columns[0].Width = 60f;
embedGrid.Columns[1].Width = 60f;

//Create a PDFStringFormat instance
PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

//Assign values to the cells of the embedGrid and set formatting
newRow.Cells[0].Value = "Spire.Doc";
newRow.Cells[0].StringFormat = stringFormat;
newRow.Cells[1].Value = "Spire.PDF";
newRow.Cells[1].StringFormat = stringFormat;

//Assign values to the cells of the grid and set formatting
row1.Cells[0].Value = "Customer's Name";
row1.Cells[0].StringFormat = stringFormat;
row1.Cells[0].Style.BackgroundBrush = PdfBrushes.Gray;
row1.Cells[1].Value = "Product(s)";
row1.Cells[1].StringFormat = stringFormat;
row1.Cells[1].Style.BackgroundBrush = PdfBrushes.Gray;
row2.Cells[0].Value = "Michael";
row2.Cells[0].StringFormat = stringFormat;
//Assign the embedGrid to the cell of the grid
row2.Cells[1].Value = embedGrid;
row2.Cells[1].StringFormat = stringFormat;

//Draw the grid to the new added page
grid.Draw(page, new PointF(0f, 50f));
//Save the pdf document
pdf.SaveToFile("EmbedGridInCell.pdf");

Sincerely,
Sofia
E-iceblue support team
User avatar

Sofia.Yang
 
Posts: 84
Joined: Tue Jul 14, 2020 1:41 am

Fri Apr 30, 2021 7:54 am

Thanks for your support, but I don't want a fix width for columns.

Please check bellow code and output

Code: Select all
PdfDocument doc = new PdfDocument();
            PdfPageBase page = doc.Pages.Add();
            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";

            PdfGrid embedgrid = new PdfGrid();

            embedgrid.Columns.Add(3);

           PdfGridRow pdfembedGridRow = embedgrid.Rows.Add();

            pdfembedGridRow.Cells[0].Value = "Embed Row1 Col1";
            pdfembedGridRow.Cells[1].Value = "Embed Row1 Col2";
            pdfembedGridRow.Cells[2].Value = "Embed Row1 Col3";

            pdfembedGridRow = embedgrid.Rows.Add();

            pdfembedGridRow.Cells[0].Value = "Embed Row2 Col1";
            pdfembedGridRow.Cells[1].Value = "Embed Row2 Col2";
            pdfembedGridRow.Cells[2].Value = "Embed Row2 Col3";

            pdfGridRow.Cells[1].Value = embedgrid;

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


pdf.png


I want % column with in respect of pdfGridRow.Cells[1] width

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

Fri Apr 30, 2021 9:52 am

Hello,

Thanks for your feedback.
You can set the column width of the embedded grid proportionally like the attached screenshot. If there is still any issue after testing, just feel free to write back!

Sincerely,
Sofia
E-iceblue support team
User avatar

Sofia.Yang
 
Posts: 84
Joined: Tue Jul 14, 2020 1:41 am

Fri Apr 30, 2021 11:33 am

Hi,
after adding the lines its through bellow exception.

Spire.Pdf.Exceptions.PdfException: 'Can't draw one or more inner grids, no enough space available for it.'

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

Mon May 03, 2021 2:15 am

Hello,

Thanks for your response and sorry for the late reply as weekend.
I tested your case but I did not encounter any issue. I uploaded my test project here, you can check if there is any difference with your project.

Sincerely,
Brian
E-iceblue support team
User avatar

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

Return to Spire.PDF