How to insert Text in a given position with the given font

View: New views
7 Messages — Rating Filter:   Alert me  

How to insert Text in a given position with the given font

by notoriousxl :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi, this is my first post.

I have to insert some text in a itextsharp document. In few words: I have to write something similar to a ASP.NET label inside a PDF.

It could be great if I can specify position and dimension with a Rectangle object, and the font with a Font object (since I have to write the text with color, style and underline previously specified in the font object).

Is there a way to do this?

(sorry for my bad English)

Re: How to insert Text in a given position with the given font

by zoopside :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

here's what I use :

        protected static void PlaceText(PdfContentByte pdfContentByte
                                , string text
                                , iTextSharp.text.Font font
                                , float lowerLeftx
                                , float lowerLefty
                                , float upperRightx
                                , float upperRighty
                                , float leading
                                , int alignment)
        {
            ColumnText ct = new ColumnText(pdfContentByte);
            ct.SetSimpleColumn(new Phrase(text, font), lowerLeftx, lowerLefty, upperRightx, upperRighty, leading, alignment);
            ct.Go();
        }

and I call it like this :

PlaceText(pdfWriter.DirectContent, "Whatever you'd like to write", new Font(Font.HELVETICA, 9, Font.NORMAL), 316, 750, 585, 790, 14, Element.ALIGN_RIGHT);

On Mon, Sep 14, 2009 at 6:36 AM, notoriousxl <notoriousxl@...> wrote:

Hi, this is my first post.

I have to insert some text in a itextsharp document. In few words: I have to
write something similar to a ASP.NET label inside a PDF.

It could be great if I can specify position and dimension with a Rectangle
object, and the font with a Font object (since I have to write the text with
color, style and underline previously specified in the font object).

Is there a way to do this?

(sorry for my bad English)
--
View this message in context: http://www.nabble.com/How-to-insert-Text-in-a-given-position-with-the-given-font-tp25434266p25434266.html
Sent from the itextsharp-questions mailing list archive at Nabble.com.


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
itextsharp-questions mailing list
itextsharp-questions@...
https://lists.sourceforge.net/lists/listinfo/itextsharp-questions


------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
itextsharp-questions mailing list
itextsharp-questions@...
https://lists.sourceforge.net/lists/listinfo/itextsharp-questions

Re: How to insert Text in a given position with the given font

by notoriousxl :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thank you very much :) (your example helped me to understand what all those llx, urx, ... stand for)

Now, I'm going to ask you an impossible question: is there a way to rotate the text (90°, 180°, 270°) inside this label?

zoopside wrote:
here's what I use :

        protected static void PlaceText(PdfContentByte pdfContentByte
                                , string text
                                , iTextSharp.text.Font font
                                , float lowerLeftx
                                , float lowerLefty
                                , float upperRightx
                                , float upperRighty
                                , float leading
                                , int alignment)
        {
            ColumnText ct = new ColumnText(pdfContentByte);
            ct.SetSimpleColumn(new Phrase(text, font), lowerLeftx,
lowerLefty, upperRightx, upperRighty, leading, alignment);
            ct.Go();
        }

and I call it like this :

PlaceText(pdfWriter.DirectContent, "Whatever you'd like to write", new
Font(Font.HELVETICA, 9, Font.NORMAL), 316, 750, 585, 790, 14,
Element.ALIGN_RIGHT);

Re: How to insert Text in a given position with the given font

by Paulo Soares-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

It may be easier to use a PdfPTable cell and benefit from the cell rotation if you are not at ease with matrix transformations.

Paulo

> -----Original Message-----
> From: notoriousxl [mailto:notoriousxl@...]
> Sent: Tuesday, September 15, 2009 12:01 PM
> To: itextsharp-questions@...
> Subject: Re: [itextsharp-questions] How to insert Text in a
> given position with the given font
>
>
> Thank you very much :) (your example helped me to understand
> what all those
> llx, urx, ... stand for)
>
> Now, I'm going to ask you an impossible question: is there a
> way to rotate
> the text (90°, 180°, 270°) inside this label?
>
>
> zoopside wrote:
> >
> > here's what I use :
> >
> >         protected static void PlaceText(PdfContentByte
> pdfContentByte
> >                                 , string text
> >                                 , iTextSharp.text.Font font
> >                                 , float lowerLeftx
> >                                 , float lowerLefty
> >                                 , float upperRightx
> >                                 , float upperRighty
> >                                 , float leading
> >                                 , int alignment)
> >         {
> >             ColumnText ct = new ColumnText(pdfContentByte);
> >             ct.SetSimpleColumn(new Phrase(text, font), lowerLeftx,
> > lowerLefty, upperRightx, upperRighty, leading, alignment);
> >             ct.Go();
> >         }
> >
> > and I call it like this :
> >
> > PlaceText(pdfWriter.DirectContent, "Whatever you'd like to
> write", new
> > Font(Font.HELVETICA, 9, Font.NORMAL), 316, 750, 585, 790, 14,
> > Element.ALIGN_RIGHT);


Aviso Legal:
Esta mensagem é destinada exclusivamente ao destinatário. Pode conter informação confidencial ou legalmente protegida. A incorrecta transmissão desta mensagem não significa a perca de confidencialidade. Se esta mensagem for recebida por engano, por favor envie-a de volta para o remetente e apague-a do seu sistema de imediato. É proibido a qualquer pessoa que não o destinatário de usar, revelar ou distribuir qualquer parte desta mensagem.

