gpt4 book ai didi

itextsharp - 无法在单元格内左对齐嵌套表格

转载 作者:行者123 更新时间:2023-12-04 23:49:19 25 4
gpt4 key购买 nike

以下代码确实生成了表和嵌套表。
但是,嵌套表始终居中对齐。
我不知道如何正确实现水平对齐。

以下方法只是循环遍历列表。
当它是一个简单的问题时,我将添加一个 TextField。
如果这是一个包含多个答案的问题,我将注入(inject)一个带有复选框和值的嵌套表。

   private PdfPTable CreateQuestionTable(RLQuestionRecordList questions)
{
PdfPCell cell;
PdfPTable table = new PdfPTable(2);
//table.SetWidths(new int[]{ 50, 50 });
table.WidthPercentage = 100;
table.SpacingBefore = 20;
table.SpacingAfter = 20;
table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
foreach (RCQuestionRecord q in questions)
{
//add question to the table
cell = new PdfPCell(new Phrase(q.ssSTQuestion.ssName, _Normal));
cell.Border = Rectangle.NO_BORDER;
cell.Padding = 5.0f;
table.AddCell(cell);

//add answer to the table.
//add generate time we don;t know where the table will be,
//hence textfields will be generated after the pdf has been generated..

if (q.ssSTQuestion.ssListOfAnswers.Length > 0)
{
// we have radiobuttons, so we add a table inside the cell
cell = new PdfPCell();
cell.Border = Rectangle.NO_BORDER;
cell.Padding = 5.0f;
//we cannot align the table to the left in the cell for some weird reason...
cell.HorizontalAlignment = Element.ALIGN_LEFT;
cell.AddElement(CreateCheckboxTable(q));
table.AddCell(cell);
}
else
{
// we have simple textfield, so add that to the cell
cell = new PdfPCell();
cell.Border = Rectangle.NO_BORDER;
cell.Padding = 5.0f;
cell.HorizontalAlignment = Element.ALIGN_LEFT;
//simple textfield
cell.CellEvent = new OOMTextField(string.Format("question_{0}", q.ssSTQuestion.ssQuestionId), q.ssSTQuestion.ssLength, q.ssSTQuestion.ssValue, bf);
table.AddCell(cell);
}
}
return table;
}

这是我要插入到上面的单元格中的嵌套表。
    /// <summary>
///
/// </summary>
/// <param name="question"></param>
/// <returns></returns>
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;
//table.SetWidths(new int[]{ 50, 50 });
foreach (RCAnswerRecord a in question.ssSTQuestion.ssListOfAnswers) {
//checkbox
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);
//checkbox
table.AddCell(cell);
widths[currentColumn++] = 20.0f + bf.GetWidthPoint(a.ssSTAnswer.ssLabel, 11);
}

table.SetTotalWidth(widths);
table.LockedWidth = true;
table.SpacingBefore = 0;
return table;
}

我缺少什么将嵌套表完全对齐到单元格内的左侧?

最佳答案

请查看名为 nested_tables_aligned.pdf 的 PDF 文档:

enter image description here

此示例中的外部表具有三列和一行。此外部表中的每个单元格都包含一个内部表。第一个内表左对齐,第二个居中对齐,第三个右对齐。

此 PDF 是使用名为 NestedTablesAligned 的 Java 示例创建的。 :

public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document(PageSize.A4.rotate());
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
float[] columnWidths = {200f, 200f, 200f};
PdfPTable table = new PdfPTable(columnWidths);
table.setTotalWidth(600f);
table.setLockedWidth(true);
buildNestedTables(table);
document.add(table);
document.close();
}

private void buildNestedTables(PdfPTable outerTable) {
PdfPTable innerTable1 = new PdfPTable(1);
innerTable1.setTotalWidth(100f);
innerTable1.setLockedWidth(true);
innerTable1.setHorizontalAlignment(Element.ALIGN_LEFT);
innerTable1.addCell("Cell 1");
innerTable1.addCell("Cell 2");
outerTable.addCell(innerTable1);
PdfPTable innerTable2 = new PdfPTable(2);
innerTable2.setTotalWidth(100f);
innerTable2.setLockedWidth(true);
innerTable2.setHorizontalAlignment(Element.ALIGN_CENTER);
innerTable2.addCell("Cell 3");
innerTable2.addCell("Cell 4");
outerTable.addCell(innerTable2);
PdfPTable innerTable3 = new PdfPTable(2);
innerTable3.setTotalWidth(100f);
innerTable3.setLockedWidth(true);
innerTable3.setHorizontalAlignment(Element.ALIGN_RIGHT);
innerTable3.addCell("Cell 5");
innerTable3.addCell("Cell 6");
outerTable.addCell(innerTable3);

}

如您所见,我在表格级别定义表格的对齐方式,而不是在单元格级别。示例原则适用于 iTextSharp。在您的 C# 示例中,您为 PdfPCell 定义对齐。而不是 PdfPTable .更改此设置,您的问题将得到解决。

关于itextsharp - 无法在单元格内左对齐嵌套表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26625455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com