【问题标题】:how to make a pdftable that is auto adjustable according to text如何制作可根据文本自动调整的 pdftable
【发布时间】:2016-10-06 10:43:23
【问题描述】:

我正在处理 winforms,需要创建一个 pdf 页面,用户可以在其中输入他的详细信息,我的代码是:

    PdfPCell first_form1 = new PdfPCell();


        first_form1.AddElement(new Phrase("6)  Complete  Postal  Address " + "\n\n", normalFontBold));
        first_form1.AddElement(new Phrase("7)  Date  of  Birth(Proof in the form of birth certificate or school certificate or medical certificate indicating age) " + "\n\n", normalFontBold));
        first_form1.AddElement(new Phrase("8)  Age (in complete years) as on the date of marriage " + "\n\n", normalFontBold));
        first_form1.AddElement(new Phrase("9)  Date  Of  Marriage " + "\n\n", normalFontBold));
        first_form1.AddElement(new Phrase("10)  Place  Of  Marriage " + "\n\n", normalFontBold));


        PdfPCell second_form1 = new PdfPCell();

        second_form1.AddElement(new Phrase(boy_address + "\n\n", normalFontBold));
        second_form1.AddElement(new Phrase(boy_dob + "\n\n\n\n", normalFontBold));
        second_form1.AddElement(new Phrase(boy_age + "\n\n\n", normalFontBold));
        second_form1.AddElement(new Phrase(date_0f_mrg + "\n\n", normalFontBold));
        second_form1.AddElement(new Phrase(place_of_mrg + "\n\n", normalFontBold));

        PdfPCell third_form1 = new PdfPCell();
        third_form1.AddElement(new Phrase(girl_address + "\n\n", normalFontBold));
        third_form1.AddElement(new Phrase(girl_dob + "\n\n\n\n", normalFontBold));
        third_form1.AddElement(new Phrase(girl_age + "\n\n\n", normalFontBold));
        third_form1.AddElement(new Phrase(date_0f_mrg + "\n\n", normalFontBold));
        third_form1.AddElement(new Phrase(place_of_mrg + "\n\n", normalFontBold));

        detailsTable_form1.AddCell(first_form1);
        detailsTable_form1.AddCell(second_form1);
        detailsTable_form1.AddCell(third_form1);

// 如果我的地址几乎相等的字符,那么它打印得很好,但是如果我的男孩地址太大,假设它有超过 100 个字符,那么接下来的行的顺序将会改变

【问题讨论】:

  • 您不正确地使用PdfPTable。您正在使用 \n 在具有行功能时定位文本。使用行!
  • 我已经提供了答案。它是如此的微不足道,以至于我很惊讶你要我提供示例代码。

标签: c# asp.net itext


【解决方案1】:

我假设您有一个包含三列的表格:

PdfPTable table = new PdfPTable(3);

您想在此表中添加行,但不是添加行,而是仅添加三个单元格,并且您试图通过使用 \n 字符来模仿行。这是个坏主意。你应该这样做:

// row 1
PdfPCell first_form1 = new PdfPCell();
first_form1.AddElement(new Phrase("6)  Complete  Postal  Address ", normalFontBold));
table.AddCell(first_form1);
PdfPCell second_form1 = new PdfPCell();
second_form1.AddElement(new Phrase(boy_address, normalFontBold));
table.AddCell(second_form1);
PdfPCell third_form1 = new PdfPCell();
third_form1.AddElement(new Phrase(girl_address, normalFontBold));
table.AddCell(third_form1);
// row 2
first_form1 = new PdfPCell();
first_form1.AddElement(new Phrase("7)  Date  of  Birth(Proof in the form of birth certificate or school certificate or medical certificate indicating age) ", normalFontBold));
table.AddCell(first_form1);
second_form1 = new PdfPCell();
second_form1.AddElement(new Phrase(boy_dob, normalFontBold));
table.AddCell(second_form1);
third_form1 = new PdfPCell();
third_form1.AddElement(new Phrase(girl_dob, normalFontBold));
table.AddCell(third_form1);
// row 3
first_form1 = new PdfPCell();
first_form1.AddElement(new Phrase("8)  Age (in complete years) as on the date of marriage ", normalFontBold));
table.AddCell(first_form1);
second_form1 = new PdfPCell();
second_form1.AddElement(new Phrase(boy_age, normalFontBold));
table.AddCell(second_form1)
third_form1 = new PdfPCell();
third_form1.AddElement(new Phrase(girl_age, normalFontBold));
table.AddCell(third_form1);;
// row 4
first_form1 = new PdfPCell();
first_form1.AddElement(new Phrase("9)  Date  Of  Marriage " + "\n\n", normalFontBold));
table.AddCell(first_form1);
second_form1 = new PdfPCell();
second_form1.AddElement(new Phrase(date_0f_mrg, normalFontBold));
table.AddCell(second_form1);
third_form1 = new PdfPCell();
third_form1.AddElement(new Phrase(date_0f_mrg, normalFontBold));
table.AddCell(third_form1);
// row 5
first_form1 = new PdfPCell();
first_form1.AddElement(new Phrase("10)  Place  Of  Marriage " + "\n\n", normalFontBold));
table.AddCell(first_form1);
second_form1 = new PdfPCell();
second_form1.AddElement(new Phrase(place_of_mrg, normalFontBold));
table.AddCell(second_form1);
third_form1 = new PdfPCell();
third_form1.AddElement(new Phrase(place_of_mrg, normalFontBold));
table.AddCell(third_form1);

这就是应该使用表格的方式。现在,如果某些数据太大而无法适应宽度,数据将分布在不同的行上,但不同的行将保持对齐(您的设置中并非如此)。

我删除了\n,但如果您希望行具有最小高度,您可以使用MinimumHeight 属性。例如:

first_form1.MinimumHeight = 48;

在我的代码中,将绘制边框。如果要更改或删除它们,可以使用 Border 属性。例如:

second_form1.Border = Rectangle.NO_BORDER;

如果您希望某些列具有不同的宽度,您可以更改WidthPercentage

table.WidthPercentage = 100;

以及相对列宽:

float[] widths = new float[] { 1, 2, 2 };
table.SetWidths(widths);

在这种情况下,第 2 列和第 3 列的宽度是第 1 列的两倍。

【讨论】:

  • 谢谢..!!非常适合我!!
  • 在这种情况下,请勾选答案分数旁边的复选标记以接受答案。
猜你喜欢
  • 1970-01-01
  • 2013-06-11
  • 2013-04-09
  • 2014-05-04
  • 1970-01-01
  • 2011-05-07
  • 1970-01-01
  • 2012-05-21
  • 1970-01-01
相关资源
最近更新 更多