Disclaimer:
This message is destined exclusively to the intended receiver. It may contain confidential or legally protected information. The incorrect transmission of this message does not mean the loss of its confidentiality. If this message is received by mistake, please send it back to the sender and delete it from your system immediately. It is forbidden to any person who is not the intended receiver to use, distribute or copy any part of this message.

------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
itextsharp-questions mailing list
itextsharp-questions@...
https://lists.sourceforge.net/lists/listinfo/itextsharp-questions

Re: How to insert Text in a given position with the given font

by notoriousxl :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Paulo Soares-3 wrote:
It may be easier to use a PdfPTable cell and benefit from the cell rotation if you are not at ease with matrix transformations.

Paulo
Ok :) I've achivied some results (thanks for your help so far, all of you), both with PdfContentByte + SetTextMatrix and PdfPCell. The problems so far:

1) SetTextMatrix:
the following code works just fine...

PdfContentByte cb = writer.DirectContent;
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.BeginText();
cb.SetFontAndSize(bf, 12);
cb.SetTextMatrix(0, -1, 1, 0, 200, 300); // x = 200, y = 300
cb.ShowText("Rotation 180°");
cb.Stroke();
cb.EndText();

... but, since I have to set the font style (bold, italic, underlined), I can't do that with BaseFont... I need a Font object

2) PdfPCell:
With this cell I can use a Font object (and I can set things such the border, which I have to do anyway), but... the following code doesn't display any text, only the cell's background and border:

var cell = new PdfPCell(new Phrase("Hello", new Font(BaseFont.CreateFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 20, Font.STRIKETHRU)));
cell.BackgroundColor = Color.YELLOW;
cell.BorderColor = Color.BLACK;
cell.BorderWidth = 2;
cell.Rotation = 90;
cell.Left = 100; cell.Bottom = 200; //it should be the cell's position
document.Add(cell);

I should also set cell's position, width and height. Any idea?

Re: How to insert Text in a given position with the given font

by notoriousxl :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

notoriousxl wrote:
2) PdfPCell:
With this cell I can use a Font object (and I can set things such the border, which I have to do anyway), but... the following code doesn't display any text, only the cell's background and border:
UPDATE: using a PdfPCell everything works fine; the only problem is the vertical alignment. No matter what I do ( cell.Padding = myConstant; cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; ) the text doesn't center vertically (I can see this setting a border on my cell). It's as if padding top is greater than padding bottom (and the text "escapes" from the bottom border).

Is there a solution to this?

Re: How to insert Text in a given position with the given font

by Paulo Soares-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

If you have a single cell the cell's size will be that of the text. Call PdfPCell.FixedHeight to see the text align.

Paulo

> -----Original Message-----
> From: notoriousxl [mailto:notoriousxl@...]
> Sent: Wednesday, September 16, 2009 2:30 PM
> To: itextsharp-questions@...
> Subject: Re: [itextsharp-questions] How to insert Text in a
> given position with the given font
>
>
>
> notoriousxl wrote:
> >
> > 2) PdfPCell:
> > With this cell I can use a Font object (and I can set
> things such the
> > border, which I have to do anyway), but... the following
> code doesn't
> > display any text, only the cell's background and border:
> >
>
> UPDATE: using a PdfPCell everything works fine; the only
> problem is the
> vertical alignment. No matter what I do ( cell.Padding = myConstant;
> cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; ) the text
> doesn't center
> vertically (I can see this setting a border on my cell). It's
> as if padding
> top is greater than padding bottom (and the text "escapes"
> from the bottom
> border).
>
> Is there a solution to this?
>
> --
> View this message in context:
> http://www.nabble.com/How-to-insert-Text-in-a-given-position-w
> ith-the-given-font-tp25434266p25472413.html
> Sent from the itextsharp-questions mailing list archive at Nabble.com.
>
>
> --------------------------------------------------------------
> ----------------
> Come build with us! The BlackBerry® Developer Conference in SF, CA
> is the only developer event you need to attend this year.
> Jumpstart your
> developing skills, take BlackBerry mobile applications to
> market and stay
> ahead of the curve. Join us from November 9-12, 2009.
> Register now!
> http://p.sf.net/sfu/devconf
> _______________________________________________
> itextsharp-questions mailing list
> itextsharp-questions@...
> https://lists.sourceforge.net/lists/listinfo/itextsharp-questions
>
Aviso Legal:
Esta mensagem é destinada exclusivamente ao destinatário. Pode conter informação confidencial ou legalmente protegida. A incorrecta transmissão desta mensagem não significa a perca de confidencialidade. Se esta mensagem for recebida por engano, por favor envie-a de volta para o remetente e apague-a do seu sistema de imediato. É proibido a qualquer pessoa que não o destinatário de usar, revelar ou distribuir qualquer parte desta mensagem.

Disclaimer:
This message is destined exclusively to the intended receiver. It may contain confidential or legally protected information. The incorrect transmission of this message does not mean the loss of its confidentiality. If this message is received by mistake, please send it back to the sender and delete it from your system immediately. It is forbidden to any person who is not the intended receiver to use, distribute or copy any part of this message.



------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
itextsharp-questions mailing list
itextsharp-questions@...
https://lists.sourceforge.net/lists/listinfo/itextsharp-questions