How to interpret float[] widths1 = new float[] { 20f, 20f, 60f };

Malam Malam 266 Reputation points
2022-02-23T17:39:25.483+00:00

How is this width calculated for iTextSharp page width when creating a pdf?
float[] widths1 = new float[] { 20f, 20f, 60f };
float[] widths1 = new float[] { 20f, 20f, 60f };
tbfooter.SetWidths(widths1);
tbfooter.WriteSelectedRows(0, -1, document.LeftMargin, writer.PageSize.GetBottom(document.BottomMargin), writer.DirectContent);

A Fair Lending and Equal Employment Opportunity Agency. For assistance: Individuals with disabilities may contact the ADA Administrator at 924-123-4567, TTY 456-765-1234 or ******@ILoveThisForum.org
The footer above is a long string (using smaller font size 8) but pdf only shows only the half; has about 2 inch space on the left and 1.75 inch on the right. It looks like:

xxxxxxxxxxxxxxxxxxxxx A Fair Lending and Equal Employment Opportunity Agency. For assistance: Individuals with disabilities xxxxxxxxxxxxxxxxx

where X=empty space

Developer technologies ASP.NET Other
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2022-02-24T05:52:18.907+00:00

    Hi @Malam Malam ,
    float[] widths1 = new float[] { 20f, 20f, 60f };
    This line of code distributes the column width proportionally to the width of the table.
    The following code means that the width of the column itself is set relative to one-third and two-thirds of the total width of the table.
    To set it to one-fifth and four-fifth, you would pass in 1f and 4f, respectively.
    You can set the absolute width by passing in a value for the total width of the table, for example:

    table.TotalWidth = 216f;  
    //relative col widths in proportions - 1/3 and 2/3  
    float[] widths = new float[] { 1f, 2f };  
    //table total width value  
    float[] widths = new float[] { 100f, 116f };  
    

    I suggest you to set PdfPTable to 1, you can refer to the code below:

     protected void btnGeneratePDFFile_Click(object sender, EventArgs e)  
            {  
                Document document = new Document();  
                var output = new FileStream(Server.MapPath("MyFirstPDF.pdf"), FileMode.Create);  
                var writer = PdfWriter.GetInstance(document, output);  
                document.Open();  
                PdfPTable tbfooter = new PdfPTable(1);  
                tbfooter.TotalWidth = document.PageSize.Width - document.LeftMargin - document.RightMargin;  
                tbfooter.DefaultCell.Border = 0;  
                tbfooter.AddCell(new Paragraph());  
                tbfooter.AddCell(new Paragraph());  
                var _cell2 = new PdfPCell(new Paragraph(new Chunk("A Fair Lending and Equal Employment Opportunity Agency. For assistance: Individuals with disabilities may contact the ADA Administrator at 924-123-4567, TTY 456-765-1234 or ******@ILoveThisForum.org", FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLUE))));  
                _cell2.HorizontalAlignment = Element.ALIGN_LEFT;  
                _cell2.Border = 0;  
                tbfooter.AddCell(_cell2);  
                tbfooter.AddCell(new Paragraph());  
                tbfooter.AddCell(new Paragraph());  
                var _celly = new PdfPCell(new Paragraph(writer.PageNumber.ToString()));//For page no.  
                _celly.HorizontalAlignment = Element.ALIGN_RIGHT;  
                _celly.Border = 0;  
                tbfooter.AddCell(_celly);  
                float[] widths1 = new float[] {1f };  
                tbfooter.SetWidths(widths1);  
                tbfooter.WriteSelectedRows(0, -1, document.LeftMargin, writer.PageSize.GetBottom(document.BottomMargin), writer.DirectContent);  
                document.Add(tbfooter);  
                document.Close();  
            }     
            protected void btnOpenPDFFile_Click(object sender, EventArgs e)  
            {  
                //Open the PDF file    
                Process.Start(Server.MapPath("MyFirstPDF.pdf"));  
                btnGeneratePDFFile.Enabled = true;  
                btnOpenPDFFile.Enabled = false;  
            }  
    

    177340-image.png
    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Jose Zero 576 Reputation points
    2022-02-23T22:43:56.817+00:00

    SetWidths is used to set each column width in a multi column table, each width is relative to table TotalWidth.
    In your case assuming a table with 3 columns and TotalWidth = 100, you are setting Col1 = 20%, Col2 = 20% and Col3 = 60%

    Here a sample for table with 3 columns

    PdfPTable Table = new PdfPTable(3);
        float[] widths = new float[] { 20, 30, 50 };
        Table.TotalWidth = 250;
        Table.SetWidths(widths);
        Table.AddCell("ABCDEFGHIJ");
        Table.AddCell("1234567890");
        Table.AddCell("abcde12345");
        Table.WriteSelectedRows(0, -1, 10, 200, cb)
    

    Another sample with just 1 column

    PdfPTable Table = new PdfPTable(1);
        Table.TotalWidth = 250;
        Table.AddCell("TestTestTestTestTestTestTestTestTestTestTestTest");
        Table.WriteSelectedRows(0, -1, 10, 200, cb);
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.