如何制作可根据文本自动调整的pdftable
how to make a pdftable that is auto adjustable according to text
我正在使用 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 个字符,那么接下来的行的顺序将被更改
我假设您有一个包含三列的 table:
PdfPTable table = new PdfPTable(3);
您想向此 table 添加行,但您没有添加行,而是只添加了三个单元格,并且您正试图通过使用 \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);
这就是table应该的用法。现在,如果一些数据太大而不适合宽度,数据将分布在不同的行上,但不同的行将保持对齐(这在您的设置中不是这种情况)。
我删除了 \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 列的两倍。
我正在使用 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 个字符,那么接下来的行的顺序将被更改
我假设您有一个包含三列的 table:
PdfPTable table = new PdfPTable(3);
您想向此 table 添加行,但您没有添加行,而是只添加了三个单元格,并且您正试图通过使用 \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);
这就是table应该的用法。现在,如果一些数据太大而不适合宽度,数据将分布在不同的行上,但不同的行将保持对齐(这在您的设置中不是这种情况)。
我删除了 \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 列的两倍。