0

I am working on winforms and need to create a pdf page in which users enters his details and my code is :

    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);

// And if i am having address almost equal characters then it prints fine but if i m having boy address too large suppose it is having more than 100 characters then the sequence for the next upcoming lines will be changed

1 Answers1

1

I assume that you have a table with three columns:

PdfPTable table = new PdfPTable(3);

You want to add rows to this table, but instead of adding rows, you only add three cells and you are trying to mimick rows by using \n characters. That's a bad idea. You should do somthing like this:

// 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);

This is how tables should be used. Now if some data is too large to fit the width, the data will be distributed over different lines, but the different rows will remain aligned (which wasn't the case in your setup).

I dropped the \n, but if you want the rows to have a minimum height, you can use the MinimumHeight property. For instance:

first_form1.MinimumHeight = 48;

In my code, borders will be drawn. If you want to change or remove them, you can use the Border property. For instance:

second_form1.Border = Rectangle.NO_BORDER;

If you want some columns to have a different width, you can change the WidthPercentage:

table.WidthPercentage = 100;

And the relative column widths:

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

In this case, column 2 and 3 are twice as wide as column 1.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165