PDFsharp & MigraDoc Forum https://forum.pdfsharp.net/ |
|
Set Table Column width using proportional algorithm https://forum.pdfsharp.net/viewtopic.php?f=2&t=4866 |
Page 1 of 1 |
Author: | bradvan71 [ Thu Aug 14, 2025 3:53 pm ] |
Post subject: | Set Table Column width using proportional algorithm |
Hi, I am new to the MIgrDoc forum...honestly...new to using pdfSharp / MigraDoc altogether. I am creating a pricing report that adds table sections to a document dynamically based on user input criteria. The data part works fine....the datatables are used to create the Table sections dynamically and each Table section can have different columns. There are 2 columns that are constants in each Table regardless of the data....an Item Product Code and its description, so I set the proportional width of thos 2 columns as static . I am trying to take the full table section width - LeftMargin - minus right margin and use that as the available Table Section width... I then use that to get the widths for the Item Code and Description columns based on the available width To get the remaining column widths, i subtract the first 2 columns proportional widths and then divide the remaining width by the number of remaining columns to get the remaining columns proportionate width.... In my mind that should work but not in MIgra Doc....when I execute, the columns are all squeezed on top of one another... here is my code snippet that adds the columns to the Pdf Table section and assigns the column widhts based on the proportions calculated.... tableSection.PageSetup = reportSection.PageSetup.Clone(); tableSection.PageSetup.PageFormat = PageFormat.Letter; tableSection.PageSetup.LeftMargin = Unit.FromInch(0.125); tableSection.PageSetup.RightMargin = Unit.FromInch(0.125); tableSection.PageSetup.BottomMargin = Unit.FromInch(0.5); double availableContentWidth = tableSection.PageSetup.PageWidth.Point - tableSection.PageSetup.LeftMargin.Point - tableSection.PageSetup.RightMargin.Point; //double availableContentWidth = reportSection.PageSetup.PageWidth.Point - reportSection.PageSetup.LeftMargin.Point - reportSection.PageSetup.RightMargin.Point; // 8inches is 576 points double colItemIdRatio = 0.115; double colItemDescRatio = 0.135; double colRemainRatio = (1 - colItemIdRatio - colItemDescRatio) / (numberOfColumns - 2); Table table = tableSection.AddTable(); table.Borders.Width = 0.25; // Example border setting foreach (DataColumn dtColumn in dataTable.Columns) { //column.Format.Alignment = ParagraphAlignment.Left; // Default alignment double columnWidth; if (dtColumn.ColumnName.Contains("item_id")) { columnWidth = availableContentWidth * colItemIdRatio; //columnWidth = Unit.FromInch(1.05); } else if (dtColumn.ColumnName.Contains("item_desc")) { columnWidth = availableContentWidth * colItemDescRatio; //columnWidth = Unit.FromInch(1.45); } else { columnWidth = availableContentWidth * colRemainRatio; //columnWidth = Unit.FromInch(0.4); } //Column column = table.AddColumn(columnWidth); table.AddColumn(Unit.FromPoint(columnWidth)); } The commented out code works because I am assigning an absolute width, but the other code causes the issue I described above...and I'm not entirely sure why at this point. I have read the GitHub documentation for MigrDoc and creating Tables, and I have tried several differnat proportiional type calculation approaches that all end in the same outcome. I am really hoping that someone can point me in the right direction as to why MigraDoc is behaving this way....what am I missing here. I can provide more of the source code if necessary...I just assumed that I provided the most relevant piece of the puzzle. Regards, Brad |
Author: | !!! [ Fri Aug 15, 2025 1:28 pm ] |
Post subject: | Re: Set Table Column width using proportional algorithm |
bradvan71 wrote: I can provide more of the source code if necessary...I just assumed that I provided the most relevant piece of the puzzle. You can provide a MDDDL file containing such a table. One table with a total of three columns should be enough.Nothing obviously wrong with your code snippet. Submitting a complete VS solution is sometimes more helpful than just showing snippets, especially if the real issue is not covered by the snippets. |
Author: | bradvan71 [ Fri Aug 22, 2025 4:40 pm ] |
Post subject: | Re: Set Table Column width using proportional algorithm |
Hi, There are more than 3 columns to the tables...the first 2 always being constant which contain an item code and a item description. so if the table section is set as Letter sized widht less margins for left and right...columns 1 and 2 are set as a proportion of that available width...the remsaining columns would share the remaining available widht based on the number of columns which could vary depending on the DataTable in the DataSet.. I am trying to make those remaining column widths be dynamic based on remaining available widht / remaining number of columns. Unless I am missing something in my readings of what the MDDDL file does, I'm not sure it will help. I'm not understanding what MigraDoc is doing under the hood when creating a table section as I am and using proportionate columns widths...it doesn't seem to like how I have contained the math inside my loop to create the Table columns in the Table section. Regards, Brad |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |