Christoph Wille
Christoph Wille

Reputation: 311

iTextSharp and VerticalText

I followed the iText samples for vertical text:

http://1t3xt.info/examples/browse/?page=example&id=145

and created this C# version of it:

PdfReader reader = new PdfReader("existing.pdf");
PdfStamper stamp = new PdfStamper(reader, new FileStream("stamped.pdf", FileMode.Create));

// change the content on top of page 1
PdfContentByte cb = stamp.GetOverContent(1);

Rectangle psize = reader.GetPageSize(1);
float width = psize.Width;
float height = psize.Height;

BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
VerticalText vt = new VerticalText(cb);
vt.SetVerticalLayout(width / 2, height / 2, height, 1, 0);
vt.AddText(new Phrase("Test", new Font(bf, 20)));
vt.Go();

stamp.Close();

It is centered on the page, alright, but it is not vertical - but horizontal (actually left aligned horizontally from the center of the page).

Am I doing something wrong here or is iTextSharp misbehaving?

Upvotes: 2

Views: 3990

Answers (2)

Mark Storer
Mark Storer

Reputation: 15870

The parameters you're passing to setVerticalLayout are the likely culprit.

// from the java source
public void setVerticalLayout(float startX, 
                              float startY, 
                              float height, 
                              int maxLines,
                              float leading)

So your startX and startY are pointed at the center of the page, and your available height is the height of the page (leaving half the defined area off the bottom of the page). You are also restricting it to a single line, with zero leading. In theory, your text would start at the center of the page, and continue downward off the page bottom.

In practice, you're getting Something Else Entirely.

There may also be an issue with building a font from a base font in this case, unless that font happens to have Identity-V encoding, BaseFont.IDENTITY_V.

OTOH, if your baseFont is already in Identity-V, then I'd guess that VerticalText is expecting to have to mangle horizontally "encoded" text into a vertical alignment and ends up doing just the opposite with vertically "encoded" text.

How odd. I'd love to hear an update.

Upvotes: 2

one name
one name

Reputation: 1

Try

cb.ShowTextAligned(alignment, text, x, y, rotation);

Upvotes: 0

Related Questions