The following code creates a table and a nested table. However, the nested table is always aligned in the middle. I do not know how to perform horizontal alignment correctly.
The following method simply iterates over the list. when this is a simple question, I will add a TextField. if the question contains multiple answers, I insert a nested table using checkboxes and values.
private PdfPTable CreateQuestionTable(RLQuestionRecordList questions)
{
PdfPCell cell;
PdfPTable table = new PdfPTable(2);
table.WidthPercentage = 100;
table.SpacingBefore = 20;
table.SpacingAfter = 20;
table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
foreach (RCQuestionRecord q in questions)
{
cell = new PdfPCell(new Phrase(q.ssSTQuestion.ssName, _Normal));
cell.Border = Rectangle.NO_BORDER;
cell.Padding = 5.0f;
table.AddCell(cell);
if (q.ssSTQuestion.ssListOfAnswers.Length > 0)
{
cell = new PdfPCell();
cell.Border = Rectangle.NO_BORDER;
cell.Padding = 5.0f;
cell.HorizontalAlignment = Element.ALIGN_LEFT;
cell.AddElement(CreateCheckboxTable(q));
table.AddCell(cell);
}
else
{
cell = new PdfPCell();
cell.Border = Rectangle.NO_BORDER;
cell.Padding = 5.0f;
cell.HorizontalAlignment = Element.ALIGN_LEFT;
cell.CellEvent = new OOMTextField(string.Format("question_{0}", q.ssSTQuestion.ssQuestionId), q.ssSTQuestion.ssLength, q.ssSTQuestion.ssValue, bf);
table.AddCell(cell);
}
}
return table;
}
This is the nested table that I want to insert in the cell above.
private PdfPTable CreateCheckboxTable(RCQuestionRecord question)
{
PdfPCell cell;
int numCells = question.ssSTQuestion.ssListOfAnswers.Length;
PdfPTable table = new PdfPTable(numCells);
float[] widths = new float[numCells];
int currentColumn = 0;
foreach (RCAnswerRecord a in question.ssSTQuestion.ssListOfAnswers) {
cell = new PdfPCell(new Phrase(a.ssSTAnswer.ssLabel, _Normal));
cell.Border = Rectangle.NO_BORDER;
cell.Padding = 0.0f;
cell.PaddingLeft = 20.0f;
cell.HorizontalAlignment = Element.ALIGN_LEFT;
cell.VerticalAlignment = Element.ALIGN_CENTER;
cell.CellEvent = new OOMCheckBox(string.Format("question_{0}", question.ssSTQuestion.ssQuestionId), a.ssSTAnswer.ssIsSelected, a.ssSTAnswer.ssLabel, bf);
table.AddCell(cell);
widths[currentColumn++] = 20.0f + bf.GetWidthPoint(a.ssSTAnswer.ssLabel, 11);
}
table.SetTotalWidth(widths);
table.LockedWidth = true;
table.SpacingBefore = 0;
return table;
}
What am I missing to completely align the nested tables to the left inside the cell?
source
share