如何使用iText相对于页面定位文本? [英] How to position text relative to page using iText?

查看:236
本文介绍了如何使用iText相对于页面定位文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置文本的位置,使其相对于页面大小垂直居中?我想定位它例如从右边开始x个点并垂直居中。当然文本旋转了90度。

How do I set the position of text so that it is centered vertically relative to its page size? I want to position it say for example x number of points from right and centered vertically. The text of course is rotated 90 degrees.

            int n = reader.getNumberOfPages();
            PdfImportedPage page;
            PdfCopy.PageStamp stamp;
            for (int j = 0; j < n; )
            {
                ++j;
                page = writer.getImportedPage(reader, j);
                stamp = writer.createPageStamp(page);
                Rectangle crop = reader.getCropBox(1);
                // add overlay text
                Phrase phrase = new Phrase("Overlay Text");
                ColumnText.showTextAligned(stamp.getOverContent(), Element.ALIGN_CENTER, phrase,
                        crop.getRight(72f), crop.getHeight() / 2, 90);
                stamp.alterContents();
                writer.addPage(page);
            }

上面的代码给出了文本的不一致位置,在某些页面中,只有叠加文本的一部分是可见的。请帮助,我不知道如何正确使用mediabox和cropbox,我是itext的新手。

The code above gives me inconsistent position of text, and in some pages, only a portion of the "Overlay text" is visible. Please help, I don't know how to properly use mediabox and cropbox and I'm new to itext.

谢谢!

推荐答案

关于不一致的位置:应该通过添加垂直偏移来修复:

Regarding the inconsistent position: that should be fixed by adding the vertical offset:

crop.getRight(72f), crop.getBottom() + crop.getHeight() / 2



你知道吗?您使用1英寸边距的右边框作为 x 坐标,但是您忘记考虑 y 页面底部的坐标(它并不总是 0 )。通常情况下,这应该可以解决定位问题。

Do you see? You took the right border with a margin of 1 inch as x coordinate, but you forgot to take into account the y coordinate of the bottom of the page (it's not always 0). Normally, this should fix the positioning problem.

关于只有部分叠加文字可见的事实:我的第一个猜测就是你要在现有内容,但猜测是错误的(您正在使用 getOverContent())。第二个问题到底是什么意思?你是否意味着文本被CropBox剪裁?您是否正在寻找一种方法来衡量短语的内容,看它是否适合您添加之前的高度?

Regarding the fact that only a portion of the overlay text is visible: my first guess was that you're adding content under the existing content, but that guess is wrong (you're using getOverContent()). What exactly do you mean by that second question? Do yo mean the text is clipped by the CropBox? Are you looking for a way to measure the content of phrase to see if it fits the height before you add it?

这篇关于如何使用iText相对于页面定位文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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