itext块互相写

发布于 2025-02-08 05:47:48 字数 868 浏览 1 评论 0原文

我有一个简单的代码,将句子(代码中的命名内容)分为令牌,然后在PDF文件中写入令牌,但每个句子都在新行中。

        Document document = new Document(PageSize.A4, 20, 20, 20, 20);
        PdfWriter.getInstance(document, new FileOutputStream("ticket.pdf"));
        document.open();
        Font font = FontFactory.getFont(FontFactory.COURIER, 16, BaseColor.BLACK);
        String[] tokens = content.split("\\|");
        for (String token : tokens) {
            Chunk chunk = new Chunk(token, font);
            document.add(chunk);
            document.add(Chunk.NEWLINE);
        }
        document.close();

但是,当我打开结果pdf文件时,所有令牌(块)都在第一行中互相写入:

“在这里输入图像描述”

有人可以指向我我在这里做错了什么?代码用Java 11用Spring Boot编写,Itext版本为5.5.10

I have a simple piece of code that splits a sentence (named content in the code) into tokens and then writes tokens in pdf file, but each in new line.

        Document document = new Document(PageSize.A4, 20, 20, 20, 20);
        PdfWriter.getInstance(document, new FileOutputStream("ticket.pdf"));
        document.open();
        Font font = FontFactory.getFont(FontFactory.COURIER, 16, BaseColor.BLACK);
        String[] tokens = content.split("\\|");
        for (String token : tokens) {
            Chunk chunk = new Chunk(token, font);
            document.add(chunk);
            document.add(Chunk.NEWLINE);
        }
        document.close();

But when I open the result pdf file, all of the tokens (chunks) are written over each other in the first line, like this:

enter image description here

Can anyone point to me what am I doing wrong here? Code is written in Spring Boot with Java 11, and iText version is 5.5.10

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文