2

我遵循了垂直文本的 iText 示例:

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

并创建了它的这个 C# 版本:

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

它在页面上居中,好吧,但它不是垂直的 - 而是水平的(实际上从页面中心水平左对齐)。

我在这里做错了什么还是 iTextSharp 行为不端?

4

2 回答 2

2

您传递给 setVerticalLayout 的参数可能是罪魁祸首。

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

所以你的 startX 和 startY 指向页面的中心,你的可用高度是页面的高度(离开页面底部的定义区域的一半)。您还将其限制为一行,前导为零。理论上,您的文本将从页面中心开始,并从页面底部向下继续。

在实践中,你得到了完全不同的东西。

在这种情况下,从基本字体构建字体也可能存在问题,除非该字体恰好具有 Identity-V 编码,BaseFont.IDENTITY_V.

OTOH,如果您的 baseFont 已经在 Identity-V 中,那么我猜 VerticalText 期望必须将水平“编码”文本转换为垂直对齐,并最终与垂直“编码”文本相反。

多么奇怪。我很想听听更新。

于 2010-12-21T17:49:19.683 回答
0

尝试

cb.ShowTextAligned(对齐、文本、x、y、旋转);

于 2010-05-04T13:33:31.533 回答