Quantcast

Aligning Text with Line Breaks

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Aligning Text with Line Breaks

kmcnet

Hello Everyone and thanks for your help in advance.  I am trying to left align three lines of text using iTextSharp but am running into problems. Here is my code:

 

            Dim cb As PdfContentByte = writer.DirectContent

            Dim PatientHeader As String = _PatientName & Environment.NewLine & "DOB:  " & Environment.NewLine & "MR Number" & _MRNumber

            cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, PatientHeader, 50, 600, 0)

 

However, the text prints only in one line.

 

Thanks


------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
iText-questions mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Aligning Text with Line Breaks

info-2
On 29/03/2012 21:55, Hugh McLaughlin wrote:

cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, PatientHeader, 50, 600, 0)

 

However, the text prints only in one line.

Of course: you're using the ShowTextAligned method.
As documented, this method puts everything on one line.
Please used the SetSimpleColumn method to define a rectangle,
Then use the AddElement method to add elements to the ColumnText object.
Then use the Go method to actually add the content to the page.

It is advised to read the documentation before starting to code.

------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
iText-questions mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Aligning Text with Line Breaks

kmcnet

Thank you so much for the help.  I will endeavor to read the documentation more in depth, however, I, like many, find the available documentation somewhat confusing.

 

If I may ask one more question, I am confused regarding the specification of the coordinates for the SetSimpleColumn.  The documentation and examples I have found seem to require a calculation rather than simply specifying absolute points.   However, the documentation, http://api.itextpdf.com/itext/com/itextpdf/text/pdf/ColumnText.html#setSimpleColumn(float, float, float, float, float, int), seems to suggest absolute points may be specified.  Would you or  someone else be able to clarify this?

 

Thank you again for your patience and help.

 

From: 1T3XT BVBA [mailto:[hidden email]]
Sent: Friday, March 30, 2012 2:32 AM
To: Post all your questions about iText here
Subject: Re: [iText-questions] Aligning Text with Line Breaks

 

On 29/03/2012 21:55, Hugh McLaughlin wrote:

cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, PatientHeader, 50, 600, 0)

 

However, the text prints only in one line.

Of course: you're using the ShowTextAligned method.
As documented, this method puts everything on one line.
Please used the SetSimpleColumn method to define a rectangle,
Then use the AddElement method to add elements to the ColumnText object.
Then use the Go method to actually add the content to the page.

It is advised to read the documentation before starting to code.


------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
iText-questions mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Aligning Text with Line Breaks

info-2
On 30/03/2012 19:30, Hugh McLaughlin wrote:

If I may ask one more question, I am confused regarding the specification of the coordinates for the SetSimpleColumn.


If you have an A4 page, it measures 595 x 842 points and the 0,0 coordinate is in the lower-left corner.
Suppose you want to use ColumnText to fill the complete page but keep a border of half an inch, you set a column with coordinates:
llx = 36
lly = 36
urx = 559
ury = 806

  The documentation and examples I have found seem to require a calculation rather than simply specifying absolute points.


How would you find the "correct points" if you don't do some Math?

   However, the documentation


If we talk about "the" documentation, we refer to "iText in Action - Second Edition" http://itextpdf.com/book/
Learning how to program using the API documentation is like learning to speak a language by reading a single-language dictionary ;-)

------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
iText-questions mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Aligning Text with Line Breaks

kmcnet

Thanks again for the response.  What I meant by calculation is that the example found here:

 

http://www.devshed.com/c/a/Java/Adding-Columns-With-iTextSharp/

 

Calculate the urx and ury corrodinates rather than specifying absolute positions.  Here is where I am having a positioning problem:

 

I display the date with

 

cb.ShowTextAligned(3, "Date:  " & Now().ToShortDateString, 450, 600, 0)

 

When I use:

 

            Column1.SetSimpleColumn(72, 500, 300, 600)

            Column1.Alignment = Element.ALIGN_TOP

 

I think it should draw rectangle that 228 x 100 with the top line of text displaying the same height on the page as the date.

 

Regarding the documentation, I have ordered the book from Amazon and hope to have it soon.

 

Thanks again.

 

From: 1T3XT BVBA [mailto:[hidden email]]
Sent: Friday, March 30, 2012 2:28 PM
To: Post all your questions about iText here
Subject: Re: [iText-questions] Aligning Text with Line Breaks

 

On 30/03/2012 19:30, Hugh McLaughlin wrote:

If I may ask one more question, I am confused regarding the specification of the coordinates for the SetSimpleColumn.


If you have an A4 page, it measures 595 x 842 points and the 0,0 coordinate is in the lower-left corner.
Suppose you want to use ColumnText to fill the complete page but keep a border of half an inch, you set a column with coordinates:
llx = 36
lly = 36
urx = 559
ury = 806


  The documentation and examples I have found seem to require a calculation rather than simply specifying absolute points.


How would you find the "correct points" if you don't do some Math?


   However, the documentation


If we talk about "the" documentation, we refer to "iText in Action - Second Edition" http://itextpdf.com/book/
Learning how to program using the API documentation is like learning to speak a language by reading a single-language dictionary ;-)


------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
iText-questions mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Aligning Text with Line Breaks

info-2
On 30/03/2012 20:59, Hugh McLaughlin wrote:

