Sayeed Ahmed
Sayeed Ahmed

Reputation: 53

Adjust pdf text dynamically according to Text width using iTextSharp C#

I'm using iTexSharp for PDF in my application. I got a scenario to display some text details (Patient disease details). I have two columns 1st Column displays for Patient Details and 2nd Column displays patients illness. In 2nd Column I have to display the patient disease text from Left to Right in text indent. In PDF I have an Blue Arrow line which will be the middle of the PDF page between two columns. Whenever I have the text length less than 25 characters I'm fine to display the text from left to right align, but when text exceeds more than 20 characters in length I have to adjust little left to right the content but in this case I'm ending up displaying the text from right to left where I see lot of space exist after the text (Disease). Where red line is the starting point of displaying 2nd column text

Text with in 25 chars (Looks' good) enter image description here

Text with more than 25 characters enter image description here

Below is my code to display the two columns from Table

if (patientDetails.DiseaseText.Length > 25 && patientDetails.DiseaseText.Length < 57)
                    welcomeOuterTable.SetWidths(new float[] { 65f, 65f });
                else
                    welcomeOuterTable.SetWidths(new float[] { 97f, 33f });

I need to set the second value of new float[] to be the textwidth length in relativeWidths number as example

 if (patientDetails.DiseaseText.Length > 25 && patientDetails.DiseaseText.Length < 57)
                        welcomeOuterTable.SetWidths(new float[] { 65f, patientDetails.DiseaseText.Length.Width});
                    else
                        welcomeOuterTable.SetWidths(new float[] { 97f, 33f });

Upvotes: 1

Views: 1402

Answers (1)

Hakan Usakli
Hakan Usakli

Reputation: 592

If I understand your question correct, you want to set the 2nd column width dynamically based on the contents of patientDetails.DiseaseText.

If that is the case, you want to know the rendered width of the text, and if it will fit in one line or not.

These links show how you can calculate the rendered width of a string based on a font. From there you can make a decision about your column width.

How to calculate the string width in iText?

iText -- How do I get the rendered dimensions of text?

https://kb.itextpdf.com/home/it5kb/faq/how-to-get-the-rendered-dimensions-of-text

Upvotes: 1

Related Questions