iTextSharp ShowTextAligned锚点 [英] iTextSharp ShowTextAligned Anchor Point

查看:828
本文介绍了iTextSharp ShowTextAligned锚点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用iTextSharp的ShowTextAligned方法成功地将文本添加到PDF.该方法如下所示(C#):

I'm currently successfully adding text to a PDF using iTextSharp's ShowTextAligned method. The method looks like this (C#):

public void ShowTextAligned(
    int alignment,
    string text,
    float x,
    float y,
    float rotation
)

但是,尚不清楚我们正在制作的文本的锚点在哪里.我们提供了xy,但是它们对应于文本矩形的左上角,左下角还是其他?还会受行间距的影响吗?

However, it is unclear where the anchor point is for the text we're making. We provide x and y, but does these correspond to the upper left corner of the text rectangle, the lower left corner, or something else? Also is this impacted by line spacing?

我在网站上查看了文档,但是这不是很解释.请参见PdfContentByte类/PdfContentByte方法/ShowTextAligned方法.

I looked at the documentation at this website, but it isn't very explanatory. See PdfContentByte Class / PdfContentByte Methods / ShowTextAligned Method.

推荐答案

显然,定位点取决于对齐方式.如果锚点位于文本的左侧,那么说您右对齐是没有意义的.

Obviously the anchor point depends on the kind of alignment. It does not make sense to say you right-align if your anchor point is at the left side of the text.

此外,文本操作通常相对于基线对齐.

Furthermore, text operations usually align relative to the baseline.

因此:

  • 对于左对齐的文本,定位点是文本基线的最左点.
  • 对于居中对齐的文本,定位点是文本基线的中间点.
  • 对于右对齐的文本,锚点是文本基线的最右点.

更直观:

这是使用以下方法生成的:

This has been generated using:

[Test]
public void ShowAnchorPoints()
{
    Directory.CreateDirectory(@"C:\Temp\test-results\content\");
    string dest = @"C:\Temp\test-results\content\showAnchorPoints.pdf";

    using (Document document = new Document())
    {
        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(dest, FileMode.Create, FileAccess.Write));
        document.Open();

        PdfContentByte canvas = writer.DirectContent;

        canvas.MoveTo(300, 100);
        canvas.LineTo(300, 700);
        canvas.MoveTo(100, 300);
        canvas.LineTo(500, 300);
        canvas.MoveTo(100, 400);
        canvas.LineTo(500, 400);
        canvas.MoveTo(100, 500);
        canvas.LineTo(500, 500);
        canvas.Stroke();

        ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("Left aligned"), 300, 500, 0);
        ColumnText.ShowTextAligned(canvas, Element.ALIGN_CENTER, new Phrase("Center aligned"), 300, 400, 0);
        ColumnText.ShowTextAligned(canvas, Element.ALIGN_RIGHT, new Phrase("Right aligned"), 300, 300, 0);
    }
}

这篇关于iTextSharp ShowTextAligned锚点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