What I meant by calculation is that the example found here:

http://www.devshed.com/c/a/Java/Adding-Columns-With-iTextSharp/

Calculate the urx and ury corrodinates rather than specifying absolute positions.


Er... it depends what you mean by "calculate".
I see that absolute positions are specified:
lower-left corner: 72, 72
upper-right corner: 217, 288
So this is a rectangle of two by three inches.

  Here is where I am having a positioning problem:

I display the date with

cb.ShowTextAligned(3, "Date:  " & Now().ToShortDateString, 450, 600, 0)

When I use:

            Column1.SetSimpleColumn(72, 500, 300, 600)


I don't understand.
If you want to put a date on position x = 450, y = 600,
why are you creating a rectangle that will start writing on position x = 72, y = 600?

            Column1.Alignment = Element.ALIGN_TOP


??? Element.ALIGN_TOP ???
In ColumnText, you can only use Element.ALIGN_LEFT, Element.ALIGN_CENTER, Element.ALIGN_RIGHT and Element.ALIGN_JUSTIFIED.


I think it should draw rectangle that 228 x 100 with the top line of text displaying the same height on the page as the date.


ColumnText won't draw a rectangle, but use a rectangle of 228 x 100 as boundary for the text you're adding.
As for the same height: you're forgetting to take the leading into account.
450, 600 represents the start of the baseline of the date-text.
Whereas with the other ColumnText 72,600 represents the upper-left corner of the rectangle that will contain the text you're adding to the column.
The position of the baseline will depend on the leading of the text.
The leading that will be used will depend on whether you're working in text mode or in composite mode...
Suppose you're working in composite mode and you're using the default font size (12) and the default leading (1.5 times the font size), then the start of the baseline will be 72, 782, not 72, 600!

------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
iText-questions mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Aligning Text with Line Breaks

kmcnet

Thanks again for the help.  You are misunderstanding what I am saying.  The date being displayed is not a part of the column.  However, I would like the top line of the column to be vertically aligned with the date.  So your explanation of leading makes perfect sense as I do not take that into account.  I am not sure how to do this, but will dig deeper.

 

As far as ALIGN_TOP, intellisense shows this is a property.

 

Thanks again for the help.

 

From: 1T3XT BVBA [mailto:[hidden email]]
Sent: Friday, March 30, 2012 3:26 PM
To: Post all your questions about iText here
Subject: Re: [iText-questions] Aligning Text with Line Breaks

 

On 30/03/2012 20:59, Hugh McLaughlin wrote:

What I meant by calculation is that the example found here:

http://www.devshed.com/c/a/Java/Adding-Columns-With-iTextSharp/

Calculate the urx and ury corrodinates rather than specifying absolute positions.


Er... it depends what you mean by "calculate".
I see that absolute positions are specified:
lower-left corner: 72, 72
upper-right corner: 217, 288
So this is a rectangle of two by three inches.


  Here is where I am having a positioning problem:

I display the date with

cb.ShowTextAligned(3, "Date:  " & Now().ToShortDateString, 450, 600, 0)

When I use:

            Column1.SetSimpleColumn(72, 500, 300, 600)


I don't understand.
If you want to put a date on position x = 450, y = 600,
why are you creating a rectangle that will start writing on position x = 72, y = 600?


            Column1.Alignment = Element.ALIGN_TOP


??? Element.ALIGN_TOP ???
In ColumnText, you can only use Element.ALIGN_LEFT, Element.ALIGN_CENTER, Element.ALIGN_RIGHT and Element.ALIGN_JUSTIFIED.



I think it should draw rectangle that 228 x 100 with the top line of text displaying the same height on the page as the date.


ColumnText won't draw a rectangle, but use a rectangle of 228 x 100 as boundary for the text you're adding.
As for the same height: you're forgetting to take the leading into account.
450, 600 represents the start of the baseline of the date-text.
Whereas with the other ColumnText 72,600 represents the upper-left corner of the rectangle that will contain the text you're adding to the column.
The position of the baseline will depend on the leading of the text.
The leading that will be used will depend on whether you're working in text mode or in composite mode...
Suppose you're working in composite mode and you're using the default font size (12) and the default leading (1.5 times the font size), then the start of the baseline will be 72, 782, not 72, 600!


------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
iText-questions mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Aligning Text with Line Breaks

info-2
On 30/03/2012 21:31, Hugh McLaughlin wrote:
I am not sure how to do this, but will dig deeper.
The way I do it:
I create one columntext object:
ColumnText ct = new ColumnText(canvas);
And then I add elements like this:
ct.setSimpleColumn(dateRect);
ct.addElement(date);
ct.go();
ct.setSimpleColumn(textRect);
ct.addElement(text);
ct.go();
Because I don't mix the showTextAligned() method with the go() method, I don't have to worry about the difference in approach between the two methods. The one danger with my approach, is this: if the date Element doesn't fit in the dateRect, the remaining part will be shown in the textRect. Actually, this is the desired behavior in my use case: I don't want to lose any content.
In your case, you may want to ask the ct object if there's any text left, and either discard it, or repeat the process until the text fits (this is a case where you'll have to add the content in simulation mode first, and add it 'for real' as soon as the content fits the column).

------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
iText-questions mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php
Loading...