Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate text / Vertical text in itextsharp

Tags:

c#

itextsharp

I need vertical text or just a way to rotate a ColumnText in ITextSharp.

(It needs to be absolute position)

Until now i have tried a lot of diffrent solution, but with no luck.

Here is a couple of tries:

1.

_cb.SetFontAndSize(BaseFont.CreateFont(), 12f);
_cb.ShowTextAligned(Element.ALIGN_CENTER, "Hello World", 50, 50, 90);

2.

var vt =  new VerticalText(_cb);
vt.SetVerticalLayout(50, 50, 400, 8, 30);            
vt.AddText(new Chunk("asdasd",_sf.ChildBackPageTextOneFont()));
vt.Go();

3.

System.Drawing.Drawing2D.Matrix foo = new System.Drawing.Drawing2D.Matrix();
foo.Rotate(90);
_cb.ConcatCTM(foo);

I have also tried to draw it with System.Drawing.Graphics, but the quality is VERY poor.

Any solution? Thanks.

like image 932
Luticka Avatar asked Dec 08 '10 15:12

Luticka


3 Answers

Actually, the easiest way is similar to your first try. You just needed to add a call to BeginText() and EndText() like this

_cb.SetFontAndSize(BaseFont.CreateFont(), 12f);
_cb.BeginText();
_cb.ShowTextAligned(Element.ALIGN_CENTER, "Hello World", 50, 50, 90);
_cb.EndText();
_cb.Stroke();
like image 177
Stefan Mihai Stanescu Avatar answered Oct 21 '22 03:10

Stefan Mihai Stanescu


I have tried a lot of methods from the web for this rotate issue. But none of them worked. Finally I figured out a simple solution. Maybe we can do it like this. We can draw a table with no borders, and just with one cell. And we add text in the cell, finally rotate the cell. Every is ok then.

        table = new PdfPTable(1);
        table.TotalWidth = 72;
        paragraph = new Paragraph("123");
        cell = new PdfPCell(paragraph);
        cell.Rotation = 270;
        cell.BorderWidth = 0;
        table.AddCell(cell);
        table.WriteSelectedRows(0, -1, 72, 72, writer.DirectContent);

Besides, the WriteSelectedRows method can position this cell.

like image 39
Sun Robin Avatar answered Oct 21 '22 03:10

Sun Robin


Found the answer:

Use something like this:

Imports System.Drawing, System.Drawing.Drawing2D
Dim transf as new Matrix
transf.RotateAt(30,New PointF(100,100), MatrixOrder.Append)
writer.DirectContent.Transform(transf)

transf.Invert()
writer.DirectContent.Transform(transf)

Rotate the canvas, write some text, rotate it back.

like image 4
Luticka Avatar answered Oct 21 '22 03:10

Luticka