java Itext:在绝对位置显示段落

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15988315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:33:31  来源:igfitidea点击:

Itext: Show paragraph at absolute position

javaitext

提问by MTilsted

Is there a way to position a paragraph absolutely, in a way that also works when a list is added to the paragraph?

有没有办法绝对定位一个段落,在将列表添加到段落时也可以使用的方式?

A google search shows that I should use ColumnText, but I can't get that to work if there is a list in the paragraph. It simply shows the list items next to each other on the same linie. Here is my test program:

谷歌搜索显示我应该使用 ColumnText,但如果段落中有一个列表,我就无法使用它。它只是在同一行中显示彼此相邻的列表项。这是我的测试程序:

        PdfWriter writer=PdfWriter.getInstance(document,new FileOutputStream("/tmp/output.pdf"));
        document.open();
        ColumnText ct = new ColumnText(writer.getDirectContent());
        ct.setSimpleColumn(0,0,300,300);
        Paragraph p=new Paragraph();
        List list=new List();
        list.add(new ListItem("First item"));
        list.add(new ListItem("second item"));
        list.add(new ListItem("third item"));
        p.add(list);
        ct.addElement(p);
        ct.go();
        document.close();
        writer.close();

回答by Bruno Lowagie

I looked in the changelogs of iText and I discovered that this was fixed in iText 5.2.1, released on March 31st, 2012. That's over a year ago. Please upgrade to the latest version and the problem will disappear.

我查看了 iText 的更新日志,我发现这在 2012 年 3 月 31 日发布的 iText 5.2.1 中得到了修复。那是一年多前的事情了。请升级到最新版本,问题就会消失。

Note that all 5.2.x versions were removed from SourceForge because they contained a bug that occasionally produced PDFs that weren't compliant with ISO-32000-1. Based on the description of your problem, I know that you're using a version of iText that is even older than the 5.2.x series, so you definitely need to upgrade.

请注意,所有 5.2.x 版本都已从 SourceForge 中删除,因为它们包含一个偶尔会生成不符合 ISO-32000-1 的 PDF 的错误。根据您对问题的描述,我知道您使用的 iText 版本比 5.2.x 系列还要旧,因此您肯定需要升级。

回答by kavi temre

A different way to organize you content in PDF file is that you can use PdfPTable . first write list content into the table using loop instruction and then define the position of that PdfPTable in your output pdf file.

在 PDF 文件中组织内容的另一种方法是使用 PdfPTable 。首先使用循环指令将列表内容写入表格,然后定义该 PdfPTable 在输出 pdf 文件中的位置。

回答by Francesco Lo Cascio

You can use this function:

您可以使用此功能:

private void PlaceChunck(String text, int x, int y) {
    PdfContentByte cb = writer.DirectContent;
    BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    cb.SaveState();
    cb.BeginText();
    cb.MoveText(x, y);
    cb.SetFontAndSize(bf, 12);
    cb.ShowText(text);
    cb.EndText();
    cb.RestoreState();
}

see also: itext positioning text absolutely;

另请参阅:itext 绝对定位文本