Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to print in PDF two columns of information that contain some strings that the user inputs. This is my code so far:
C#
string s = "";                                                                  
s = s + string.Format("{0,-60}", "Name: " + name);
s = s + string.Format("{0,15}","AAA: ");
p1.Add(s);
document.Add(p1);

string p = "";                                                                 
p = p + string.Format("{0,-60}", "Surname: " + surname);
p = p + string.Format("{0,15}","BBB: ");
p2.Add(p);
document.Add(p2);

string r = "";                                                        
r = r + string.Format("{0,-60}", "School: " + school);
r = r + string.Format("{0,15}","CCC: ");
p3.Add(r);
document.Add(p3);

If the user enters, for example, "John Edward Jr." for name, "Pascal Einstein W. Alfi" for surname and "St. John" for school, then the expected output is something like this:

Name: John Edward Jr.________________AAA:

Surname: Pascal Einstein W. Alfi_______BBB:

School: St. John___________________CCC:
I suppose the spaces in every string name, surname and school are the problem. How can I deal with this?

EXPECTED OUTPUT:

Name: John Edward Jr._________________AAA:

Surname: Pascal Einstein W. Alfi_______BBB:

School: St. John________________________CCC:
Posted
Updated 27-Dec-15 22:01pm
v2

1 solution

If you are using a fixed width font, then you just need to use the length of each string to calculate the number of spaces required. If you are using variable width fonts than you need to use graphics draw methods to position each field at a specific point on the page.
 
Share this answer
 
Comments
PeMaCN 28-Dec-15 4:38am    
Can you please edit your solution with some code?
Richard MacCutchan 28-Dec-15 5:32am    
Code for what? In case 1 you just calculate position minus length of string to know how many spaces to add. For case 2 you need to use whatever commands are necessary for absolute positioning of text (which you could also use for case 1).
PeMaCN 28-Dec-15 5:47am    
What position to calculate? Do you mean this: int width = 60 - name.Count(Char.IsWhiteSpace);
Richard MacCutchan 28-Dec-15 5:52am    
Well only you can answer that, as only you know where the second column starts.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900