Rotate text / Vertical text in itextsharp

匿名 (未验证) 提交于 2019-12-03 03:06:01

问题:

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.

回答1:

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.



回答2:

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.



回答3:

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(); 

Here's the documentation for ShowTextAligned



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!