public void AutoAdjustColumnWidths() { double[] array = new double[this.tableData[0].Count]; for (int i = 0; i < this.tableData.Count; i++) { List <Cell> list = this.tableData[i]; for (int j = 0; j < list.Count; j++) { Cell cell = list[j]; if (cell.GetColSpan() == 1 && cell.text != null) { cell.SetWidth((double)cell.font.StringWidth(cell.text)); if (array[j] == 0.0 || (double)cell.GetWidth() > array[j]) { array[j] = (double)cell.GetWidth(); } } } } for (int k = 0; k < this.tableData.Count; k++) { List <Cell> list2 = this.tableData[k]; for (int l = 0; l < list2.Count; l++) { Cell cell2 = list2[l]; cell2.SetWidth(array[l] + (double)(3f * this.padding)); } } }
/** * Auto adjusts the widths of all columns so that they are just wide enough to hold the text without truncation. */ public void AutoAdjustColumnWidths() { // Find the maximum text width for each column float[] max_col_widths = new float[tableData[0].Count]; for (int i = 0; i < tableData.Count; i++) { List <Cell> row = tableData[i]; for (int j = 0; j < row.Count; j++) { Cell cell = row[j]; if (cell.GetColSpan() == 1) { float cellWidth = 0f; if (cell.image != null) { cellWidth = cell.image.GetWidth(); } if (cell.text != null) { if (cell.font.StringWidth(cell.fallbackFont, cell.text) > cellWidth) { cellWidth = cell.font.StringWidth(cell.fallbackFont, cell.text); } } cell.SetWidth(cellWidth + cell.left_padding + cell.right_padding); if (max_col_widths[j] == 0f || cell.GetWidth() > max_col_widths[j]) { max_col_widths[j] = cell.GetWidth(); } } } } for (int i = 0; i < tableData.Count; i++) { List <Cell> row = tableData[i]; for (int j = 0; j < row.Count; j++) { Cell cell = row[j]; cell.SetWidth(max_col_widths[j]); } } }
/** * Auto adjusts the widths of all columns so that they are just wide enough to hold the text without truncation. */ public void AutoAdjustColumnWidths() { float[] maxColWidths = new float[tableData[0].Count]; for (int i = 0; i < numOfHeaderRows; i++) { for (int j = 0; j < maxColWidths.Length; j++) { Cell cell = tableData[i][j]; float textWidth = cell.font.StringWidth(cell.fallbackFont, cell.text); textWidth += cell.leftPadding + cell.rightPadding; if (textWidth > maxColWidths[j]) { maxColWidths[j] = textWidth; } } } for (int i = numOfHeaderRows; i < tableData.Count; i++) { for (int j = 0; j < maxColWidths.Length; j++) { Cell cell = tableData[i][j]; if (cell.GetColSpan() > 1) { continue; } if (cell.text != null) { float textWidth = cell.font.StringWidth(cell.fallbackFont, cell.text); textWidth += cell.leftPadding + cell.rightPadding; if (textWidth > maxColWidths[j]) { maxColWidths[j] = textWidth; } } if (cell.image != null) { float imageWidth = cell.image.GetWidth() + cell.leftPadding + cell.rightPadding; if (imageWidth > maxColWidths[j]) { maxColWidths[j] = imageWidth; } } if (cell.barCode != null) { try { float barcodeWidth = cell.barCode.DrawOn(null)[0] + cell.leftPadding + cell.rightPadding; if (barcodeWidth > maxColWidths[j]) { maxColWidths[j] = barcodeWidth; } } catch (Exception) { } } if (cell.textBlock != null) { String[] tokens = Regex.Split(cell.textBlock.text, @"\s+"); foreach (String token in tokens) { float tokenWidth = cell.textBlock.font.StringWidth(cell.textBlock.fallbackFont, token); tokenWidth += cell.leftPadding + cell.rightPadding; if (tokenWidth > maxColWidths[j]) { maxColWidths[j] = tokenWidth; } } } } } for (int i = 0; i < tableData.Count; i++) { List <Cell> row = tableData[i]; for (int j = 0; j < row.Count; j++) { Cell cell = row[j]; cell.SetWidth(maxColWidths[j] + 0.1f); } } AutoResizeColumnsWithColspanBiggerThanOne(); }
private void AddExtraTableRows(List <List <Cell> > tableData2) { foreach (List <Cell> row in tableData) { int maxNumVerCells = 0; foreach (Cell cell in row) { int numVerCells = cell.GetNumVerCells(); if (numVerCells > maxNumVerCells) { maxNumVerCells = numVerCells; } } for (int i = 0; i < maxNumVerCells; i++) { List <Cell> row2 = new List <Cell>(); foreach (Cell cell in row) { Cell cell2 = new Cell(cell.GetFont(), ""); cell2.SetFallbackFont(cell.GetFallbackFont()); cell2.SetWidth(cell.GetWidth()); if (i == 0) { cell2.SetPoint(cell.GetPoint()); cell2.SetTopPadding(cell.topPadding); } if (i == (maxNumVerCells - 1)) { cell2.SetBottomPadding(cell.bottomPadding); } cell2.SetLeftPadding(cell.leftPadding); cell2.SetRightPadding(cell.rightPadding); cell2.SetLineWidth(cell.lineWidth); cell2.SetBgColor(cell.GetBgColor()); cell2.SetPenColor(cell.GetPenColor()); cell2.SetBrushColor(cell.GetBrushColor()); cell2.SetProperties(cell.GetProperties()); cell2.SetVerTextAlignment(cell.GetVerTextAlignment()); if (i == 0) { if (cell.image != null) { cell2.SetImage(cell.GetImage()); } if (cell.GetCompositeTextLine() != null) { cell2.SetCompositeTextLine(cell.GetCompositeTextLine()); } else { cell2.SetText(cell.GetText()); } if (maxNumVerCells > 1) { cell2.SetBorder(Border.BOTTOM, false); } } else { cell2.SetBorder(Border.TOP, false); if (i < (maxNumVerCells - 1)) { cell2.SetBorder(Border.BOTTOM, false); } } row2.Add(cell2); } tableData2.Add(row2); } } }
/** * Wraps around the text in all cells so it fits the column width. * This method should be called after all calls to setColumnWidth and autoAdjustColumnWidths. */ public void WrapAroundCellText() { List <List <Cell> > tableData2 = new List <List <Cell> >(); for (int i = 0; i < tableData.Count; i++) { List <Cell> row = tableData[i]; for (int j = 0; j < row.Count; j++) { Cell cell = row[j]; int colspan = cell.GetColSpan(); for (int n = 1; n < colspan; n++) { Cell next = row[j + n]; cell.SetWidth(cell.GetWidth() + next.GetWidth()); next.SetWidth(0f); } } } // Adjust the number of header rows automatically! numOfHeaderRows = GetNumHeaderRows(); rendered = numOfHeaderRows; AddExtraTableRows(tableData2); for (int i = 0; i < tableData2.Count; i++) { List <Cell> row = tableData2[i]; for (int j = 0; j < row.Count; j++) { Cell cell = row[j]; if (cell.text != null) { int n = 0; float effectiveWidth = cell.width - (cell.leftPadding + cell.rightPadding); String[] tokens = TextUtils.SplitTextIntoTokens( cell.text, cell.font, cell.fallbackFont, effectiveWidth); StringBuilder buf = new StringBuilder(); foreach (String token in tokens) { if (cell.font.StringWidth(cell.fallbackFont, (buf.ToString() + " " + token).Trim()) > effectiveWidth) { tableData2[i + n][j].SetText(buf.ToString().Trim()); buf = new StringBuilder(token); n++; } else { buf.Append(" "); buf.Append(token); } } tableData2[i + n][j].SetText(buf.ToString().Trim()); } else { tableData2[i][j].SetCompositeTextLine(cell.GetCompositeTextLine()); } } } tableData = tableData2; }
/** * Wraps around the text in all cells so it fits the column width. * This method should be called after all calls to SetColumnWidth and AutoAdjustColumnWidths. * */ public void WrapAroundCellText() { List <List <Cell> > tableData2 = new List <List <Cell> >(); for (int i = 0; i < tableData.Count; i++) { List <Cell> row = tableData[i]; int maxNumVerCells = 1; for (int j = 0; j < row.Count; j++) { Cell cell = row[j]; int colspan = cell.GetColSpan(); for (int n = 1; n < colspan; n++) { Cell next = row[j + n]; cell.SetWidth(cell.GetWidth() + next.GetWidth()); next.SetWidth(0f); } int numVerCells = cell.GetNumVerCells(); if (numVerCells > maxNumVerCells) { maxNumVerCells = numVerCells; } } for (int j = 0; j < maxNumVerCells; j++) { List <Cell> row2 = new List <Cell>(); for (int k = 0; k < row.Count; k++) { Cell cell = row[k]; Cell cell2 = new Cell(cell.GetFont(), ""); cell2.SetFallbackFont(cell.GetFallbackFont()); cell2.SetPoint(cell.GetPoint()); cell2.SetWidth(cell.GetWidth()); if (j == 0) { cell2.SetTopPadding(cell.top_padding); } if (j == (maxNumVerCells - 1)) { cell2.SetBottomPadding(cell.bottom_padding); } cell2.SetLeftPadding(cell.left_padding); cell2.SetRightPadding(cell.right_padding); cell2.SetLineWidth(cell.GetLineWidth()); cell2.SetBgColor(cell.GetBgColor()); cell2.SetPenColor(cell.GetPenColor()); cell2.SetBrushColor(cell.GetBrushColor()); cell2.SetProperties(cell.GetProperties()); cell2.SetVerTextAlignment(cell.GetVerTextAlignment()); cell2.SetIgnoreImageHeight(cell.GetIgnoreImageHeight()); if (j == 0) { cell2.SetImage(cell.GetImage()); if (cell.GetCompositeTextLine() != null) { cell2.SetCompositeTextLine(cell.GetCompositeTextLine()); } else { cell2.SetText(cell.GetText()); } if (maxNumVerCells > 1) { cell2.SetBorder(Border.BOTTOM, false); } } else { cell2.SetBorder(Border.TOP, false); if (j < (maxNumVerCells - 1)) { cell2.SetBorder(Border.BOTTOM, false); } } row2.Add(cell2); } tableData2.Add(row2); } } for (int i = 0; i < tableData2.Count; i++) { List <Cell> row = tableData2[i]; for (int j = 0; j < row.Count; j++) { Cell cell = row[j]; if (cell.text != null) { int n = 0; String[] textLines = cell.GetText().Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None); foreach (String textLine in textLines) { StringBuilder sb = new StringBuilder(); String[] tokens = Regex.Split(textLine, @"\s+"); if (tokens.Length == 1) { sb.Append(tokens[0]); } else { for (int k = 0; k < tokens.Length; k++) { String token = tokens[k]; if (cell.font.StringWidth(cell.fallbackFont, sb.ToString() + " " + token) > (cell.GetWidth() - (cell.left_padding + cell.right_padding))) { tableData2[i + n][j].SetText(sb.ToString()); sb = new StringBuilder(token); n++; } else { if (k > 0) { sb.Append(" "); } sb.Append(token); } } } tableData2[i + n][j].SetText(sb.ToString()); n++; } } else { tableData2[i][j].SetCompositeTextLine(cell.GetCompositeTextLine()); } } } tableData = tableData2; }
/** * Wraps around the text in all cells so it fits the column width. * This method should be called after all calls to SetColumnWidth and AutoAdjustColumnWidths. * */ public void WrapAroundCellText() { List<List<Cell>> tableData2 = new List<List<Cell>>(); for (int i = 0; i < tableData.Count; i++) { List<Cell> row = tableData[i]; int maxNumVerCells = 1; for (int j = 0; j < row.Count; j++) { Cell cell = row[j]; int colspan = cell.GetColSpan(); for (int n = 1; n < colspan; n++) { Cell next = row[j + n]; cell.SetWidth(cell.GetWidth() + next.GetWidth()); next.SetWidth(0f); } int numVerCells = cell.GetNumVerCells(); if (numVerCells > maxNumVerCells) { maxNumVerCells = numVerCells; } } for (int j = 0; j < maxNumVerCells; j++) { List<Cell> row2 = new List<Cell>(); for (int k = 0; k < row.Count; k++) { Cell cell = row[k]; Cell cell2 = new Cell(cell.GetFont(), ""); cell2.SetFallbackFont(cell.GetFallbackFont()); cell2.SetPoint(cell.GetPoint()); cell2.SetCompositeTextLine(cell.GetCompositeTextLine()); cell2.SetWidth(cell.GetWidth()); if (j == 0) { cell2.SetTopPadding(cell.top_padding); } if (j == (maxNumVerCells - 1)) { cell2.SetBottomPadding(cell.bottom_padding); } cell2.SetLeftPadding(cell.left_padding); cell2.SetRightPadding(cell.right_padding); cell2.SetLineWidth(cell.GetLineWidth()); cell2.SetBgColor(cell.GetBgColor()); cell2.SetPenColor(cell.GetPenColor()); cell2.SetBrushColor(cell.GetBrushColor()); cell2.SetProperties(cell.GetProperties()); cell2.SetImage(cell.GetImage()); if (j == 0) { cell2.SetText(cell.GetText()); if (maxNumVerCells > 1) { cell2.SetBorder(Border.BOTTOM, false); } } else { cell2.SetBorder(Border.TOP, false); if (j < (maxNumVerCells - 1)) { cell2.SetBorder(Border.BOTTOM, false); } } row2.Add(cell2); } tableData2.Add(row2); } } for (int i = 0; i < tableData2.Count; i++) { List<Cell> row = tableData2[i]; for (int j = 0; j < row.Count; j++) { Cell cell = row[j]; if (cell.text != null) { int n = 0; String[] tokens = Regex.Split(cell.GetText(), @"\s+"); StringBuilder sb = new StringBuilder(); if (tokens.Length == 1) { sb.Append(tokens[0]); } else { for (int k = 0; k < tokens.Length; k++) { String token = tokens[k]; if (cell.font.StringWidth(sb.ToString() + " " + token) > (cell.GetWidth() - (cell.left_padding + cell.right_padding))) { tableData2[i + n][j].SetText(sb.ToString()); sb = new StringBuilder(token); n++; } else { if (k > 0) { sb.Append(" "); } sb.Append(token); } } } tableData2[i + n][j].SetText(sb.ToString()); } } } tableData = tableData2; }
public void WrapAroundCellText() { List <List <Cell> > list = new List <List <Cell> >(); for (int i = 0; i < this.tableData.Count; i++) { List <Cell> list2 = this.tableData[i]; int num = 1; for (int j = 0; j < list2.Count; j++) { Cell cell = list2[j]; int colSpan = cell.GetColSpan(); for (int k = 1; k < colSpan; k++) { Cell cell2 = list2[j + k]; cell.SetWidth((double)(cell.GetWidth() + cell2.GetWidth())); cell2.SetWidth(0.0); } int numVerCells = cell.GetNumVerCells(this.padding); if (numVerCells > num) { num = numVerCells; } } for (int l = 0; l < num; l++) { List <Cell> list3 = new List <Cell>(); for (int m = 0; m < list2.Count; m++) { Cell cell3 = list2[m]; Cell cell4 = new Cell(cell3.GetFont(), ""); cell4.SetPoint(cell3.GetPoint()); cell4.SetCompositeTextLine(cell3.GetCompositeTextLine()); cell4.SetWidth((double)cell3.GetWidth()); if (l == 0) { cell4.SetTopPadding(cell3.top_padding); } if (l == num - 1) { cell4.SetBottomPadding(cell3.bottom_padding); } cell4.SetLeftPadding(cell3.left_padding); cell4.SetRightPadding(cell3.right_padding); cell4.SetLineWidth(cell3.GetLineWidth()); cell4.SetBgColor(cell3.GetBgColor()); cell4.SetPenColor(cell3.GetPenColor()); cell4.SetBrushColor(cell3.GetBrushColor()); cell4.SetProperties(cell3.GetProperties()); if (l == 0) { cell4.SetText(cell3.GetText()); } else { if (l > 0) { cell4.SetBorder(65536, false); if (l < num - 1) { cell4.SetBorder(131072, false); } } } list3.Add(cell4); } list.Add(list3); } } for (int n = 0; n < list.Count; n++) { List <Cell> list4 = list[n]; for (int num2 = 0; num2 < list4.Count; num2++) { Cell cell5 = list4[num2]; if (cell5.text != null) { int num3 = 0; string[] array = Regex.Split(cell5.GetText(), "\\s+"); StringBuilder stringBuilder = new StringBuilder(); if (array.Length == 1) { stringBuilder.Append(array[0]); } else { for (int num4 = 0; num4 < array.Length; num4++) { string text = array[num4]; if (cell5.font.StringWidth(stringBuilder.ToString() + " " + text) > cell5.GetWidth() - (cell5.left_padding + cell5.right_padding + 2f * this.padding)) { list[n + num3][num2].SetText(stringBuilder.ToString()); stringBuilder = new StringBuilder(text); num3++; } else { if (num4 > 0) { stringBuilder.Append(" "); } stringBuilder.Append(text); } } } list[n + num3][num2].SetText(stringBuilder.ToString()); } } } this.tableData = list; }